随笔- 42  评论- 9  文章- 0 

数据库连接池连接异常com.alibaba.druid.pool.GetConnectionTimeoutException

1、事务没提交

开启了事务,但是没有关闭事务,导致连接池一直被占用

事务管理代码:

@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);
        }
    }

启动事务没提交

TransactionStatus transactionStatus = null;
transactionStatus = startTx();

应该提交事务,释放连接池

commitTx(transactionStatus);

异常回滚事务

rollbackTx(transactionStatus);

 

2、连接没关闭

打开了数据库连接,没有关闭,连接池被占用

Connection conn = null;
ResultSet rs = null;

conn = jdbcTemplate.getDataSource().getConnection();

rs = conn.getMetaData().getTables(null, null, table, null);

应该在完成数据库相关操作后,关闭连接,释放连接池

if (rs != null) {
       rs.close();
 }
  if (conn != null) {
       conn.close();
 }

 

posted on 2018-11-13 15:41  依米欧  阅读(...)  评论(...编辑  收藏