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

来源:互联网 发布:喀秋莎录屏软件7.1 编辑:程序博客网 时间:2024/05/17 02:55

如何在关闭数据库链接时,自动关闭由该链接创建的所有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. }   

 

在dao内的getConnection时 可以这么写

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

 

关闭Connection时 可以这么写

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

 

要创建Statement时可以这么写

代码
  1. pstmt = ConnectionUtils.prepareStatement(conn,bufSql.toString());   

 

 

 
原创粉丝点击