问题
Caused by: com.alibaba.druid.pool.GetConnectionTimeoutException: wait millis 45000, active 100, runningSqlCount 2
原因
1.事务没提交
开启了事务,但是没有关闭事务,导致连接池一直被占用
事务管理代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @Autowired private PlatformTransactionManager platformTransactionManager; protected TransactionStatus startTx() { TransactionStatus transactionStatus = platformTransactionManager .getTransaction(new DefaultTransactionDefinition( TransactionDefinition.PROPAGATION_REQUIRES_NEW)); return transactionStatus; } protected void commitTx(TransactionStatus transactionStatus) { if(transactionStatus != null){ platformTransactionManager.commit(transactionStatus); } } protected void rollbackTx(TransactionStatus transactionStatus) { if(transactionStatus != null){ platformTransactionManager.rollback(transactionStatus); } }
|
启动事务没提交
1 2
| TransactionStatus transactionStatus = null; transactionStatus = startTx();
|
应该提交事务,释放连接池
1
| commitTx(transactionStatus);
|
异常回滚事务
1
| rollbackTx(transactionStatus);
|
2. 连接没关闭
打开了数据库连接,没有关闭,连接池被占用
1 2 3 4 5 6
| Connection conn = null; ResultSet rs = null;
conn = jdbcTemplate.getDataSource().getConnection();
rs = conn.getMetaData().getTables(null, null, table, null);
|
应该在完成数据库相关操作后,关闭连接,释放连接池
1 2 3 4 5 6
| if (rs != null) { rs.close(); } if (conn != null) { conn.close(); }
|