Mega experimental fixes; try to stop a new smallsql connection from being opened...
[jgroupdav.git] / src / main / java / net / bionicmessage / utils / JDBCConnectionFactory.java
blobc4d6d3af4e510070fb1b6a99234ad385fdf06a73
1 /*
2 * Permission is hereby granted, free of charge, to any person obtaining a
3 * copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation
5 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 * and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
14 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
17 * IN THE SOFTWARE.
19 package net.bionicmessage.utils;
21 import java.util.Hashtable;
22 import java.sql.Connection;
23 import java.sql.SQLException;
25 /**
26 * A 'factory' singleton-type thingy that ensures only ONE connection to a JDBC
27 * resource exists
28 * @author matt
30 public class JDBCConnectionFactory {
32 private static JDBCConnectionFactory instance =
33 null;
34 private static Hashtable<String, Connection> connectionTable = null;
36 private JDBCConnectionFactory() {
37 connectionTable = new Hashtable();
40 public static JDBCConnectionFactory getInstance() {
41 if (instance == null) {
42 return new JDBCConnectionFactory();
44 return instance;
47 public static Connection getConnection(String jdbcID) throws SQLException {
48 if (connectionTable.get(jdbcID) != null) {
49 return connectionTable.get(jdbcID);
51 // Else, create one
52 Connection conn = java.sql.DriverManager.getConnection(jdbcID);
53 connectionTable.put(jdbcID, conn);
54 return conn;
57 public static void removeConnection(String jdbcID) {
58 connectionTable.remove(jdbcID);
61 public static boolean doesConnectionExist(String jdbcID) {
62 if (connectionTable.get(jdbcID) != null) {
63 return true;
65 return false;