001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data.jdbc.datasource;
017:
018: import java.sql.Connection;
019: import java.sql.SQLException;
020:
021: import org.apache.commons.dbcp.BasicDataSource;
022: import org.geotools.data.DataSourceException;
023:
024: /**
025: * Utility methods to build a default connection pool
026: *
027: * @author Andrea Aime - TOPP
028: *
029: */
030: public class DataSourceUtil {
031: private DataSourceUtil() {
032: // singleton
033: }
034:
035: /**
036: * Builds up a default DBCP DataSource that easy to use connection factories
037: * can use to setup a connection pool.
038: *
039: * @param url
040: * the jdbc url
041: * @param driverName
042: * the jdbc driver full qualified class name
043: * @param username
044: * @param password
045: * @param validationQuery
046: * the validation query to be used for connection liveliness on
047: * borrow, or null, if no check is to be performed
048: * @return
049: * @throws DataSourceException
050: */
051: public static ManageableDataSource buildDefaultDataSource(
052: String url, String driverName, String username,
053: String password, String validationQuery)
054: throws DataSourceException {
055: return buildDefaultDataSource(url, driverName, username,
056: password, 10, 1, validationQuery, false, 0);
057: }
058:
059: /**
060: * Builds up a default DBCP DataSource that easy to use connection factories
061: * can use to setup a connection pool.
062: *
063: * @param url
064: * the jdbc url
065: * @param driverName
066: * the jdbc driver full qualified class name
067: * @param username
068: * @param password
069: * @param maxActive maximum number of concurrent connections in the pool
070: * @param minIdle minimum number of concurrent connections in the pool
071: * @param validationQuery
072: * the validation query to be used for connection liveliness on
073: * borrow, or null, if no check is to be performed
074: * @param cachePreparedStatements
075: * wheter to cache prepared statements or not
076: * @return
077: * @throws DataSourceException
078: */
079: public static ManageableDataSource buildDefaultDataSource(
080: String url, String driverName, String username,
081: String password, int maxActive, int minIdle,
082: String validationQuery, boolean cachePreparedStatements,
083: int removeAbandonedTimeout) throws DataSourceException {
084: // basics
085: BasicDataSource dataSource = new BasicDataSource();
086: dataSource.setDriverClassName(driverName);
087: dataSource.setUrl(url);
088: dataSource.setUsername(username);
089: dataSource.setPassword(password);
090: dataSource.setAccessToUnderlyingConnectionAllowed(true);
091:
092: // pool size
093: dataSource.setMaxActive(maxActive);
094: dataSource.setMinIdle(minIdle);
095:
096: // pool eviction settings
097: dataSource.setMinEvictableIdleTimeMillis(1000 * 20);
098: dataSource.setTimeBetweenEvictionRunsMillis(1000 * 10);
099:
100: // connection validation
101: if (validationQuery != null) {
102: dataSource.setTestOnBorrow(true);
103: dataSource.setValidationQuery(validationQuery);
104: }
105:
106: // prepared statement cache
107: if (cachePreparedStatements) {
108: dataSource.setPoolPreparedStatements(true);
109: dataSource.setMaxOpenPreparedStatements(10);
110: }
111:
112: // remove abandoned connections (I know it's deprecated, but we do want
113: // something shaving off lost connections. Let's give them 5 minutes of
114: // continuous usage
115: if (removeAbandonedTimeout > 0) {
116: dataSource.setRemoveAbandoned(true);
117: dataSource
118: .setRemoveAbandonedTimeout(removeAbandonedTimeout);
119: dataSource.setLogAbandoned(true);
120: }
121:
122: Connection conn = null;
123: try {
124: conn = dataSource.getConnection();
125: } catch (Exception e) {
126: throw new DataSourceException("Connection test failed ", e);
127: } finally {
128: if (conn != null)
129: try {
130: conn.close();
131: } catch (SQLException e) {
132: }
133: }
134:
135: return new DBCPDataSource(dataSource);
136: }
137: }
|