MySql连接异常解决

这两天遇到一个mysql连接的问题,找人弄了好几天也没弄好,先看一下报错信息:

====================================================================

org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Cannot open connection

at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:596)

at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)

at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:334)

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.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)

at hitec.dms.element.service.biz.impl.DmsMonitorThreadServiceImpl$$EnhancerByCGLIB$$a293fc8f.findList(<generated>)

at hitec.influence.platform.quartz.ThreadListener.monitorThread(ThreadListener.java:117)

at hitec.influence.platform.quartz.job.GatherJob.process(GatherJob.java:12)

at hitec.influence.platform.quartz.QuartzDispatcher$QuartzJob.execute(QuartzDispatcher.java:377)

at org.quartz.core.JobRunShell.run(JobRunShell.java:202)

at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525) Caused by: org.hibernate.exception.JDBCConnectionException: Cannot open connection

at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:97)

at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)

at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)

at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)

at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)

at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)

at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)

at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1354)

at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:555)

... 11 more Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

at sun.reflect.GeneratedConstructorAccessor248.newInstance(Unknown Source)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)

at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)

at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:350)

at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2393)

at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2430)

at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2215)

at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)

at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)

at sun.reflect.GeneratedConstructorAccessor43.newInstance(Unknown Source)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)

at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)

at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)

at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)

at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)

at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1148)

at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:106)

at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)

at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)

at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)

... 16 more Caused by: java.net.SocketException: 打开的文件过多

at java.net.Socket.createImpl(Socket.java:447)

at java.net.Socket.<init>(Socket.java:421)

at java.net.Socket.<init>(Socket.java:241)

at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)

at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)

... 34 more

====================================================================

看到这些报错信息,一直以为是代码里的hibernate建立连接后一直没有断开,直到连接空闲8小时后mysql给断开不让连接,后来一同事以前遇到过这样的问题,其实一直没有注意到下方的一个异常“more Caused by: java.net.SocketException: 打开的文件过多”,因为我的程序是部署在linux上,因为linux有一个ulimit最大打开文件个数,默认是1024(ulimit -a即可查看相关信息),后来查到是别人封装好的jar包里有一个InputStream只有新建打开没有关闭,一直在循环新建打开文件,直到打开超过1024报错,把inputstream加上关闭就OK啦!

上一篇:ORA-04031错误导致宕机案例分析


下一篇:初学Java9:学习Mybatis时报错:Parameter 'name' not found. Available parameters are [1, 0, param1, param2]