Dec 12, 2015 4:46:04 PM com.alibaba.druid.pool.DruidDataSource error SEVERE: discard connection com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communications link failure during commit(). Transaction resolution unknown. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.Util.getInstance(Util.java:381) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926) at com.mysql.jdbc.ConnectionImpl.commit(ConnectionImpl.java:1552) at com.alibaba.druid.filter.FilterChainImpl.connection_commit(FilterChainImpl.java:180) at com.alibaba.druid.filter.stat.StatFilter.connection_commit(StatFilter.java:260) at com.alibaba.druid.filter.FilterChainImpl.connection_commit(FilterChainImpl.java:176) at com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl.commit(ConnectionProxyImpl.java:123) at com.alibaba.druid.pool.DruidPooledConnection.commit(DruidPooledConnection.java:718) at org.springframework.jdbc.datasource.DataSourceTransactionManager.doCommit(DataSourceTransactionManager.java:269) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726) at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:478) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644) at com.artbulb.img.service.impl.TemplateService$$EnhancerBySpringCGLIB$$4e8979d8.batchUpdateArtsGoodsByArtsIdTempId(<generated>) at com.artbulb.img.service.concurrent.ArtsReleaseGoodsCallable.call(ArtsReleaseGoodsCallable.java:217) at com.artbulb.img.service.concurrent.ArtsReleaseGoodsCallable.call(ArtsReleaseGoodsCallable.java:1) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) at java.lang.Thread.run(Thread.java:662) [DEBUG] 2015-12-12 16:46:04:994 SqlSessionUtils:289 - Transaction synchronization ended with unknown status for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3eb86609] [DEBUG] 2015-12-12 16:46:04:994 SqlSessionUtils:298 - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3eb86609] [DEBUG] 2015-12-12 16:46:04:995 DataSourceTransactionManager:319 - Could not reset JDBC Connection after transaction java.sql.SQLException: connection holder is null at com.alibaba.druid.pool.DruidPooledConnection.checkStateInternal(DruidPooledConnection.java:1107) at com.alibaba.druid.pool.DruidPooledConnection.checkState(DruidPooledConnection.java:1100) at com.alibaba.druid.pool.DruidPooledConnection.setAutoCommit(DruidPooledConnection.java:670) at org.springframework.jdbc.datasource.DataSourceTransactionManager.doCleanupAfterCompletion(DataSourceTransactionManager.java:314) at org.springframework.transaction.support.AbstractPlatformTransactionManager.cleanupAfterCompletion(AbstractPlatformTransactionManager.java:1012) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:807) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726) at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:478) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644) at com.artbulb.img.service.impl.TemplateService$$EnhancerBySpringCGLIB$$4e8979d8.batchUpdateArtsGoodsByArtsIdTempId(<generated>) at com.artbulb.img.service.concurrent.ArtsReleaseGoodsCallable.call(ArtsReleaseGoodsCallable.java:217) at com.artbulb.img.service.concurrent.ArtsReleaseGoodsCallable.call(ArtsReleaseGoodsCallable.java:1) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) at java.lang.Thread.run(Thread.java:662) Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communications link failure during commit(). Transaction resolution unknown. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.Util.getInstance(Util.java:381) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926) at com.mysql.jdbc.ConnectionImpl.commit(ConnectionImpl.java:1552) at com.alibaba.druid.filter.FilterChainImpl.connection_commit(FilterChainImpl.java:180) at com.alibaba.druid.filter.stat.StatFilter.connection_commit(StatFilter.java:260) at com.alibaba.druid.filter.FilterChainImpl.connection_commit(FilterChainImpl.java:176) at com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl.commit(ConnectionProxyImpl.java:123) at com.alibaba.druid.pool.DruidPooledConnection.commit(DruidPooledConnection.java:718) at org.springframework.jdbc.datasource.DataSourceTransactionManager.doCommit(DataSourceTransactionManager.java:269) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757) ... 16 more [DEBUG] 2015-12-12 16:46:04:995 DataSourceTransactionManager:324 - Releasing JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6f156508] after transaction
一开始以为是一些数据库连接池,或者mysql的超时参数导致的,
然后各种参数都测试过,还报这个问题。
检查代码:
pool.submit(new ArtsReleaseGoodsCallable(artsInfo.getArtsId(), getProductPrint(), image, artsInfo.getDisplayWidth(), artsInfo.getDisplayHeight(), templateDAO, redis, templateService,cacheKey,isInsertGoods,idList));
将service 中的由Spring管理的实例变量传入多线程中,然后在多线程中调用该实例变量相关方法。
推理原因:
1、 应该是主线程进入service,service 拿到当前线程上线文的数据库connection连接对象,
2、在线程池中,有多个线程的缓存对象,线程上下文中引用的都是同一个connection。
3、由于执行的时间比较久,当一个connection连接执行了超过一定时间就会报异常。
切记:
多线程中应该避免有状态的bean
不要试图缓存数据库连接池中取出来的连接
涉及到增删改的操作不要放到线程池中去执行