KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > jdbc > JDBCConnectionImpl


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.jdbc;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.DriverManager JavaDoc;
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 public class JDBCConnectionImpl {
37
38     final static Log log = LogFactory.getLog(JDBCConnectionImpl.class);
39     
40     static long currentId = 0;
41
42     // database instance name, such as myOracleDB
43
private String JavaDoc key;
44     private long id;
45     private Connection JavaDoc conn;
46     private Hashtable JavaDoc preparedStatements = new Hashtable JavaDoc();
47
48     // the precious connection
49
private JDBCConnectionImpl(String JavaDoc key, JDBCConnectionSettings settings) throws SQLException JavaDoc,
50                     ClassNotFoundException JavaDoc {
51         id = currentId++;
52         this.key = key;
53         Class.forName(settings.driver);
54         conn = DriverManager.getConnection(settings.url, settings.username, settings.password);
55     }
56     
57     public void setAutoCommit(boolean autoCommit) throws SQLException JavaDoc {
58         conn.setAutoCommit(autoCommit);
59     }
60
61     String JavaDoc getKey() {
62         return key;
63     }
64
65     boolean isClosed() throws SQLException JavaDoc {
66         return conn.isClosed();
67     }
68
69     ResultSet JavaDoc executeQuery(String JavaDoc sqlString) throws SQLException JavaDoc {
70         Statement JavaDoc stmt = conn.createStatement();
71         ResultSet JavaDoc rs = stmt.executeQuery(sqlString);
72         return rs;
73     }
74
75     void execute(String JavaDoc sqlString) throws SQLException JavaDoc {
76         Statement JavaDoc stmt = conn.createStatement();
77         try {
78             stmt.execute(sqlString);
79         }
80         finally {
81             stmt.close();
82         }
83     }
84
85     synchronized PreparedStatement JavaDoc aquirePreparedStatement(String JavaDoc key, String JavaDoc sqlString) throws SQLException JavaDoc {
86         Vector JavaDoc avail = (Vector JavaDoc) preparedStatements.get(key);
87         if (avail == null) {
88             avail = new Vector JavaDoc();
89             preparedStatements.put(key, avail);
90         }
91
92         if (avail.size() > 0) {
93             return (PreparedStatement JavaDoc) avail.remove(0);
94         } else
95             return conn.prepareStatement(sqlString);
96     }
97
98     synchronized void releasePreparedStatement(String JavaDoc key, PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
99         ps.clearParameters();
100         Vector JavaDoc avail = (Vector JavaDoc) preparedStatements.get(key);
101         avail.add(ps);
102     }
103
104     public long getId() {
105         return id;
106     }
107     
108     public Connection JavaDoc getConnection() {
109         return conn;
110     }
111
112     public static class JDBCPool {
113         // dictionary of database names with corresponding vector of connections
114
private Hashtable JavaDoc poolDictionary = new Hashtable JavaDoc();
115         // dictionary of database settings
116
private Hashtable JavaDoc poolSettings = new Hashtable JavaDoc();
117
118         // methods and attributes for Singleton pattern
119
private JDBCPool() {
120         } // private constructor
121

122         private static JDBCPool _instance; // get class instance
123

124         // Singleton getter utilizing Double Checked Locking pattern
125
public static JDBCPool getInstance() {
126             if (_instance == null) {
127                 synchronized (JDBCPool.class) {
128                     if (_instance == null) {
129                         _instance = new JDBCPool();
130                     }
131                 }
132             }
133             return _instance;
134         }
135
136         public synchronized void createImpl(String JavaDoc key, String JavaDoc driver, String JavaDoc url, String JavaDoc username, String JavaDoc password)
137                         throws SQLException JavaDoc, ClassNotFoundException JavaDoc {
138
139             Vector JavaDoc pool = new Vector JavaDoc();
140
141             JDBCConnectionSettings settings = new JDBCConnectionSettings();
142             settings.driver = driver;
143             settings.url = url;
144             settings.username = username;
145             settings.password = password;
146
147             poolDictionary.put(key, pool);
148             poolSettings.put(key, settings);
149
150             pool.addElement(new JDBCConnectionImpl(key, settings));
151
152         }
153         
154         public Map JavaDoc getPools() {
155             return poolDictionary;
156         }
157
158         public synchronized void closeAll() {
159             for (Iterator JavaDoc i = poolDictionary.entrySet().iterator(); i.hasNext();) {
160                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
161                 if (log.isInfoEnabled())
162                     log.info("Closing connections for " + entry.getKey());
163                 Vector JavaDoc pool = (Vector JavaDoc) entry.getValue();
164                 for (Iterator JavaDoc j = pool.iterator(); j.hasNext();) {
165                     JDBCConnectionImpl conx = (JDBCConnectionImpl) j.next();
166                     try {
167                         conx.conn.close();
168                     } catch (SQLException JavaDoc sqle) {
169                         log.warn("Failed to close pooled connection. ", sqle);
170                     }
171                 }
172             }
173             poolDictionary.clear();
174
175         }
176
177         // get connection from pool
178
public synchronized JDBCConnectionImpl acquireImpl(String JavaDoc key) throws SQLException JavaDoc, ClassNotFoundException JavaDoc {
179             if (log.isDebugEnabled())
180                 log.debug("Aquiring connection for " + key);
181
182             // get pool matching database name
183
Vector JavaDoc pool = (Vector JavaDoc) poolDictionary.get(key);
184             if (pool != null) {
185                 while (pool.size() > 0) {
186                     if (log.isDebugEnabled())
187                         log.debug(pool.size() + " connections in pool.");
188                     JDBCConnectionImpl impl = null;
189                     // retrieve existing unused connection
190
impl = (JDBCConnectionImpl) pool.elementAt(pool.size() - 1);
191                     // remove connection from pool
192
pool.removeElementAt(pool.size() - 1);
193
194                     // If the connection has closed then drop it and select
195
// another
196
if (impl.isClosed()) {
197                         if (log.isDebugEnabled())
198                             log.debug("Connecting for " + key + " is closed, get the next one");
199                         continue;
200                     }
201
202                     if (log.isDebugEnabled())
203                         log.debug("Found connection " + impl.toString());
204
205                     return impl;
206                 }
207             }
208             if (log.isDebugEnabled())
209                 log.debug("Pool is empty, getting ");
210
211             JDBCConnectionSettings settings = (JDBCConnectionSettings) poolSettings.get(key);
212             // pool is empty so create new connection
213
return new JDBCConnectionImpl(key, settings);
214         }
215
216         // return connection to pool
217
public synchronized void releaseImpl(JDBCConnectionImpl impl) {
218             String JavaDoc key = impl.getKey();
219             Vector JavaDoc pool = (Vector JavaDoc) poolDictionary.get(key);
220             if (pool == null) {
221                 pool = new Vector JavaDoc();
222                 poolDictionary.put(key, pool);
223             }
224
225             pool.addElement(impl);
226         }
227     }
228
229     static class JDBCConnectionSettings {
230         String JavaDoc url;
231         String JavaDoc driver;
232         String JavaDoc cls;
233         String JavaDoc username;
234         String JavaDoc password;
235     }
236
237     public void rollback() throws SQLException JavaDoc {
238         conn.rollback();
239     }
240
241     public void commit() throws SQLException JavaDoc {
242         conn.commit();
243     }
244 }
245
Popular Tags