1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package pl.kernelpanic.dbmonster.connection;
48
49 import java.sql.Connection;
50 import java.sql.DatabaseMetaData;
51 import java.sql.SQLException;
52 import java.util.Properties;
53
54 import javax.sql.DataSource;
55
56 import org.apache.commons.dbcp.ConnectionFactory;
57 import org.apache.commons.dbcp.PoolingDataSource;
58 import org.apache.commons.dbcp.PoolableConnectionFactory;
59 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
60 import org.apache.commons.logging.Log;
61 import org.apache.commons.pool.ObjectPool;
62 import org.apache.commons.pool.impl.GenericObjectPool;
63
64 /***
65 * The connection provider which uses commons-dbcp.
66 * It uses the following properties to establish connection:
67 *
68 * <ul>
69 * <li>dbmonster.jdbc.driver (ie. org.postgresql.Driver)</li>
70 * <li>dbmonster.jdbc.url (ie. jdbc:postgresql://127.0.0.1/dbmonster)</li>
71 * <li>dbmonster.jdbc.username (ie. dbmonster)</li>
72 * <li>dbmonster.jdbc.password (ie. dbmonster)</li>
73 * </ul>
74 *
75 * By default these properties are taken from System.getProperty, but you may
76 * also provide them on your own.
77 *
78 * @author Piotr Maj
79 *
80 * @version $Id: DBCPConnectionProvider.html,v 1.1 2007/06/21 08:38:14 sbahloul Exp $
81 */
82 public class DBCPConnectionProvider implements ConnectionProvider, LogEnabled {
83
84 /***
85 * JDBC driver name.
86 */
87 private String driver = System.getProperty("dbmonster.jdbc.driver");
88
89 /***
90 * JDBC url.
91 */
92 private String url = System.getProperty("dbmonster.jdbc.url");
93
94 /***
95 * JDBC user name.
96 */
97 private String username = System.getProperty("dbmonster.jdbc.username");
98
99 /***
100 * JDBC user's password.
101 */
102 private String password = System.getProperty("dbmonster.jdbc.password");
103
104 /***
105 * Properties used to establish connection.
106 */
107 private Properties properties = null;
108
109 /***
110 * A datasource.
111 */
112 private DataSource dataSource = null;
113
114 /***
115 * Logger.
116 */
117 private Log logger = null;
118
119 /***
120 * Connection pool handler.
121 */
122 private ObjectPool pool;
123
124 /***
125 * Creates new SimpleConnectionProvider.
126 *
127 * @throws Exception if driver class could not be found.
128 */
129 public DBCPConnectionProvider() throws Exception {
130 initDriver();
131 setupPool();
132 }
133
134 /***
135 * Creates new DBCPConnectionProvider with given connection info.
136 *
137 * @param jdbcDriver JDBC driver
138 * @param jdbcUrl JDBC url
139 * @param jdbcUsername JDBC user name
140 * @param jdbcPassword JDBC password
141 *
142 * @throws Exception if the driver class could not be found.
143 */
144 public DBCPConnectionProvider(
145 final String jdbcDriver,
146 final String jdbcUrl,
147 final String jdbcUsername,
148 final String jdbcPassword) throws Exception {
149 driver = jdbcDriver;
150 url = jdbcUrl;
151 username = jdbcUsername;
152 password = jdbcPassword;
153 initDriver();
154 setupPool();
155 }
156
157 /***
158 * Creates new DBCPConnectionProvider with properties (usefull
159 * for interbase).
160 *
161 * @param jdbcDriver JDBC driver
162 * @param jdbcUrl JDBC url
163 * @param props properties
164 *
165 * @throws Exception if driver class could not be found.
166 */
167 public DBCPConnectionProvider(
168 final String jdbcDriver,
169 final String jdbcUrl,
170 final Properties props) throws Exception {
171 driver = jdbcDriver;
172 url = jdbcUrl;
173 properties = props;
174 initDriver();
175 setupPool();
176 }
177
178 /***
179 * @see ConnectionProvider#getConnection()
180 */
181 public final Connection getConnection() throws SQLException {
182 if (dataSource == null) {
183 throw new SQLException("Data source is null!");
184 }
185 Connection conn = dataSource.getConnection();
186 conn.setAutoCommit(true);
187 return conn;
188 }
189
190 /***
191 * @see ConnectionProvider#testConnection()
192 */
193 public final void testConnection() throws SQLException {
194 Connection conn = getConnection();
195 DatabaseMetaData dbmd = conn.getMetaData();
196 if (logger != null && logger.isInfoEnabled()) {
197 logger.info("Today we are feeding: "
198 + dbmd.getDatabaseProductName() + " "
199 + dbmd.getDatabaseProductVersion());
200 }
201 conn.close();
202 dbmd = null;
203 conn = null;
204 }
205
206 /***
207 * Sets the logger.
208 *
209 * @param log a logger.
210 */
211 public final void setLogger(final Log log) {
212 logger = log;
213 }
214
215 /***
216 * Shutdown this connection provider.
217 */
218 public final void shutdown() {
219 try {
220 pool.close();
221 } catch (Exception e) {
222 } finally {
223 pool = null;
224 }
225 dataSource = null;
226 driver = null;
227 logger = null;
228 password = null;
229 properties = null;
230 url = null;
231 username = null;
232 }
233
234 /***
235 * Initializes the JDBC driver.
236 *
237 * @throws ClassNotFoundException if driver class could not be found.
238 */
239 private final void initDriver() throws ClassNotFoundException {
240 Class.forName(driver);
241 }
242
243 /***
244 * Sets the pool up.
245 *
246 * @throws Exception if the pool cannot be set up
247 */
248 private final void setupPool() throws Exception {
249 pool = new GenericObjectPool(null);
250 ConnectionFactory connFactory = null;
251 if (properties != null) {
252 connFactory = new DriverManagerConnectionFactory(url, properties);
253 } else {
254 connFactory =
255 new DriverManagerConnectionFactory(url, username, password);
256 }
257 new PoolableConnectionFactory(
258 connFactory, pool, null, null, false, true);
259 dataSource = new PoolingDataSource(pool);
260 }
261 }