KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > util > ConnectionManager


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.util;
26
27 import org.apache.commons.dbcp.ConnectionFactory;
28 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
29 import org.apache.commons.dbcp.PoolableConnectionFactory;
30 import org.apache.commons.dbcp.PoolingDataSource;
31 import org.apache.commons.pool.ObjectPool;
32 import org.apache.commons.pool.impl.GenericObjectPool;
33 import org.radeox.util.logging.Logger;
34 import org.snipsnap.app.Application;
35 import org.snipsnap.config.Configuration;
36
37 import javax.sql.DataSource JavaDoc;
38 import java.sql.Connection JavaDoc;
39 import java.sql.ResultSet JavaDoc;
40 import java.sql.SQLException JavaDoc;
41 import java.sql.Statement JavaDoc;
42
43 /**
44  * The connection manager handles all database connections.
45  *
46  * @author Stephan J. Schmidt, Matthias L. Jugel
47  * @version $Id: ConnectionManager.java 1712 2004-07-14 11:20:59Z leo $
48  */

49 public class ConnectionManager {
50   private static ConnectionManager instance;
51
52   public static ConnectionManager getInstance() {
53     if (null == instance) {
54       instance = new ConnectionManager();
55     }
56     return instance;
57   }
58
59   public static synchronized void removeInstance() {
60     if (null != instance) {
61       instance = null;
62     }
63   }
64
65   private DataSource dataSource = null;
66
67   private ConnectionManager() {
68   }
69
70   private void update(Configuration config) {
71     if (null == dataSource) {
72       try {
73         System.err.println("ConnectionManager: Registering JDBC driver: " + config.getJdbcDriver());
74         Class.forName(config.getJdbcDriver());
75       } catch (Exception JavaDoc e) {
76         Logger.fatal("unable to register JDBC driver: " + config.getJdbcDriver(), e);
77       }
78
79       String JavaDoc jdbcUrl = config.getJdbcUrl();
80 // if (jdbcUrl.indexOf("?") != -1) {
81
// jdbcUrl = jdbcUrl.concat("&");
82
// } else {
83
// jdbcUrl = jdbcUrl.concat("?");
84
// }
85
String JavaDoc jdbcPassword = config.getJdbcPassword();
86 // if (null == jdbcPassword) {
87
// jdbcPassword = "";
88
// }
89
// jdbcUrl = jdbcUrl.concat("user=" + config.getJdbcUser()).concat("&password=" + jdbcPassword);
90
// System.err.println("ConnectionManager: using: "+ jdbcUrl);
91
ObjectPool connectionPool = new GenericObjectPool(null);
92       ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcUrl, config.getJdbcUser(), jdbcPassword);
93       PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
94       dataSource = new PoolingDataSource(connectionPool);
95     }
96   }
97
98   private Connection JavaDoc connection() {
99     update(Application.get().getConfiguration());
100
101     try {
102       return dataSource.getConnection();
103     } catch (Exception JavaDoc e) {
104       Logger.fatal("unable to get connection: ", e);
105       return null;
106     }
107   }
108
109   private DataSource dataSource() {
110     update(Application.get().getConfiguration());
111
112     return dataSource;
113   }
114
115   public static DataSource getDataSource() {
116     return getInstance().dataSource();
117   }
118
119   public static Connection JavaDoc getConnection() {
120     return getInstance().connection();
121   }
122
123   public static void close(Connection JavaDoc conn) {
124     if (null != conn) {
125       try {
126         conn.close();
127       } catch (SQLException JavaDoc e) {
128         // We can't do anything
129
}
130     }
131   }
132
133   public static void close(Statement JavaDoc statement) {
134     if (null != statement) {
135       try {
136         statement.close();
137       } catch (SQLException JavaDoc e) {
138         // We can't do anything
139
}
140     }
141   }
142
143   public static void close(ResultSet JavaDoc result) {
144     if (null != result) {
145       try {
146         result.close();
147       } catch (SQLException JavaDoc e) {
148         // We can't do anything
149
}
150     }
151   }
152 }
153
Popular Tags