java spring c3p0 jdbc 怎么循环插入几十次就挂了

发布于 2022-08-31 20:22:04 字数 4046 浏览 18 评论 0

java spring c3p0 jdbc 怎么循环插入几十次就挂了
是不是没释放连接

报错好像是连接过多,应该怎么处理

package com.tuzuan.test.dao;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tuzuan.dao.FileTuZuanDao;
import com.tuzuan.model.FileTuZuan;

public class FileTuZuanDaoImplTest {

    private ApplicationContext ctx = null;
    private FileTuZuanDao fileTuZuanDao;

    {
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        fileTuZuanDao = (FileTuZuanDao) ctx.getBean("fileTuZuanDao");
    }

    @Test
    public void testSave() {
        for (int i = 0; i < 1000; i++) {
            FileTuZuan fileTuZuan = new FileTuZuan("jpg", "7fd8c3b7d493a53435e6f2a459b4a9e0", "a226e30887c65aa124939d5b1fdfcff3", 100, 500, "/file_tuzuan/2014/11/19", "jpg", "m_96e753bfb781000001ff20c498e0 - 副本 (4).jpg", 12541, 1416381592, 0);
            int fileId = fileTuZuanDao.save(fileTuZuan);
            System.out.println(fileId);
        }
    }

}
警告: com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@2c21ac2a -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: 
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"
    at sun.reflect.GeneratedConstructorAccessor13.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:377)
    at com.mysql.jdbc.Util.getInstance(Util.java:360)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:924)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1037)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2234)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at sun.reflect.GeneratedConstructorAccessor11.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:377)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:395)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
    at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:146)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:195)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:184)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:200)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1086)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1073)
    at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:44)
    at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1810)
    at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:648)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还给你自由 2022-09-07 20:22:04

已经提示的很明显了:Data source rejected establishment of connection, message from server: "Too many connections"

1,增大连接池链接上限;
2,增大数据库连接上限;
3,使用后的链接及时释放。

围归者 2022-09-07 20:22:04

同ls
连接没释放的可能性比较大,建议你看下datasource的状态就一清二楚。

DataSource ds=ctx.getBean("datasource");  
PooledDataSource pds = (PooledDataSource) ds;
System.err.println("空闲的连接: " + pds.getNumIdleConnectionsDefaultUser());
System.err.println("使用中的连接: " + pds.getNumBusyConnectionsDefaultUser());
明月松间行 2022-09-07 20:22:04

你可以把"applicationContext.xml" 贴出来, 看看你的dataSource bean是怎么定义的

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文