View Javadoc

1   package com.diasparsoftware.jdbc;
2   
3   import java.sql.*;
4   import java.util.*;
5   
6   import com.diasparsoftware.java.util.*;
7   
8   /***
9    * Incorporate one of these registries anywhere you would
10   * like to ensure that JDBC resources are cleaned up.
11   * 
12   * @author <a href="jbr@diasparsoftware.com>J. B. Rainsberger</a>
13   */
14  public class JdbcResourceRegistry {
15      private List connectionsToClose = new LinkedList();
16      private List statementsToClose = new LinkedList();
17      private List resultSetsToClose = new LinkedList();
18  
19      public void registerStatement(Statement statement) {
20          statementsToClose.add(statement);
21      }
22  
23      public void registerResultSet(ResultSet resultSet) {
24          resultSetsToClose.add(resultSet);
25      }
26  
27      public void registerConnection(Connection connection) {
28          connectionsToClose.add(connection);
29      }
30  
31      public void cleanUp() {
32          CollectionUtil
33              .forEachDoIgnoreException(
34                  statementsToClose,
35                  new ExceptionalClosure() {
36  
37              public Object execute(Object each) throws Exception {
38                  ((Statement) each).close();
39                  return null;
40              }
41          });
42  
43          CollectionUtil
44              .forEachDoIgnoreException(
45                  resultSetsToClose,
46                  new ExceptionalClosure() {
47  
48              public Object execute(Object each) throws Exception {
49                  ((ResultSet) each).close();
50                  return null;
51              }
52          });
53  
54          CollectionUtil
55              .forEachDoIgnoreException(
56                  connectionsToClose,
57                  new ExceptionalClosure() {
58  
59              public Object execute(Object each) throws Exception {
60                  ((Connection) each).close();
61                  return null;
62              }
63          });
64  
65      }
66  }