java.sql.SQLException:连接太多+ Spring IBatis

时间:2015-11-27 06:06:21

标签: java mysql jdbc ibatis spring-jdbc

我正在使用问题java.sql.SQLException: Too many connections

平台: Tomcat server, Java Spring, iBatis and MySQL Amazon Instance

以下是我的JDBC配置:

app.jbdc.initialSize=10
app.jbdc.maxActive=200
app.jdbc.maxWait =1000
app.jdbc.minEvictableIdleTimeMillis =500
app.jdbc.validationQuery=select 1
app.jdbc.testOnBorrow=true
app.jdbc.removeAbandoned=true
app.jdbc.removeAbandonedTimeout=30

下面是错误堆栈:

java.sql.SQLException
MESSAGE: Too many connections
STACKTRACE:

java.sql.SQLException: Too many connections
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2851)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:753)
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3562)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1154)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:1818)
    at com.mysql.jdbc.Connection.<init>(Connection.java:405)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:268)
    at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
    at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)
    at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:840)
    at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:96)
    at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
    at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at net.bull.javamelody.JdbcWrapper$3.invoke(JdbcWrapper.java:757)
    at net.bull.javamelody.JdbcWrapper$DelegatingInvocationHandler.invoke(JdbcWrapper.java:281)
    at com.sun.proxy.$Proxy8.getConnection(Unknown Source)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:113)
    at org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy$TransactionAwareInvocationHandler.invoke(TransactionAwareDataSourceProxy.java:213)
    at com.sun.proxy.$Proxy14.prepareStatement(Unknown Source)
    at com.ibatis.sqlmap.engine.execution.SqlExecutor.prepareStatement(SqlExecutor.java:494)
    at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeUpdate(SqlExecutor.java:76)
    at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.sqlExecuteUpdate(GeneralStatement.java:200)
    at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeUpdate(GeneralStatement.java:78)
    at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert(SqlMapExecutorDelegate.java:447)
    at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:82)
    at org.springframework.orm.ibatis.SqlMapClientTemplate$8.doInSqlMapClient(SqlMapClientTemplate.java:366)
    at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:200)
    at org.springframework.orm.ibatis.SqlMapClientTemplate.insert(SqlMapClientTemplate.java:364)
    at com.wo.user.dao.UserDaoImpl.insertUser(UserDaoImpl.java:26)
    at com.wo.user.service.UserServiceImpl.insertUser(UserServiceImpl.java:89)
    at com.wo.version2.controller.UserControllerV2.registerUser2(UserControllerV2.java:187)
    at sun.reflect.GeneratedMethodAccessor46.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:774)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:164)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:202)
    at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:180)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:156)
    at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:233)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1822)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

我们可以控制最大活动线程吗?在这种情况下可以做些什么?

假设:Spring iBatis正在关闭并打开连接

任何建议或帮助都将不胜感激。

5 个答案:

答案 0 :(得分:1)

更新mysql全局变量以增加连接。

set global max_connections=2000

答案 1 :(得分:1)

如果你这样使用的话。 app.jbdc.maxActive = 2000 app.jdbc.maxWait = 100

然后您可以解决此问题。

基本上这种类型的异常是由于驱动程序不可用而引起的。

答案 2 :(得分:1)

从堆栈跟踪看来,正在使用dbcp连接池库。

您的应用程序是数据库的客户端,并且配置为允许最多200个(maxActive)连接。

我的解释是数据库服务器在达到200限制之前拒绝连接。

您可以配置服务器端以允许来自应用程序的足够连接,或者减少应用程序尝试允许的连接数。

答案 3 :(得分:1)

尝试创建SQLMapClient时,将属性Pool.MaxActiveConnections设置为某个值,从而提供数据库属性。

  Properties props = new Properties();
  props.put("Jdbc.Host", "localhost");
  props.put("Jdbc.Port", "3106");
  props.put("Jdbc.Database", "test");
  props.put("Jdbc.Username", "root");
  props.put("Jdbc.Password", "secret");
  props.put("Pool.MaxActiveConnections", "20");
  //timetowait in millliseconds
  props.put("Pool.TimeToWait", "20000");
  //sqlMapClientReader is reader of sqlmap-config.xml
  SqlMapClient sqlMap = 
    SqlMapClientBuilder.buildSQLMapClient(sqlMapClientReader, props);

或在sqlmap-config.xml中设置

<dataSource type="SIMPLE">
            <property name="JDBC.Driver" value="${driver}"/>
            <property name="JDBC.ConnectionURL" value="${url}"/>
            <property name="JDBC.Username" value="${username}"/>
            <property name="JDBC.Password" value="${password}"/>
            <property name="Pool.MaximumActiveConnections" value="${activeConns}"/>
            <property name="Pool.TimeToWait" value="${timeToWait}"/>
</datasource>

答案 4 :(得分:0)

也许您不会在查询方法中关闭连接资源,例如conn.close()!