如何在关闭数据库链接时,自动关闭由该链接创建的所有Statement

关键字: 企业应用       

前提条件:
1 使用连接池
2 使用了spring的ioc,即DAO是单例的

提出这个问题是由于我们系统中的实际出现的状况
由于开发人员众多,素质参差不齐,开发时间紧迫,
出现了大量的不符合规范的代码以及错误代码.
常见的就是 在关闭链接的时候没有关闭链接的创建的所有的Statement
(关闭了部分,但不是所有)

所以想和 大家探讨一下该如何在代码层次实现关闭数据库链接时,自动关闭由该链接创建的所有的Statement.
我的思路是这样的

将"当前线程+当前链接"创建的所有Statement 放入一个ThreadLocal 对象内.
当关闭链接时, 从ThreadLocal 对象取出 所有的 Statement ,逐个关闭.

不知道这样的思路是否可行.
下面附上代码:

为了阅读方便没有写出全部的创建Statement的方法

代码
  1.   
  2. public class ConnectionUtils {   
  3.     public static final ThreadLocal statementMap = new ThreadLocal();   
  4.   
  5.     public static void initStatementMap(Connection conn){   
  6.         String key=String.valueOf(conn);   
  7.         Map map=(Map)statementMap.get();   
  8.         if (map==null){   
  9.             map=Collections.synchronizedMap(new HashMap());   
  10.             statementMap.set(map);   
  11.         }   
  12.         if (map.get(key)==null) {   
  13.             map.put(key, new ArrayList());   
  14.         }   
  15.     }   
  16.        
  17.     public static   void putStatement(Connection conn,Statement statement){   
  18.         Map map=(Map)statementMap.get();   
  19.         List list=(List)map.get(conn.toString());   
  20.         list.add(statement);   
  21.     }   
  22.        
  23.     public static   void closeAllStatement(Connection conn){   
  24.         Map map=(Map)statementMap.get();   
  25.         List list=(List)map.get(conn.toString());   
  26.         for (Iterator itor=list.iterator();itor.hasNext();){   
  27.             Statement stm=(Statement)itor.next();   
  28.             try {   
  29.                 stm.close();   
  30.             } catch (SQLException e) {   
  31.             }   
  32.         }   
  33.     }   
  34.        
  35.     public static Statement createStatement(Connection conn) throws SQLException{   
  36.         Statement statement=conn.createStatement();   
  37.         putStatement(conn,statement);   
  38.         return statement;   
  39.     }   
  40.        
  41.   
  42.     public static  CallableStatement prepareCall(Connection conn, String sql) throws SQLException {   
  43.         CallableStatement statement=conn.prepareCall(sql);   
  44.         putStatement(conn,statement);   
  45.         return statement;   
  46.     }   
  47.   
  48.     public static   PreparedStatement prepareStatement(Connection conn, String sql) throws SQLException{   
  49.         PreparedStatement statement= conn.prepareStatement(sql);   
  50.         putStatement(conn,statement);   
  51.         return statement;   
  52.     }   
  53.   
  54. }   
<script>render_code();</script>

 

在dao内的getConnection时 可以这么写

代码
  1. protected final Connection getConnection(){   
  2.     Connection conn=DataSourceUtils.getConnection(getDataSource());   
  3.     ConnectionUtils.initStatementMap(conn);   
  4.     return conn;   
  5. }   
<script>render_code();</script>

 

关闭Connection时 可以这么写

代码
  1. protected final void closeConnection(Connection conn) {   
  2.     ConnectionUtils.closeAllStatement(conn);   
  3.     DataSourceUtils.releaseConnection(conn, getDataSource());   
  4. }   
<script>render_code();</script>

 

要创建Statement时可以这么写

代码
  1. pstmt = ConnectionUtils.prepareStatement(conn,bufSql.toString());   
<script>render_code();</script>

 

以上只是我的一些思路,虽然在本机测试是可以的,但是不知道到底实际上是否可行
还请看看 谢谢了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值