在一个应用中如果同一个Spring 的IOC容器被实例化两次就会出现 CannotAcquireResourceException 异常

现象描述:我在一个Junit 的测试类中实例化IOC容器 : ac = new ClassPathXmlApplicationContext("applicationContext.xml");  

在另一个SpringMVC的业务层的类中又进行了实例化,ac = new ClassPathXmlApplicationContext("applicationContext.xml");  

也就是说 applicationContext.xml 配置文件被加载了两次。

导致的结果:

我在Junit 测试类中进行单元测试的时候就会出现:Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source. 这样的异常.

我把异常截取一部分贴在下面:

八月 06, 2017 8:39:21 下午 com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
警告: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@51a56061 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
八月 06, 2017 8:39:21 下午 com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
警告: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@51a56061 -- APPARENT DEADLOCK!!! Complete Status: 
Managed Threads: 3
Active Threads: 3
Active Tasks: 
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@5f629425 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@5b5d0b11 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1e5f9e1e (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2)
Pending Tasks: 
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@648343dc
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@2f8b20d5
//省略掉部分异常


八月 06, 2017 8:39:30 下午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1e5f9e1e -- 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.GeneratedConstructorAccessor26.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
//省略掉部分异常

八月 06, 2017 8:39:30 下午 org.hibernate.engine.jdbc.internal.JdbcServicesImpl configure
WARN: HHH000342: Could not obtain connection to query metadata : Connections could not be acquired from the underlying database!
八月 06, 2017 8:39:30 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
八月 06, 2017 8:39:30 下午 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000422: Disabling contextual LOB creation as connection was null
八月 06, 2017 8:39:30 下午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@5f629425 -- 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.GeneratedConstructorAccessor26.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
//省略掉部分异常

八月 06, 2017 8:39:30 下午 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
八月 06, 2017 8:39:30 下午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
八月 06, 2017 8:39:30 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
八月 06, 2017 8:39:30 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
八月 06, 2017 8:39:30 下午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@5b5d0b11 -- 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.GeneratedConstructorAccessor26.newInstance(Unknown Source)
//省略掉部分异常

八月 06, 2017 8:39:30 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000319: Could not get database metadata
java.sql.SQLException: Connections could not be acquired from the underlying database!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:141)
//省略掉部分异常
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
... 240 more


更新一下

现象描述:

我把整个Junit 测试类注释掉之后,只留那个业务层中的类 ac = new ClassPathXmlApplicationContext("applicationContext.xml");  然后run on server 还是报错。

复制代码
  private ApplicationContext ac;
    
    private TestDaoService2 testDaoService2;
    
    {
        ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        testDaoService2 = ac.getBean(TestDaoService2.class);
    }
复制代码

报错如下

警告: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@118aafb2 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
八月 06, 2017 9:04:13 下午 com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
警告: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@118aafb2 -- APPARENT DEADLOCK!!! Complete Status: 
Managed Threads: 3
Active Threads: 3
Active Tasks: 
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1f4d53f3 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1b72c4c2 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@5e7059c8 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1)
Pending Tasks: 
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1ac38a8b
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@6fe480eb
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@62bd0d01
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@3e5fa687
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@384ee415
com.mchange.v2.resourcepool.BasicResourcePool$1RefurbishCheckinResourceTask@73e6c82f
com.mchange.v2.resourcepool.BasicResourcePool$1RefurbishCheckinResourceTask@79b90657
Pool thread stack traces:
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main]
java.lang.Thread.sleep(Native Method)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main]
java.lang.Thread.sleep(Native Method)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main]
java.lang.Thread.sleep(Native Method)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)

原文地址:https://www.cnblogs.com/GooPolaris/p/7919773.html