From 67fb26b0be3d5bc6848608b59c26dd216befbaa8 Mon Sep 17 00:00:00 2001 From: daniel Date: Fri, 29 Jun 2018 11:20:31 +0800 Subject: [PATCH 01/11] =?UTF-8?q?druid=20spring=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fine-druid/pom.xml | 18 ++ .../stat/JdbcStatManagerFactoryBean.java | 2 +- .../SpringIbatisBeanNameAutoProxyCreator.java | 4 +- .../SpringIbatisBeanTypeAutoProxyCreator.java | 12 +- .../spring/MBeanServerFactoryBean.java | 2 +- .../spring/mvc/StatHandlerInterceptor.java | 10 +- .../spring/stat/BeanTypeAutoProxyCreator.java | 20 +-- .../spring/stat/DruidStatInterceptor.java | 10 +- .../annotation/StatAnnotationAdvisor.java | 14 +- .../StatAnnotationBeanPostProcessor.java | 8 +- .../config/DruidStatBeanDefinitionParser.java | 16 +- .../config/DruidStatNamespaceHandler.java | 2 +- fine-hibernate/pom.xml | 18 ++ .../access/spi/UnsafeGetterFieldImpl.java | 116 +++++++++++++ .../access/spi/UnsafeSetterMethodImpl.java | 160 ++++++++++++++++++ 15 files changed, 362 insertions(+), 50 deletions(-) create mode 100644 fine-druid/pom.xml create mode 100644 fine-hibernate/pom.xml create mode 100644 fine-hibernate/src/com/fr/third/org/hibernate/property/access/spi/UnsafeGetterFieldImpl.java create mode 100644 fine-hibernate/src/com/fr/third/org/hibernate/property/access/spi/UnsafeSetterMethodImpl.java diff --git a/fine-druid/pom.xml b/fine-druid/pom.xml new file mode 100644 index 000000000..299bfd9f2 --- /dev/null +++ b/fine-druid/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + + + com.fr.third + base-third-code + 10.0-RELEASE-SNAPSHOT + + + com.fr.third + fine-druid + 10.0-RELEASE-SNAPSHOT + + + \ No newline at end of file diff --git a/fine-druid/src/com/fr/third/alibaba/druid/stat/JdbcStatManagerFactoryBean.java b/fine-druid/src/com/fr/third/alibaba/druid/stat/JdbcStatManagerFactoryBean.java index 8a9476640..c11c3f7ea 100644 --- a/fine-druid/src/com/fr/third/alibaba/druid/stat/JdbcStatManagerFactoryBean.java +++ b/fine-druid/src/com/fr/third/alibaba/druid/stat/JdbcStatManagerFactoryBean.java @@ -15,7 +15,7 @@ */ package com.fr.third.alibaba.druid.stat; -import org.springframework.beans.factory.FactoryBean; +import com.fr.third.springframework.beans.factory.FactoryBean; public class JdbcStatManagerFactoryBean implements FactoryBean { diff --git a/fine-druid/src/com/fr/third/alibaba/druid/support/ibatis/SpringIbatisBeanNameAutoProxyCreator.java b/fine-druid/src/com/fr/third/alibaba/druid/support/ibatis/SpringIbatisBeanNameAutoProxyCreator.java index 7ed48fe12..a90775d1a 100644 --- a/fine-druid/src/com/fr/third/alibaba/druid/support/ibatis/SpringIbatisBeanNameAutoProxyCreator.java +++ b/fine-druid/src/com/fr/third/alibaba/druid/support/ibatis/SpringIbatisBeanNameAutoProxyCreator.java @@ -20,8 +20,8 @@ import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.aop.TargetSource; -import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator; +import com.fr.third.springframework.aop.TargetSource; +import com.fr.third.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient; diff --git a/fine-druid/src/com/fr/third/alibaba/druid/support/ibatis/SpringIbatisBeanTypeAutoProxyCreator.java b/fine-druid/src/com/fr/third/alibaba/druid/support/ibatis/SpringIbatisBeanTypeAutoProxyCreator.java index e89525de6..6a49278c0 100644 --- a/fine-druid/src/com/fr/third/alibaba/druid/support/ibatis/SpringIbatisBeanTypeAutoProxyCreator.java +++ b/fine-druid/src/com/fr/third/alibaba/druid/support/ibatis/SpringIbatisBeanTypeAutoProxyCreator.java @@ -19,11 +19,11 @@ import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.aop.TargetSource; -import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator; -import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.FactoryBean; -import org.springframework.util.PatternMatchUtils; +import com.fr.third.springframework.aop.TargetSource; +import com.fr.third.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator; +import com.fr.third.springframework.beans.factory.BeanFactory; +import com.fr.third.springframework.beans.factory.FactoryBean; +import com.fr.third.springframework.util.PatternMatchUtils; import java.util.ArrayList; import java.util.List; @@ -95,7 +95,7 @@ public class SpringIbatisBeanTypeAutoProxyCreator extends AbstractAutoProxyCreat * @param beanName the bean name to check * @param mappedName the name in the configured list of names * @return if the names match - * @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String) + * @see com.fr.third.springframework.util.PatternMatchUtils#simpleMatch(String, String) */ protected boolean isMatch(String beanName, String mappedName) { return PatternMatchUtils.simpleMatch(mappedName, beanName); diff --git a/fine-druid/src/com/fr/third/alibaba/druid/support/spring/MBeanServerFactoryBean.java b/fine-druid/src/com/fr/third/alibaba/druid/support/spring/MBeanServerFactoryBean.java index 323b487f2..f09efc077 100644 --- a/fine-druid/src/com/fr/third/alibaba/druid/support/spring/MBeanServerFactoryBean.java +++ b/fine-druid/src/com/fr/third/alibaba/druid/support/spring/MBeanServerFactoryBean.java @@ -19,7 +19,7 @@ import java.lang.management.ManagementFactory; import javax.management.MBeanServer; -import org.springframework.beans.factory.FactoryBean; +import com.fr.third.springframework.beans.factory.FactoryBean; public class MBeanServerFactoryBean implements FactoryBean { diff --git a/fine-druid/src/com/fr/third/alibaba/druid/support/spring/mvc/StatHandlerInterceptor.java b/fine-druid/src/com/fr/third/alibaba/druid/support/spring/mvc/StatHandlerInterceptor.java index 310a44dc3..834c028f8 100644 --- a/fine-druid/src/com/fr/third/alibaba/druid/support/spring/mvc/StatHandlerInterceptor.java +++ b/fine-druid/src/com/fr/third/alibaba/druid/support/spring/mvc/StatHandlerInterceptor.java @@ -24,11 +24,11 @@ import javax.servlet.http.HttpServletResponse; import com.fr.third.alibaba.druid.filter.stat.StatFilterContext; import com.fr.third.alibaba.druid.support.http.stat.WebSessionStat; import com.fr.third.alibaba.druid.support.http.stat.WebURIStat; -import org.springframework.beans.factory.DisposableBean; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.web.servlet.HandlerInterceptor; -import org.springframework.web.servlet.HandlerMapping; -import org.springframework.web.servlet.ModelAndView; +import com.fr.third.springframework.beans.factory.DisposableBean; +import com.fr.third.springframework.beans.factory.InitializingBean; +import com.fr.third.springframework.web.servlet.HandlerInterceptor; +import com.fr.third.springframework.web.servlet.HandlerMapping; +import com.fr.third.springframework.web.servlet.ModelAndView; import com.fr.third.alibaba.druid.support.http.AbstractWebStatImpl; import com.fr.third.alibaba.druid.support.http.stat.WebAppStat; diff --git a/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/BeanTypeAutoProxyCreator.java b/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/BeanTypeAutoProxyCreator.java index 49de94af4..131fb9a20 100644 --- a/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/BeanTypeAutoProxyCreator.java +++ b/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/BeanTypeAutoProxyCreator.java @@ -15,15 +15,15 @@ */ package com.fr.third.alibaba.druid.support.spring.stat; -import org.springframework.aop.TargetSource; -import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator; -import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.FactoryBean; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; -import org.springframework.util.Assert; -import org.springframework.util.PatternMatchUtils; +import com.fr.third.springframework.aop.TargetSource; +import com.fr.third.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator; +import com.fr.third.springframework.beans.factory.BeanFactory; +import com.fr.third.springframework.beans.factory.FactoryBean; +import com.fr.third.springframework.beans.factory.InitializingBean; +import com.fr.third.springframework.context.ApplicationContext; +import com.fr.third.springframework.context.ApplicationContextAware; +import com.fr.third.springframework.util.Assert; +import com.fr.third.springframework.util.PatternMatchUtils; import java.util.ArrayList; import java.util.Collections; @@ -83,7 +83,7 @@ public class BeanTypeAutoProxyCreator extends AbstractAutoProxyCreator implement * @param beanName the bean name to check * @param mappedName the name in the configured list of names * @return if the names match - * @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String) + * @see com.fr.third.springframework.util.PatternMatchUtils#simpleMatch(String, String) */ protected boolean isMatch(String beanName, String mappedName) { return PatternMatchUtils.simpleMatch(mappedName, beanName); diff --git a/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/DruidStatInterceptor.java b/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/DruidStatInterceptor.java index 5c50b63f4..82a2604b4 100644 --- a/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/DruidStatInterceptor.java +++ b/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/DruidStatInterceptor.java @@ -21,9 +21,9 @@ import com.fr.third.alibaba.druid.support.logging.Log; import com.fr.third.alibaba.druid.support.logging.LogFactory; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; -import org.springframework.aop.TargetSource; -import org.springframework.beans.factory.DisposableBean; -import org.springframework.beans.factory.InitializingBean; +import com.fr.third.springframework.aop.TargetSource; +import com.fr.third.springframework.beans.factory.DisposableBean; +import com.fr.third.springframework.beans.factory.InitializingBean; import java.lang.reflect.Method; @@ -123,8 +123,8 @@ public class DruidStatInterceptor implements MethodInterceptor, InitializingBean try { // 最多支持10层代理 for (int i = 0; i < 10; ++i) { - if (thisObject instanceof org.springframework.aop.framework.Advised) { - TargetSource targetSource = ((org.springframework.aop.framework.Advised) thisObject).getTargetSource(); + if (thisObject instanceof com.fr.third.springframework.aop.framework.Advised) { + TargetSource targetSource = ((com.fr.third.springframework.aop.framework.Advised) thisObject).getTargetSource(); if (targetSource == null) { break; diff --git a/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/annotation/StatAnnotationAdvisor.java b/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/annotation/StatAnnotationAdvisor.java index 5c0ab8524..622a57f08 100644 --- a/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/annotation/StatAnnotationAdvisor.java +++ b/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/annotation/StatAnnotationAdvisor.java @@ -17,13 +17,13 @@ package com.fr.third.alibaba.druid.support.spring.stat.annotation; import com.fr.third.alibaba.druid.support.spring.stat.DruidStatInterceptor; import org.aopalliance.aop.Advice; -import org.springframework.aop.Pointcut; -import org.springframework.aop.support.AbstractPointcutAdvisor; -import org.springframework.aop.support.ComposablePointcut; -import org.springframework.aop.support.annotation.AnnotationMatchingPointcut; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.BeanFactoryAware; +import com.fr.third.springframework.aop.Pointcut; +import com.fr.third.springframework.aop.support.AbstractPointcutAdvisor; +import com.fr.third.springframework.aop.support.ComposablePointcut; +import com.fr.third.springframework.aop.support.annotation.AnnotationMatchingPointcut; +import com.fr.third.springframework.beans.BeansException; +import com.fr.third.springframework.beans.factory.BeanFactory; +import com.fr.third.springframework.beans.factory.BeanFactoryAware; @SuppressWarnings("serial") public class StatAnnotationAdvisor extends AbstractPointcutAdvisor implements BeanFactoryAware { diff --git a/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/annotation/StatAnnotationBeanPostProcessor.java b/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/annotation/StatAnnotationBeanPostProcessor.java index a585c3da0..44033243e 100644 --- a/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/annotation/StatAnnotationBeanPostProcessor.java +++ b/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/annotation/StatAnnotationBeanPostProcessor.java @@ -18,10 +18,10 @@ package com.fr.third.alibaba.druid.support.spring.stat.annotation; import javax.annotation.Resource; import com.fr.third.alibaba.druid.support.spring.stat.DruidStatInterceptor; -import org.springframework.aop.framework.AbstractAdvisingBeanPostProcessor; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.BeanFactoryAware; +import com.fr.third.springframework.aop.framework.AbstractAdvisingBeanPostProcessor; +import com.fr.third.springframework.beans.BeansException; +import com.fr.third.springframework.beans.factory.BeanFactory; +import com.fr.third.springframework.beans.factory.BeanFactoryAware; @SuppressWarnings("serial") public class StatAnnotationBeanPostProcessor extends AbstractAdvisingBeanPostProcessor implements BeanFactoryAware { diff --git a/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/config/DruidStatBeanDefinitionParser.java b/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/config/DruidStatBeanDefinitionParser.java index 8491f7ae5..28a519369 100644 --- a/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/config/DruidStatBeanDefinitionParser.java +++ b/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/config/DruidStatBeanDefinitionParser.java @@ -15,14 +15,14 @@ */ package com.fr.third.alibaba.druid.support.spring.stat.config; -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.config.BeanDefinitionHolder; -import org.springframework.beans.factory.parsing.BeanComponentDefinition; -import org.springframework.beans.factory.parsing.CompositeComponentDefinition; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.support.BeanDefinitionRegistry; -import org.springframework.beans.factory.xml.BeanDefinitionParser; -import org.springframework.beans.factory.xml.ParserContext; +import com.fr.third.springframework.beans.factory.config.BeanDefinition; +import com.fr.third.springframework.beans.factory.config.BeanDefinitionHolder; +import com.fr.third.springframework.beans.factory.parsing.BeanComponentDefinition; +import com.fr.third.springframework.beans.factory.parsing.CompositeComponentDefinition; +import com.fr.third.springframework.beans.factory.support.BeanDefinitionBuilder; +import com.fr.third.springframework.beans.factory.support.BeanDefinitionRegistry; +import com.fr.third.springframework.beans.factory.xml.BeanDefinitionParser; +import com.fr.third.springframework.beans.factory.xml.ParserContext; import org.w3c.dom.Element; public class DruidStatBeanDefinitionParser implements BeanDefinitionParser { diff --git a/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/config/DruidStatNamespaceHandler.java b/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/config/DruidStatNamespaceHandler.java index 5e6be134f..34f679280 100644 --- a/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/config/DruidStatNamespaceHandler.java +++ b/fine-druid/src/com/fr/third/alibaba/druid/support/spring/stat/config/DruidStatNamespaceHandler.java @@ -15,7 +15,7 @@ */ package com.fr.third.alibaba.druid.support.spring.stat.config; -import org.springframework.beans.factory.xml.NamespaceHandlerSupport; +import com.fr.third.springframework.beans.factory.xml.NamespaceHandlerSupport; public class DruidStatNamespaceHandler extends NamespaceHandlerSupport { diff --git a/fine-hibernate/pom.xml b/fine-hibernate/pom.xml new file mode 100644 index 000000000..3fca883d7 --- /dev/null +++ b/fine-hibernate/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + + + com.fr.third + base-third-code + 10.0-RELEASE-SNAPSHOT + + + com.fr.third + fine-hibernate + 10.0-RELEASE-SNAPSHOT + + + \ No newline at end of file diff --git a/fine-hibernate/src/com/fr/third/org/hibernate/property/access/spi/UnsafeGetterFieldImpl.java b/fine-hibernate/src/com/fr/third/org/hibernate/property/access/spi/UnsafeGetterFieldImpl.java new file mode 100644 index 000000000..054e5c213 --- /dev/null +++ b/fine-hibernate/src/com/fr/third/org/hibernate/property/access/spi/UnsafeGetterFieldImpl.java @@ -0,0 +1,116 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package com.fr.third.org.hibernate.property.access.spi; + +import com.fr.third.org.hibernate.engine.spi.SessionImplementor; +import com.fr.third.org.hibernate.property.access.internal.AbstractFieldSerialForm; +import sun.misc.Unsafe; +import sun.reflect.FieldAccessor; +import sun.reflect.ReflectionFactory; + +import java.io.ObjectStreamException; +import java.io.Serializable; +import java.lang.reflect.Field; +import java.lang.reflect.Member; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.security.AccessController; +import java.util.Locale; +import java.util.Map; + +/** + * Field-based implementation of Getter + * + * @author Steve Ebersole + */ +public class UnsafeGetterFieldImpl implements Getter { + private final Class containerClass; + private final String propertyName; + private final Field field; + private final FieldAccessor accessor; + + static ReflectionFactory reflectionFactory = + AccessController.doPrivileged( + new sun.reflect.ReflectionFactory.GetReflectionFactoryAction()); + + + + + public UnsafeGetterFieldImpl(Class containerClass, String propertyName, Field field) { + this.containerClass = containerClass; + this.propertyName = propertyName; + this.field = field; + accessor = reflectionFactory.newFieldAccessor(field, false); + } + + @Override + public Object get(Object owner) { + if(accessor != null){ + return accessor.get(owner); + } + try { + return field.get( owner ); + } + catch (Exception e) { + throw new PropertyAccessException( + String.format( + Locale.ROOT, + "Error accessing field [%s] by reflection for persistent property [%s#%s] : %s", + field.toGenericString(), + containerClass.getName(), + propertyName, + owner + ), + e + ); + } + } + + @Override + public Object getForInsert(Object owner, Map mergeMap, SessionImplementor session) { + return get( owner ); + } + + @Override + public Class getReturnType() { + return field.getType(); + } + + @Override + public Member getMember() { + return field; + } + + @Override + public String getMethodName() { + return null; + } + + @Override + public Method getMethod() { + return null; + } + + private Object writeReplace() throws ObjectStreamException { + return new SerialForm( containerClass, propertyName, field ); + } + + private static class SerialForm extends AbstractFieldSerialForm implements Serializable { + private final Class containerClass; + private final String propertyName; + + private SerialForm(Class containerClass, String propertyName, Field field) { + super( field ); + this.containerClass = containerClass; + this.propertyName = propertyName; + } + + private Object readResolve() { + return new UnsafeGetterFieldImpl( containerClass, propertyName, resolveField() ); + } + } +} diff --git a/fine-hibernate/src/com/fr/third/org/hibernate/property/access/spi/UnsafeSetterMethodImpl.java b/fine-hibernate/src/com/fr/third/org/hibernate/property/access/spi/UnsafeSetterMethodImpl.java new file mode 100644 index 000000000..69f070d30 --- /dev/null +++ b/fine-hibernate/src/com/fr/third/org/hibernate/property/access/spi/UnsafeSetterMethodImpl.java @@ -0,0 +1,160 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package com.fr.third.org.hibernate.property.access.spi; + +import com.fr.third.org.hibernate.PropertyAccessException; +import com.fr.third.org.hibernate.PropertySetterAccessException; +import com.fr.third.org.hibernate.engine.spi.SessionFactoryImplementor; +import com.fr.third.org.hibernate.internal.CoreMessageLogger; + +import java.io.ObjectStreamException; +import java.io.Serializable; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import static com.fr.third.org.hibernate.internal.CoreLogging.messageLogger; + +/** + * @author Steve Ebersole + */ +public class UnsafeSetterMethodImpl implements Setter { + private static final CoreMessageLogger LOG = messageLogger( UnsafeSetterMethodImpl.class ); + + private final Class containerClass; + private final String propertyName; + private final Method setterMethod; + + private final boolean isPrimitive; + + public UnsafeSetterMethodImpl(Class containerClass, String propertyName, Method setterMethod) { + this.containerClass = containerClass; + this.propertyName = propertyName; + this.setterMethod = setterMethod; + + this.isPrimitive = setterMethod.getParameterTypes()[0].isPrimitive(); + } + + @Override + public void set(Object target, Object value, SessionFactoryImplementor factory) { + try { + setterMethod.invoke( target, value ); + } + catch (NullPointerException npe) { + if ( value == null && isPrimitive ) { + throw new PropertyAccessException( + npe, + "Null value was assigned to a property of primitive type", + true, + containerClass, + propertyName + ); + } + else { + throw new PropertyAccessException( + npe, + "NullPointerException occurred while calling", + true, + containerClass, + propertyName + ); + } + } + catch (InvocationTargetException ite) { + throw new PropertyAccessException( + ite, + "Exception occurred inside", + true, + containerClass, + propertyName + ); + } + catch (IllegalAccessException iae) { + throw new PropertyAccessException( + iae, + "IllegalAccessException occurred while calling", + true, + containerClass, + propertyName + ); + //cannot occur + } + catch (IllegalArgumentException iae) { + if ( value == null && isPrimitive ) { + throw new PropertyAccessException( + iae, + "Null value was assigned to a property of primitive type", + true, + containerClass, + propertyName + ); + } + else { + final Class expectedType = setterMethod.getParameterTypes()[0]; + LOG.illegalPropertySetterArgument( containerClass.getName(), propertyName ); + LOG.expectedType( expectedType.getName(), value == null ? null : value.getClass().getName() ); + throw new PropertySetterAccessException( + iae, + containerClass, + propertyName, + expectedType, + target, + value + ); + } + } + } + + @Override + public String getMethodName() { + return setterMethod.getName(); + } + + @Override + public Method getMethod() { + return setterMethod; + } + + private Object writeReplace() throws ObjectStreamException { + return new SerialForm( containerClass, propertyName, setterMethod ); + } + + private static class SerialForm implements Serializable { + private final Class containerClass; + private final String propertyName; + + private final Class declaringClass; + private final String methodName; + private final Class argumentType; + + private SerialForm(Class containerClass, String propertyName, Method method) { + this.containerClass = containerClass; + this.propertyName = propertyName; + this.declaringClass = method.getDeclaringClass(); + this.methodName = method.getName(); + this.argumentType = method.getParameterTypes()[0]; + } + + private Object readResolve() { + return new UnsafeSetterMethodImpl( containerClass, propertyName, resolveMethod() ); + } + + @SuppressWarnings("unchecked") + private Method resolveMethod() { + try { + final Method method = declaringClass.getDeclaredMethod( methodName, argumentType ); + method.setAccessible( true ); + return method; + } + catch (NoSuchMethodException e) { + throw new PropertyAccessSerializationException( + "Unable to resolve setter method on deserialization : " + declaringClass.getName() + "#" + + methodName + "(" + argumentType.getName() + ")" + ); + } + } + } +} From 99c5ad25e03bf14e877c17b157dfe52d78a32628 Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 5 Jul 2018 14:15:03 +0800 Subject: [PATCH 02/11] hibernate batch --- .../jdbc/internal/JdbcCoordinatorImpl.java | 65 +++++++++++++------ .../jdbc/internal/StatementPreparerImpl.java | 2 +- .../internal/PropertyAccessFieldImpl.java | 3 +- 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/fine-hibernate/src/com/fr/third/org/hibernate/engine/jdbc/internal/JdbcCoordinatorImpl.java b/fine-hibernate/src/com/fr/third/org/hibernate/engine/jdbc/internal/JdbcCoordinatorImpl.java index 25c84d2b9..c7538cd2e 100644 --- a/fine-hibernate/src/com/fr/third/org/hibernate/engine/jdbc/internal/JdbcCoordinatorImpl.java +++ b/fine-hibernate/src/com/fr/third/org/hibernate/engine/jdbc/internal/JdbcCoordinatorImpl.java @@ -18,6 +18,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import com.fr.third.org.jboss.logging.Logger; @@ -187,10 +188,14 @@ public class JdbcCoordinatorImpl implements JdbcCoordinator { LOG.tracev( "Closing JDBC container [{0}]", this ); Connection connection; try { - if ( currentBatch != null ) { - LOG.closingUnreleasedBatch(); - currentBatch.release(); + LOG.closingUnreleasedBatch(); + for(Map.Entry e : batchMap.entrySet()){ + e.getValue().release(); } +// if ( currentBatch != null ) { +// LOG.closingUnreleasedBatch(); +// currentBatch.release(); +// } cleanup(); } finally { @@ -199,35 +204,55 @@ public class JdbcCoordinatorImpl implements JdbcCoordinator { return connection; } + + private Map batchMap = new ConcurrentHashMap(); + @Override public Batch getBatch(BatchKey key) { - if ( currentBatch != null ) { - if ( currentBatch.getKey().equals( key ) ) { - return currentBatch; - } - else { - currentBatch.execute(); - currentBatch.release(); - } - } - currentBatch = batchBuilder().buildBatch( key, this ); - return currentBatch; + Batch batch = batchMap.get(key); + if(batch == null) { + batch = batchBuilder().buildBatch( key, this ); + batchMap.put(key, batch); + } + return batch; + +// +// if ( currentBatch != null ) { +// if ( currentBatch.getKey().equals( key ) ) { +// return currentBatch; +// } +// else { +// currentBatch.execute(); +// currentBatch.release(); +// } +// } +// currentBatch = batchBuilder().buildBatch( key, this ); +// return currentBatch; } @Override public void executeBatch() { - if ( currentBatch != null ) { - currentBatch.execute(); - // needed? - currentBatch.release(); + for(Map.Entry e : batchMap.entrySet()){ + e.getValue().execute(); + e.getValue().release(); } +// if ( currentBatch != null ) { +// currentBatch.execute(); +// // needed? +// currentBatch.release(); +// } } @Override public void abortBatch() { - if ( currentBatch != null ) { - currentBatch.release(); + + for(Map.Entry e : batchMap.entrySet()){ + e.getValue().release(); } +// if ( currentBatch != null ) { +// currentBatch.release(); +// } + } private transient StatementPreparer statementPreparer; diff --git a/fine-hibernate/src/com/fr/third/org/hibernate/engine/jdbc/internal/StatementPreparerImpl.java b/fine-hibernate/src/com/fr/third/org/hibernate/engine/jdbc/internal/StatementPreparerImpl.java index f15a71410..29c739ccc 100644 --- a/fine-hibernate/src/com/fr/third/org/hibernate/engine/jdbc/internal/StatementPreparerImpl.java +++ b/fine-hibernate/src/com/fr/third/org/hibernate/engine/jdbc/internal/StatementPreparerImpl.java @@ -74,7 +74,7 @@ class StatementPreparerImpl implements StatementPreparer { @Override public PreparedStatement prepareStatement(String sql, final boolean isCallable) { - jdbcCoordinator.executeBatch(); + //jdbcCoordinator.executeBatch(); return buildPreparedStatementPreparationTemplate( sql, isCallable ).prepareStatement(); } diff --git a/fine-hibernate/src/com/fr/third/org/hibernate/property/access/internal/PropertyAccessFieldImpl.java b/fine-hibernate/src/com/fr/third/org/hibernate/property/access/internal/PropertyAccessFieldImpl.java index 5bf9486ee..a72bedd28 100644 --- a/fine-hibernate/src/com/fr/third/org/hibernate/property/access/internal/PropertyAccessFieldImpl.java +++ b/fine-hibernate/src/com/fr/third/org/hibernate/property/access/internal/PropertyAccessFieldImpl.java @@ -15,6 +15,7 @@ import com.fr.third.org.hibernate.property.access.spi.PropertyAccess; import com.fr.third.org.hibernate.property.access.spi.PropertyAccessStrategy; import com.fr.third.org.hibernate.property.access.spi.Setter; import com.fr.third.org.hibernate.property.access.spi.SetterFieldImpl; +import com.fr.third.org.hibernate.property.access.spi.UnsafeGetterFieldImpl; /** * @author Steve Ebersole @@ -32,7 +33,7 @@ public class PropertyAccessFieldImpl implements PropertyAccess { this.strategy = strategy; final Field field = ReflectHelper.findField( containerJavaType, propertyName ); - this.getter = new GetterFieldImpl( containerJavaType, propertyName, field ); + this.getter = new UnsafeGetterFieldImpl( containerJavaType, propertyName, field ); this.setter = new SetterFieldImpl( containerJavaType, propertyName, field ); } From b6d8a4cfcef84a3ec4bc1086192f72697bf63070 Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 10 Jul 2018 11:58:41 +0800 Subject: [PATCH 03/11] build --- build.third_step0.gradle | 44 ++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/build.third_step0.gradle b/build.third_step0.gradle index 053396940..1fbfd6993 100644 --- a/build.third_step0.gradle +++ b/build.third_step0.gradle @@ -1,15 +1,22 @@ apply plugin: 'java' +apply plugin: 'maven-publish' +def classesDir='build/classes/main' tasks.withType(JavaCompile){ options.encoding = 'UTF-8' - destinationDir = file('build/classes/main') + destinationDir = file(classesDir) +} + +configurations.all { + resolutionStrategy.cacheChangingModulesFor 0, 'seconds' } //指定构建的jdk版本 sourceCompatibility=1.5 -def jarname="fine-third-10.0.jar" -def classesDir='build/classes/main' + + //解压lib下的jar到classes文件夹 + jar{ - baseName="fine-third-10.0" + baseName="fine-third-${version}" } repositories { @@ -28,10 +35,13 @@ sourceSets{ } //获取什么分支名 -FileTree files =fileTree(dir:'./',include:'build.*.gradle') +FileTree files =fileTree(dir:'./',include:'build*.gradle') def buildDir=files[0].path.substring(0,files[0].path.lastIndexOf (java.io.File.separator)) def branchName=buildDir.substring(buildDir.lastIndexOf (java.io.File.separator)+1) +version='10.0' def srcDir="." +def maven_version="${version}-RELEASE-SNAPSHOT" +def jar_version = version configurations { thirdjar @@ -39,12 +49,34 @@ configurations { } dependencies { - thirdjar "com.fr.third:fine-third:10.0-FEATURE-SNAPSHOT" + thirdjar "com.fr.third:fine-third:10.0-RELEASE-SNAPSHOT" sigar "com.fr.third:sigar:1.6.0" testCompile 'junit:junit:4.12' } +publishing { + publications { + third(MavenPublication) { + groupId "com.fr.third" + artifactId "fine-third" + version maven_version + from components.java + } + } + repositories { + maven { + + url "http://mvn.finedevelop.com/repository/fanruan/" + credentials { + username = findProperty("NEXUS_USERNAME") + password = findProperty("NEXUS_PASSWORD") + } + + } + } +} + task unpack(type:Copy) { delete classesDir destinationDir=file(classesDir) From fa7606657551730bdfa943d2140584efbf430ab5 Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 10 Jul 2018 14:02:53 +0800 Subject: [PATCH 04/11] 1 --- build.third_step0.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.third_step0.gradle b/build.third_step0.gradle index 1fbfd6993..ed520e12b 100644 --- a/build.third_step0.gradle +++ b/build.third_step0.gradle @@ -14,7 +14,7 @@ sourceCompatibility=1.5 //解压lib下的jar到classes文件夹 - +version='10.0' jar{ baseName="fine-third-${version}" } @@ -38,7 +38,7 @@ sourceSets{ FileTree files =fileTree(dir:'./',include:'build*.gradle') def buildDir=files[0].path.substring(0,files[0].path.lastIndexOf (java.io.File.separator)) def branchName=buildDir.substring(buildDir.lastIndexOf (java.io.File.separator)+1) -version='10.0' + def srcDir="." def maven_version="${version}-RELEASE-SNAPSHOT" def jar_version = version From d1a07d5d0aded7ab0d855d5eba6acdb5c3a4009e Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 10 Jul 2018 14:48:28 +0800 Subject: [PATCH 05/11] =?UTF-8?q?sb=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.third_step0.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.third_step0.gradle b/build.third_step0.gradle index ed520e12b..256b434ab 100644 --- a/build.third_step0.gradle +++ b/build.third_step0.gradle @@ -16,7 +16,7 @@ sourceCompatibility=1.5 //解压lib下的jar到classes文件夹 version='10.0' jar{ - baseName="fine-third-${version}" + baseName="fine-third" } repositories { From 04a12528d0bce20e2e5581c73ad31c8ae80e01e3 Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 10 Jul 2018 14:49:39 +0800 Subject: [PATCH 06/11] =?UTF-8?q?SB=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.third_step1.gradle | 2 +- build.third_step2.gradle | 2 +- build.third_step3.gradle | 2 +- build.third_step4.gradle | 2 +- build.third_step5.gradle | 2 +- build.third_step6.gradle | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/build.third_step1.gradle b/build.third_step1.gradle index 205f72c16..1e65d5896 100644 --- a/build.third_step1.gradle +++ b/build.third_step1.gradle @@ -56,7 +56,7 @@ dependencies{ compile fileTree(dir:"${srcDir}/fine-poi/lib",include:'**/*.jar') compile fileTree(dir:"${srcDir}/fine-quartz/lib",include:'**/*.jar') compile fileTree(dir:"${srcDir}/build/libs/",include:'**/*.jar') - compile "com.fr.third:fine-third:10.0-FEATURE-SNAPSHOT" + compile "com.fr.third:fine-third:10.0-RELEASE-SNAPSHOT" compile fileTree(dir:"../../finereport-lib-other/${branchName}",include:'**/*.jar') testCompile 'junit:junit:4.12' } diff --git a/build.third_step2.gradle b/build.third_step2.gradle index e71ab7dc9..3f5282d3c 100644 --- a/build.third_step2.gradle +++ b/build.third_step2.gradle @@ -45,7 +45,7 @@ dependencies{ compile fileTree(dir:"${srcDir}/fine-aspectj/lib",include:'**/*.jar') compile fileTree(dir:"${srcDir}/fine-spring/lib",include:'**/*.jar') compile fileTree(dir:"${srcDir}/build/libs/",include:'**/*.jar') - compile "com.fr.third:fine-third:10.0-FEATURE-SNAPSHOT" + compile "com.fr.third:fine-third:10.0-RELEASE-SNAPSHOT" compile fileTree(dir:"../../finereport-lib-other/${branchName}",include:'**/*.jar') testCompile 'junit:junit:4.12' } diff --git a/build.third_step3.gradle b/build.third_step3.gradle index 0bf16d70f..e30a07a11 100644 --- a/build.third_step3.gradle +++ b/build.third_step3.gradle @@ -46,7 +46,7 @@ def branchName=buildDir.substring(buildDir.lastIndexOf (java.io.File.separator)+ dependencies{ compile fileTree(dir:"${srcDir}/fine-jboss-logging/lib",include:'**/*.jar') compile fileTree(dir:"${srcDir}/build/libs/",include:'**/*.jar') - compile "com.fr.third:fine-third:10.0-FEATURE-SNAPSHOT" + compile "com.fr.third:fine-third:10.0-RELEASE-SNAPSHOT" compile fileTree(dir:"../../finereport-lib-other/${branchName}",include:'**/*.jar') testCompile 'junit:junit:4.12' } diff --git a/build.third_step4.gradle b/build.third_step4.gradle index f75a4f847..2542e2eb3 100644 --- a/build.third_step4.gradle +++ b/build.third_step4.gradle @@ -45,7 +45,7 @@ def branchName=buildDir.substring(buildDir.lastIndexOf (java.io.File.separator)+ dependencies{ compile fileTree(dir:"${srcDir}/fine-hibernate/lib",include:'**/*.jar') compile fileTree(dir:"${srcDir}/build/libs/",include:'**/*.jar') - compile "com.fr.third:fine-third:10.0-FEATURE-SNAPSHOT" + compile "com.fr.third:fine-third:10.0-RELEASE-SNAPSHOT" compile fileTree(dir:"../../finereport-lib-other/${branchName}",include:'**/*.jar') testCompile 'junit:junit:4.12' } diff --git a/build.third_step5.gradle b/build.third_step5.gradle index 67933f2b3..324c9a8c4 100644 --- a/build.third_step5.gradle +++ b/build.third_step5.gradle @@ -44,7 +44,7 @@ dependencies{ compile fileTree(dir:"${srcDir}/fine-druid/lib",include:'ojdbc7-12.1.0.jar') compile fileTree(dir:"${srcDir}/fine-druid/lib",include:'**/*.jar') compile fileTree(dir:"${srcDir}/build/libs/",include:'**/*.jar') - compile "com.fr.third:fine-third:10.0-FEATURE-SNAPSHOT" + compile "com.fr.third:fine-third:10.0-RELEASE-SNAPSHOT" compile fileTree(dir:"../../finereport-lib-other/${branchName}",include:'**/*.jar') testCompile 'junit:junit:4.12' } diff --git a/build.third_step6.gradle b/build.third_step6.gradle index 048ef33b2..46f7ebe3b 100644 --- a/build.third_step6.gradle +++ b/build.third_step6.gradle @@ -66,7 +66,7 @@ dependencies{ compile fileTree(dir:"${srcDir}/fine-redisson/lib",include:'**/*.jar') compile fileTree(dir:"${srcDir}/fine-socketio/lib",include:'**/*.jar') compile fileTree(dir:"${srcDir}/build/libs/",include:'**/*.jar') - compile "com.fr.third:fine-third:10.0-FEATURE-SNAPSHOT" + compile "com.fr.third:fine-third:10.0-RELEASE-SNAPSHOT" compile fileTree(dir:"../../finereport-lib-other/${branchName}",include:'**/*.jar') testCompile 'junit:junit:4.12' } From 29182f22d2aaa714fb1516949187d3e72e4fd242 Mon Sep 17 00:00:00 2001 From: loy Date: Mon, 16 Jul 2018 16:28:48 +0800 Subject: [PATCH 07/11] =?UTF-8?q?CORE-110=20=E8=B0=83=E6=95=B4hibernate?= =?UTF-8?q?=E6=BA=90=E7=A0=81=EF=BC=8C=E6=96=B0=E5=A2=9E=E5=8D=95=E7=8B=AC?= =?UTF-8?q?=E9=92=88=E5=AF=B9session=E8=AE=BE=E7=BD=AE=E7=9A=84batchSize?= =?UTF-8?q?=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../engine/jdbc/batch/internal/BatchBuilderImpl.java | 9 ++++++--- .../org/hibernate/internal/AbstractSessionImpl.java | 11 +++++++++++ .../hibernate/resource/jdbc/spi/JdbcSessionOwner.java | 4 ++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/fine-hibernate/src/com/fr/third/org/hibernate/engine/jdbc/batch/internal/BatchBuilderImpl.java b/fine-hibernate/src/com/fr/third/org/hibernate/engine/jdbc/batch/internal/BatchBuilderImpl.java index f8714c0b9..44ad721ae 100644 --- a/fine-hibernate/src/com/fr/third/org/hibernate/engine/jdbc/batch/internal/BatchBuilderImpl.java +++ b/fine-hibernate/src/com/fr/third/org/hibernate/engine/jdbc/batch/internal/BatchBuilderImpl.java @@ -61,9 +61,12 @@ public class BatchBuilderImpl implements BatchBuilder, Configurable, Manageable, @Override public Batch buildBatch(BatchKey key, JdbcCoordinator jdbcCoordinator) { - LOG.tracef( "Building batch [size=%s]", size ); - return size > 1 - ? new BatchingBatch( key, jdbcCoordinator, size ) + int batchSize = jdbcCoordinator.getJdbcSessionOwner().getJdbcBatchSize() > 1 + ? jdbcCoordinator.getJdbcSessionOwner().getJdbcBatchSize() + : size; + LOG.tracef( "Building batch [size=%s]", batchSize ); + return batchSize > 1 + ? new BatchingBatch( key, jdbcCoordinator, batchSize ) : new NonBatchingBatch( key, jdbcCoordinator ); } diff --git a/fine-hibernate/src/com/fr/third/org/hibernate/internal/AbstractSessionImpl.java b/fine-hibernate/src/com/fr/third/org/hibernate/internal/AbstractSessionImpl.java index 50101c261..c55ccaddf 100755 --- a/fine-hibernate/src/com/fr/third/org/hibernate/internal/AbstractSessionImpl.java +++ b/fine-hibernate/src/com/fr/third/org/hibernate/internal/AbstractSessionImpl.java @@ -71,6 +71,7 @@ public abstract class AbstractSessionImpl protected transient SessionFactoryImpl factory; private final String tenantIdentifier; private boolean closed; + private int jdbcBatchSize; protected transient Transaction currentHibernateTransaction; protected transient WrapperOptionsImpl wrapperOptions; @@ -609,4 +610,14 @@ public abstract class AbstractSessionImpl return wrapperOptions; } + + @Override + public int getJdbcBatchSize() { + return jdbcBatchSize; + } + + @Override + public void setJdbcBatchSize(int jdbcBatchSize) { + this.jdbcBatchSize = jdbcBatchSize; + } } diff --git a/fine-hibernate/src/com/fr/third/org/hibernate/resource/jdbc/spi/JdbcSessionOwner.java b/fine-hibernate/src/com/fr/third/org/hibernate/resource/jdbc/spi/JdbcSessionOwner.java index b8fa0acd9..0a45b7592 100644 --- a/fine-hibernate/src/com/fr/third/org/hibernate/resource/jdbc/spi/JdbcSessionOwner.java +++ b/fine-hibernate/src/com/fr/third/org/hibernate/resource/jdbc/spi/JdbcSessionOwner.java @@ -43,4 +43,8 @@ public interface JdbcSessionOwner { public void afterTransactionCompletion(boolean successful, boolean delayed); public void flushBeforeTransactionCompletion(); + + public int getJdbcBatchSize(); + + public void setJdbcBatchSize(int jdbcBatchSize); } From 914346455a09c79111f238a6b923ab4f641fb906 Mon Sep 17 00:00:00 2001 From: abel Date: Mon, 6 Aug 2018 10:41:13 +0800 Subject: [PATCH 08/11] =?UTF-8?q?druid=E6=96=AD=E7=BA=BF=E9=87=8D=E8=BF=9E?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/druid/pool/DruidDataSource.java | 218 +++++++++++------- 1 file changed, 132 insertions(+), 86 deletions(-) diff --git a/fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java b/fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java index 341aeb540..dc0c3b042 100644 --- a/fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java +++ b/fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java @@ -35,6 +35,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -100,78 +101,78 @@ import com.fr.third.alibaba.druid.wall.WallProviderStatValue; */ public class DruidDataSource extends DruidAbstractDataSource implements DruidDataSourceMBean, ManagedDataSource, Referenceable, Closeable, Cloneable, ConnectionPoolDataSource, MBeanRegistration { - private final static Log LOG = LogFactory.getLog(DruidDataSource.class); + private final static Log LOG = LogFactory.getLog(DruidDataSource.class); - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; // stats - private final AtomicLong recycleErrorCount = new AtomicLong(); - private long connectCount = 0L; - private long closeCount = 0L; - private final AtomicLong connectErrorCount = new AtomicLong(); - private long recycleCount = 0L; - private long removeAbandonedCount = 0L; - private long notEmptyWaitCount = 0L; - private long notEmptySignalCount = 0L; - private long notEmptyWaitNanos = 0L; - - private int keepAliveCheckCount = 0; - - private int activePeak = 0; - private long activePeakTime = 0; - private int poolingPeak = 0; - private long poolingPeakTime = 0; + private final AtomicLong recycleErrorCount = new AtomicLong(); + private long connectCount = 0L; + private long closeCount = 0L; + private final AtomicLong connectErrorCount = new AtomicLong(); + private long recycleCount = 0L; + private long removeAbandonedCount = 0L; + private long notEmptyWaitCount = 0L; + private long notEmptySignalCount = 0L; + private long notEmptyWaitNanos = 0L; + + private int keepAliveCheckCount = 0; + + private int activePeak = 0; + private long activePeakTime = 0; + private int poolingPeak = 0; + private long poolingPeakTime = 0; // store private volatile DruidConnectionHolder[] connections; - private int poolingCount = 0; - private int activeCount = 0; - private long discardCount = 0; - private int notEmptyWaitThreadCount = 0; - private int notEmptyWaitThreadPeak = 0; + private int poolingCount = 0; + private int activeCount = 0; + private long discardCount = 0; + private int notEmptyWaitThreadCount = 0; + private int notEmptyWaitThreadPeak = 0; // - private DruidConnectionHolder[] evictConnections; - private DruidConnectionHolder[] keepAliveConnections; + private DruidConnectionHolder[] evictConnections; + private DruidConnectionHolder[] keepAliveConnections; // threads - private ScheduledFuture destroySchedulerFuture; - private DestroyTask destroyTask; + private ScheduledFuture destroySchedulerFuture; + private DestroyTask destroyTask; - private CreateConnectionThread createConnectionThread; - private DestroyConnectionThread destroyConnectionThread; - private LogStatsThread logStatsThread; - private int createTaskCount; + private CreateConnectionThread createConnectionThread; + private DestroyConnectionThread destroyConnectionThread; + private LogStatsThread logStatsThread; + private int createTaskCount; - private final CountDownLatch initedLatch = new CountDownLatch(2); + private CountDownLatch initedLatch = new CountDownLatch(2); - private volatile boolean enable = true; + private volatile boolean enable = true; - private boolean resetStatEnable = true; - private final AtomicLong resetCount = new AtomicLong(); + private boolean resetStatEnable = true; + private final AtomicLong resetCount = new AtomicLong(); - private String initStackTrace; + private String initStackTrace; - private volatile boolean closed = false; - private long closeTimeMillis = -1L; + private volatile boolean closed = false; + private long closeTimeMillis = -1L; - protected JdbcDataSourceStat dataSourceStat; + protected JdbcDataSourceStat dataSourceStat; - private boolean useGlobalDataSourceStat = false; + private boolean useGlobalDataSourceStat = false; - private boolean mbeanRegistered = false; + private boolean mbeanRegistered = false; - public static ThreadLocal waitNanosLocal = new ThreadLocal(); + public static ThreadLocal waitNanosLocal = new ThreadLocal(); - private boolean logDifferentThread = true; + private boolean logDifferentThread = true; - private volatile boolean keepAlive = false; + private volatile boolean keepAlive = false; - public DruidDataSource(){ + public DruidDataSource() { this(false); } - public DruidDataSource(boolean fairLock){ + public DruidDataSource(boolean fairLock) { super(fairLock); configFromPropety(System.getProperties()); @@ -602,6 +603,30 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat this.connectProperties = properties; } + private synchronized void createThreadChange() { + String threadName = "Druid-ConnectionPool-Create-" + System.identityHashCode(this) + this.getUrl(); + createConnectionThread = new CreateConnectionThread(threadName); + createConnectionThread.setStarted(false); + String destroyName = "Druid-ConnectionPool-Destroy-" + System.identityHashCode(this) + this.getUrl(); + destroyConnectionThread = new DestroyConnectionThread(destroyName); + destroyConnectionThread.setStarted(false); + initedLatch = new CountDownLatch(2); + } + + private void checkThread() throws SQLException { + if (!createConnectionThread.isStarted() && !destroyConnectionThread.isStarted()) { + createConnectionThread.setStarted(true); + createConnectionThread.start(); + destroyConnectionThread.setStarted(true); + destroyConnectionThread.start(); + try { + initedLatch.await(); + } catch (InterruptedException e) { + throw new SQLException(e.getMessage(), e); + } + } + } + public void init() throws SQLException { if (inited) { return; @@ -645,7 +670,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } if (JdbcConstants.MYSQL.equals(this.dbType) || // - JdbcConstants.MARIADB.equals(this.dbType)) { + JdbcConstants.MARIADB.equals(this.dbType)) { boolean cacheServerConfigurationSet = false; if (this.connectProperties.containsKey("cacheServerConfiguration")) { cacheServerConfigurationSet = true; @@ -805,7 +830,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat period = 1000; } destroySchedulerFuture = destroyScheduler.scheduleAtFixedRate(destroyTask, period, period, - TimeUnit.MILLISECONDS); + TimeUnit.MILLISECONDS); initedLatch.countDown(); return; } @@ -817,7 +842,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat protected void createAndStartCreatorThread() { if (createScheduler == null) { - String threadName = "Druid-ConnectionPool-Create-" + System.identityHashCode(this); + String threadName = "Druid-ConnectionPool-Create-" + System.identityHashCode(this) + this.getUrl(); createConnectionThread = new CreateConnectionThread(threadName); createConnectionThread.start(); return; @@ -828,7 +853,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat /** * load filters from SPI ServiceLoader - * + * * @see ServiceLoader */ private void initFromSPIServiceLoader() { @@ -873,7 +898,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat /** * 会去重复 - * + * * @param filter */ private void addFilter(Filter filter) { @@ -928,7 +953,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat if (driver.getMajorVersion() < 10) { throw new SQLException("not support oracle driver " + driver.getMajorVersion() + "." - + driver.getMinorVersion()); + + driver.getMinorVersion()); } if (driver.getMajorVersion() == 10 && isUseOracleImplicitCache()) { @@ -965,7 +990,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat if (query instanceof SQLSelectQueryBlock) { if (((SQLSelectQueryBlock) query).getFrom() == null) { LOG.error("invalid oracle validationQuery. " + validationQuery + ", may should be : " + validationQuery - + " FROM DUAL"); + + " FROM DUAL"); } } } @@ -994,7 +1019,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat if (query instanceof SQLSelectQueryBlock) { if (((SQLSelectQueryBlock) query).getFrom() == null) { LOG.error("invalid db2 validationQuery. " + validationQuery + ", may should be : " + validationQuery - + " FROM SYSDUMMY"); + + " FROM SYSDUMMY"); } } } @@ -1002,14 +1027,14 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat private void initValidConnectionChecker() { String realDriverClassName = driver.getClass().getName(); if (realDriverClassName.equals(JdbcConstants.MYSQL_DRIVER) // - || realDriverClassName.equals(JdbcConstants.MYSQL_DRIVER_6)) { + || realDriverClassName.equals(JdbcConstants.MYSQL_DRIVER_6)) { this.validConnectionChecker = new MySqlValidConnectionChecker(); } else if (realDriverClassName.equals(JdbcConstants.ORACLE_DRIVER) || realDriverClassName.equals(JdbcConstants.ORACLE_DRIVER2)) { this.validConnectionChecker = new OracleValidConnectionChecker(); } else if (realDriverClassName.equals(JdbcConstants.SQL_SERVER_DRIVER) - || realDriverClassName.equals(JdbcConstants.SQL_SERVER_DRIVER_SQLJDBC4) - || realDriverClassName.equals(JdbcConstants.SQL_SERVER_DRIVER_JTDS)) { + || realDriverClassName.equals(JdbcConstants.SQL_SERVER_DRIVER_SQLJDBC4) + || realDriverClassName.equals(JdbcConstants.SQL_SERVER_DRIVER_JTDS)) { this.validConnectionChecker = new MSSQLValidConnectionChecker(); } else if (realDriverClassName.equals(JdbcConstants.POSTGRESQL_DRIVER)) { this.validConnectionChecker = new PGValidConnectionChecker(); @@ -1027,7 +1052,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat String realDriverClassName = driver.getClass().getName(); if (realDriverClassName.equals(JdbcConstants.MYSQL_DRIVER) // - || realDriverClassName.equals(JdbcConstants.MYSQL_DRIVER_6)) { + || realDriverClassName.equals(JdbcConstants.MYSQL_DRIVER_6)) { this.exceptionSorter = new MySqlExceptionSorter(); } else if (realDriverClassName.equals(JdbcConstants.ORACLE_DRIVER) || realDriverClassName.equals(JdbcConstants.ORACLE_DRIVER2)) { @@ -1055,7 +1080,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat public DruidPooledConnection getConnection(long maxWaitMillis) throws SQLException { init(); - + checkThread(); if (filters.size() > 0) { FilterChainImpl filterChain = new FilterChainImpl(this); return filterChain.dataSource_connect(this, maxWaitMillis); @@ -1076,7 +1101,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat public DruidPooledConnection getConnectionDirect(long maxWaitMillis) throws SQLException { int notFullTimeoutRetryCnt = 0; - for (;;) { + for (; ; ) { // handle notFullTimeoutRetry DruidPooledConnection poolableConnection; try { @@ -1157,7 +1182,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat /** * 抛弃连接,不进行回收,而是抛弃 - * + * * @param realConnection */ public void discardConnection(Connection realConnection) { @@ -1203,7 +1228,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat if (notEmptyWaitThreadCount >= maxWaitThreadCount) { connectErrorCount.incrementAndGet(); throw new SQLException("maxWaitThreadCount " + maxWaitThreadCount + ", current wait Thread count " - + lock.getQueueLength()); + + lock.getQueueLength()); } } @@ -1235,7 +1260,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat if (holder == null) { Throwable throwable = this.createError; if (throwable != null) { - if(throwable instanceof SQLException){ + if (throwable instanceof SQLException) { throw (SQLException) throwable; } throw new GetConnectionTimeoutException(throwable.getMessage(), throwable); @@ -1249,7 +1274,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat .append(", maxActive " + maxActive)// ; JdbcDataSourceStat sourceStat = this.getDataSourceStat(); - if(sourceStat != null) { + if (sourceStat != null) { List sqlList = sourceStat.getRuningSqlList(); for (int i = 0; i < sqlList.size(); ++i) { if (i != 0) { @@ -1344,9 +1369,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } if (logDifferentThread // - && (!isAsyncCloseConnectionEnable()) // - && pooledConnection.getOwnerThread() != Thread.currentThread()// - ) { + && (!isAsyncCloseConnectionEnable()) // + && pooledConnection.getOwnerThread() != Thread.currentThread()// + ) { LOG.warn("get/close not same thread"); } @@ -1543,7 +1568,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat @Override public Object run() { ObjectName objectName = DruidDataSourceStatManager.addDataSource(DruidDataSource.this, - DruidDataSource.this.name); + DruidDataSource.this.name); DruidDataSource.this.setObjectName(objectName); DruidDataSource.this.mbeanRegistered = true; @@ -1633,7 +1658,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat private DruidConnectionHolder pollLast(long nanos) throws InterruptedException, SQLException { long estimate = nanos; - for (;;) { + for (; ; ) { if (poolingCount == 0) { emptySignal(); // send signal to CreateThread create connection @@ -1654,8 +1679,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat try { long startEstimate = estimate; estimate = notEmpty.awaitNanos(estimate); // signal by - // recycle or - // creator + // recycle or + // creator notEmptyWaitCount++; notEmptyWaitNanos += (startEstimate - estimate); @@ -1926,7 +1951,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat createTaskCount--; if (poolingCount + createTaskCount < notEmptyWaitThreadCount // - && activeCount + poolingCount + createTaskCount < maxActive) { + && activeCount + poolingCount + createTaskCount < maxActive) { emptySignal(); } } @@ -1946,7 +1971,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } private void runInternal() { - for (;;) { + for (; ; ) { // addLast lock.lock(); @@ -2043,8 +2068,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } public class CreateConnectionThread extends Thread { + private volatile boolean started = true; - public CreateConnectionThread(String name){ + public CreateConnectionThread(String name) { super(name); this.setDaemon(true); } @@ -2054,7 +2080,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat long lastDiscardCount = 0; int errorCount = 0; - for (;;) { + for (; ; ) { // addLast try { lock.lockInterruptibly(); @@ -2102,7 +2128,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat setFailContinuous(false); } catch (SQLException e) { LOG.error("create connection error, url: " + jdbcUrl + ", errorCode " + e.getErrorCode() - + ", state " + e.getSQLState(), e); + + ", state " + e.getSQLState(), e); errorCount++; if (errorCount > connectionErrorRetryAttempts && timeBetweenConnectErrorMillis > 0) { @@ -2126,6 +2152,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } catch (InterruptedException interruptEx) { break; } + DruidDataSource.this.createThreadChange(); + break; } } catch (RuntimeException e) { LOG.error("create connection error", e); @@ -2150,11 +2178,20 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat errorCount = 0; // reset errorCount } } + + public boolean isStarted() { + return started; + } + + public void setStarted(boolean started) { + this.started = started; + } } public class DestroyConnectionThread extends Thread { + private volatile boolean started = true; - public DestroyConnectionThread(String name){ + public DestroyConnectionThread(String name) { super(name); this.setDaemon(true); } @@ -2162,7 +2199,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat public void run() { initedLatch.countDown(); - for (;;) { + for (; ; ) { // 从前面开始删除 try { if (closed) { @@ -2186,6 +2223,13 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } } + public boolean isStarted() { + return started; + } + + public void setStarted(boolean started) { + this.started = started; + } } public class DestroyTask implements Runnable { @@ -2203,14 +2247,14 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat public class LogStatsThread extends Thread { - public LogStatsThread(String name){ + public LogStatsThread(String name) { super(name); this.setDaemon(true); } public void run() { try { - for (;;) { + for (; ; ) { try { logStats(); } catch (Exception e) { @@ -2236,7 +2280,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat try { Iterator iter = activeConnections.keySet().iterator(); - for (; iter.hasNext();) { + for (; iter.hasNext(); ) { DruidPooledConnection pooledConnection = iter.next(); if (pooledConnection.isRunning()) { @@ -2288,7 +2332,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } buf.append("ownerThread current state is " + pooledConnection.getOwnerThread().getState() - + ", current stackTrace\n"); + + ", current stackTrace\n"); trace = pooledConnection.getOwnerThread().getStackTrace(); for (int i = 0; i < trace.length; i++) { buf.append("\tat "); @@ -2304,7 +2348,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat return removeCount; } - /** Instance key */ + /** + * Instance key + */ protected String instanceKey = null; public Reference getReference() throws NamingException { @@ -2938,7 +2984,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } if (this.statLogger != null - && (this.statLogger.getClass() == iface || DruidDataSourceStatLogger.class == iface)) { + && (this.statLogger.getClass() == iface || DruidDataSourceStatLogger.class == iface)) { return true; } @@ -2954,7 +3000,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } if (this.statLogger != null - && (this.statLogger.getClass() == iface || DruidDataSourceStatLogger.class == iface)) { + && (this.statLogger.getClass() == iface || DruidDataSourceStatLogger.class == iface)) { return (T) statLogger; } @@ -2998,7 +3044,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat } int fillCount = 0; - for (;;) { + for (; ; ) { try { lock.lockInterruptibly(); } catch (InterruptedException e) { From 745fe8584ea0b17d318370edb66b67e91142f8fe Mon Sep 17 00:00:00 2001 From: abel Date: Mon, 6 Aug 2018 10:43:54 +0800 Subject: [PATCH 09/11] fix import --- .../src/com/fr/third/alibaba/druid/pool/DruidDataSource.java | 1 - 1 file changed, 1 deletion(-) diff --git a/fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java b/fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java index dc0c3b042..da0f2bed4 100644 --- a/fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java +++ b/fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java @@ -35,7 +35,6 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; -import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; From 68085657b2a1158c5e8b21e88ad0a5c814f48eb1 Mon Sep 17 00:00:00 2001 From: abel Date: Mon, 6 Aug 2018 11:17:31 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E5=88=87=E6=8D=A2?= =?UTF-8?q?=EF=BC=8C=E5=A4=9A=E7=BA=BF=E7=A8=8B=E5=90=8C=E6=AD=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/druid/pool/DruidDataSource.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java b/fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java index da0f2bed4..567ccd5c8 100644 --- a/fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java +++ b/fine-druid/src/com/fr/third/alibaba/druid/pool/DruidDataSource.java @@ -614,14 +614,18 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat private void checkThread() throws SQLException { if (!createConnectionThread.isStarted() && !destroyConnectionThread.isStarted()) { - createConnectionThread.setStarted(true); - createConnectionThread.start(); - destroyConnectionThread.setStarted(true); - destroyConnectionThread.start(); - try { - initedLatch.await(); - } catch (InterruptedException e) { - throw new SQLException(e.getMessage(), e); + synchronized (this) {//线程安全问题,加个双检锁 + if (!createConnectionThread.isStarted() && !destroyConnectionThread.isStarted()) { + createConnectionThread.setStarted(true); + createConnectionThread.start(); + destroyConnectionThread.setStarted(true); + destroyConnectionThread.start(); + try { + initedLatch.await(); + } catch (InterruptedException e) { + throw new SQLException(e.getMessage(), e); + } + } } } } From a57abbf7fd6f307c57f68457b9e469ace4c417d0 Mon Sep 17 00:00:00 2001 From: anchore Date: Tue, 7 Aug 2018 13:33:14 +0800 Subject: [PATCH 11/11] spring.schemas edited online with Bitbucket --- fine-spring/resources/META-INF/spring.schemas | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/fine-spring/resources/META-INF/spring.schemas b/fine-spring/resources/META-INF/spring.schemas index 3d31c4400..7f5a7575b 100644 --- a/fine-spring/resources/META-INF/spring.schemas +++ b/fine-spring/resources/META-INF/spring.schemas @@ -33,3 +33,10 @@ http\://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd=com/fr/third/sprin http\://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd=com/fr/third/springframework/web/servlet/config/spring-mvc-4.0.xsd http\://www.springframework.org/schema/mvc/spring-mvc.xsd=com/fr/third/springframework/web/servlet/config/spring-mvc-4.0.xsd +http\://www.springframework.org/schema/beans/spring-beans-2.0.xsd=com/fr/third/springframework/beans/factory/xml/spring-beans-2.0.xsd +http\://www.springframework.org/schema/beans/spring-beans-2.5.xsd=com/fr/third/springframework/beans/factory/xml/spring-beans-2.5.xsd +http\://www.springframework.org/schema/beans/spring-beans-3.0.xsd=com/fr/third/springframework/beans/factory/xml/spring-beans-3.0.xsd +http\://www.springframework.org/schema/beans/spring-beans-3.1.xsd=com/fr/third/springframework/beans/factory/xml/spring-beans-3.1.xsd +http\://www.springframework.org/schema/beans/spring-beans-3.2.xsd=com/fr/third/springframework/beans/factory/xml/spring-beans-3.2.xsd +http\://www.springframework.org/schema/beans/spring-beans-4.0.xsd=com/fr/third/springframework/beans/factory/xml/spring-beans-4.0.xsd +http\://www.springframework.org/schema/beans/spring-beans.xsd=com/fr/third/springframework/beans/factory/xml/spring-beans-4.0.xsd