1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.util.dbcp;
21
22 import org.apache.avalon.excalibur.datasource.DataSourceComponent;
23 import org.apache.avalon.framework.activity.Disposable;
24 import org.apache.avalon.framework.configuration.Configurable;
25 import org.apache.avalon.framework.configuration.Configuration;
26 import org.apache.avalon.framework.configuration.ConfigurationException;
27 import org.apache.avalon.framework.logger.AbstractLogEnabled;
28 import org.apache.commons.dbcp.BasicDataSource;
29
30 import java.io.PrintWriter;
31 import java.sql.Connection;
32 import java.sql.SQLException;
33
34 /***
35 * <p>
36 * This is a reliable DataSource implementation, based on the pooling logic provided by <a
37 * href="http://jakarta.apache.org/commons/dbcp.html">DBCP</a> and the configuration found in
38 * Avalon's excalibur code.
39 * </p>
40 *
41 * <p>
42 * This uses the normal <code>java.sql.Connection</code> object and
43 * <code>java.sql.DriverManager</code>. The Configuration is like this:
44 * <pre>
45 * <jdbc>
46
47 * <pool-controller min="<i>5</i>" max="<i>10</i>" connection-class="<i>my.overrided.ConnectionClass</i>">
48 * <keep-alive>select 1</keep-alive>
49 * </pool-controller>
50
51 * <driver><i>com.database.jdbc.JdbcDriver</i></driver>
52 * <dburl><i>jdbc:driver://host/mydb</i></dburl>
53 * <user><i>username</i></user>
54 * <password><i>password</i></password>
55 * </jdbc>
56 * </pre>
57 * </p>
58 * <p>
59 * These configuration settings are available:
60 * <ul>
61 * <li><b>driver</b> - The class name of the JDBC driver</li>
62 * <li><b>dburl</b> - The JDBC URL for this connection</li>
63 * <li><b>user</b> - The username to use for this connection</li>
64 * <li><b>password</b> - The password to use for this connection</li>
65 * <li><b>keep-alive</b> - The SQL query that will be used to validate connections from this pool before returning them to the caller. If specified, this query <strong>MUST</strong> be an SQL SELECT statement that returns at least one row.</li>
66 * <li><b>max</b> - The maximum number of active connections allowed in the pool. 0 means no limit. (default 2)</li>
67 * <li><b>max_idle</b> - The maximum number of idle connections. 0 means no limit. (default 0)</li>
68 * <li><b>initial_size</b> - The initial number of connections that are created when the pool is started. (default 0)</li>
69 * <li><b>min_idle</b> - The minimum number of active connections that can remain idle in the pool, without extra ones being created, or zero to create none. (default 0)</li>
70 * <li><b>max_wait</b> - The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception, or -1 to wait indefinitely. (default -1)</li>
71 * <li><b>testOnBorrow</b> - The indication of whether objects will be validated before being borrowed from the pool. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another. (default true)</li>
72 * <li><b>testOnReturn</b> - The indication of whether objects will be validated before being returned to the pool. (default false)</li>
73 * <li><b>testWhileIdle</b> - The indication of whether objects will be validated by the idle object evictor (if any). If an object fails to validate, it will be dropped from the pool. (default false)</li>
74 * <li><b>timeBetweenEvictionRunsMillis</b> - The number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run. (default -1)</li>
75 * <li><b>numTestsPerEvictionRun</b> - The number of objects to examine during each run of the idle object evictor thread (if any). (default 3)</li>
76 * <li><b>minEvictableIdleTimeMillis</b> - The minimum amount of time an object may sit idle in the pool before it is eligable for eviction by the idle object evictor (if any). (default 1000 * 60 * 30)</li>
77 * </ul>
78 *
79 * @version CVS $Revision: 494012 $
80 */
81 public class JdbcDataSource extends AbstractLogEnabled
82 implements Configurable,
83 Disposable,
84 DataSourceComponent {
85
86 BasicDataSource source = null;
87
88
89
90 /***
91 * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
92 */
93 public void configure(final Configuration configuration)
94 throws ConfigurationException {
95
96 try {
97 String driver = configuration.getChild("driver").getValue(null);
98 Class.forName(driver);
99
100 String dburl = configuration.getChild("dburl").getValue(null);
101 String user = configuration.getChild("user").getValue(null);
102 String password = configuration.getChild("password").getValue(null);
103
104
105
106
107 source = new BasicDataSource() {
108 protected synchronized javax.sql.DataSource createDataSource()
109 throws SQLException {
110 if (dataSource != null) {
111 return (dataSource);
112 } else {
113 javax.sql.DataSource ds = super.createDataSource();
114 connectionPool.setTestOnBorrow(true);
115 connectionPool.setTestOnReturn(true);
116 return ds;
117 }
118 }
119 };
120
121 source.setDriverClassName(driver);
122 source.setUrl(dburl);
123 source.setUsername(user);
124 source.setPassword(password);
125 source.setMaxActive(configuration.getChild("max").getValueAsInteger(2));
126 source.setMaxIdle(configuration.getChild("max_idle").getValueAsInteger(0));
127 source.setInitialSize(configuration.getChild("initial_size").getValueAsInteger(0));
128 source.setMinIdle(configuration.getChild("min_idle").getValueAsInteger(0));
129
130 source.setMaxWait(configuration.getChild("max_wait").getValueAsInteger(5000));
131 source.setValidationQuery(configuration.getChild("keep-alive").getValue(null));
132 source.setTestOnBorrow(configuration.getChild("testOnBorrow").getValueAsBoolean(true));
133 source.setTestOnReturn(configuration.getChild("testOnReturn").getValueAsBoolean(false));
134 source.setTestWhileIdle(configuration.getChild("testWhileIdle").getValueAsBoolean(false));
135 source.setTimeBetweenEvictionRunsMillis(configuration.getChild("timeBetweenEvictionRunsMillis").getValueAsInteger(-1));
136 source.setNumTestsPerEvictionRun(configuration.getChild("numTestsPerEvictionRun").getValueAsInteger(3));
137 source.setMinEvictableIdleTimeMillis(configuration.getChild("minEvictableIdleTimeMillis").getValueAsInteger(1000 * 30 * 60));
138
139
140
141
142
143
144
145
146
147 final java.io.Writer writer = new java.io.CharArrayWriter() {
148 public void flush() {
149
150 if (JdbcDataSource.this.getLogger().isErrorEnabled()) {
151 JdbcDataSource.this.getLogger().error(toString());
152 }
153 reset();
154 }
155 };
156
157 source.setLogWriter(new PrintWriter(writer, true));
158
159
160 getLogger().debug("max wait: " + source.getMaxWait());
161 getLogger().debug("max idle: " + source.getMaxIdle());
162 getLogger().debug("max active: " + source.getMaxActive());
163 getLogger().debug("initial size: " + source.getInitialSize());
164 getLogger().debug("TestOnBorrow: " + source.getTestOnBorrow());
165 getLogger().debug("TestOnReturn: " + source.getTestOnReturn());
166 getLogger().debug("TestWhileIdle: " + source.getTestWhileIdle());
167 getLogger().debug("NumTestsPerEvictionRun(): " + source.getNumTestsPerEvictionRun());
168 getLogger().debug("MinEvictableIdleTimeMillis(): " + source.getMinEvictableIdleTimeMillis());
169 getLogger().debug("TimeBetweenEvictionRunsMillis(): " + source.getTimeBetweenEvictionRunsMillis());
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 source.getConnection().close();
199 } catch (Exception e) {
200 throw new ConfigurationException("Error configurable datasource", e);
201 }
202 }
203
204 /***
205 * @see org.apache.avalon.framework.configuration.Configurable#dispose()
206 */
207 public void dispose() {
208
209 try {
210 source.close();
211 } catch (SQLException sqle) {
212 sqle.printStackTrace();
213 }
214 }
215
216 /***
217 *
218 */
219 public Connection getConnection() throws SQLException {
220 return source.getConnection();
221 }
222 }