2 smalltengger smallTengGer 于 2015.05.28 11:10 提问

关于java项目访问数据库的问题

我用spring的定时任务,第一次操作数据库的时候没问题。但是10分钟后再次操作执行方法操作数据库就会报以下错误:

org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 616,228 milliseconds ago. The last packet sent successfully to the server was 24,213 milliseconds ago.
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:240)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy34.insertObject(Unknown Source)
at com.supinfo.test.AutoGenerateData.generateData(AutoGenerateData.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:264)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 616,228 milliseconds ago. The last packet sent successfully to the server was 24,213 milliseconds ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1118)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3055)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2941)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3489)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2562)
at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:4956)
at com.mchange.v2.c3p0.impl.NewProxyConnection.setAutoCommit(NewProxyConnection.java:881)
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:223)
... 16 more
Caused by: java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:114)
at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:161)
at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:189)
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2499)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2952)
... 24 more

11个回答

tianwenfenghaha
tianwenfenghaha   2015.05.28 11:42

检查一下服务、连接信息,最后看下权限,再不行你把最大连接数调下试试,这些都没问题的话我也没辙了

smallTengGer
smallTengGer 问题是第一次都能访问,10分钟后就报错了
2 年多之前 回复
edouardzyc
edouardzyc   2015.05.28 13:37

mysql 好像连接有自动断连的问题,不过我记得mysql默认好像是8个小时? 我以前碰到过 连接池里的连接一直没有使用 结果再用的时候就无法连接了
你再网上搜搜?

yangkalaok
yangkalaok   2015.05.28 16:28

应该是连接关闭,检查第二次使用前,连接受否正常

smallTengGer
smallTengGer 第二次执行到查询的时候就报这个错了 啊
2 年多之前 回复
wgw335363240
wgw335363240   2015.05.28 16:46

你这个是使用过的connection已经被mysql关闭了,而你定时任务下次执行的时候,没有重新开connection,而是直接使用了上次打开的连接。使用连接池,或者每次都重新打开连接。

baidu_25310663
baidu_25310663   2015.05.28 17:55

连接池里看看配置的对不对

u010604631
u010604631   2015.05.28 22:16

对啊,"Software caused connection abort: recv failed"是连接异常而且是你编写的程序出现异常,你在第一次访问后是不是把把连接给关闭了,把那一部分代码copy给大家看看

jiuqiyuliang
jiuqiyuliang   2015.05.29 09:15

第一次连接没有问题,第二次连接数据库时出现了错误

fx_ss
fx_ss   2015.05.29 13:11

连接信息 有问题

devildream
devildream   2015.05.29 14:56

事务的打开与关闭这些操作,注意查看一下。

cuiwei1026522829
cuiwei1026522829   Ds   Rxr 2015.05.30 21:26

注意一下链接问题,事务的打开与关闭

共11条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片