001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: JdbcSessionFactory.java 7818 2007-06-11 05:12:08Z jzhang $
023: */
024: package com.bostechcorp.cbesb.runtime.jdbc;
025:
026: import java.util.Properties;
027:
028: import javax.sql.DataSource;
029:
030: import org.apache.commons.dbcp.ConnectionFactory;
031: import org.apache.commons.dbcp.DriverManagerConnectionFactory;
032: import org.apache.commons.dbcp.PoolableConnectionFactory;
033: import org.apache.commons.dbcp.PoolingDataSource;
034: import org.apache.commons.pool.ObjectPool;
035: import org.apache.commons.pool.impl.GenericObjectPool;
036:
037: import com.bostechcorp.cbesb.common.util.ErrorUtil;
038: import com.bostechcorp.cbesb.common.util.RuntimeClassLoader;
039:
040: public class DbUtil {
041:
042: public static String CONSTANT_DRIVER = "driverName";
043: public static String CONSTANT_USER = "user";
044: public static String CONSTANT_PASSWORD = "password";
045: public static String CONSTANT_URL = "url";
046:
047: public DataSource setupDataSource(String jndiName,
048: Properties connectionProperties) throws Exception {
049:
050: PoolingDataSource result = new PoolingDataSource();
051:
052: // instantiate the JDBC driver class. Nothing new
053: // here; this is JDBC 101
054:
055: //System.out.println(connectionProperties.getProperty(CONSTANT_DRIVER));
056: Class
057: .forName(connectionProperties
058: .getProperty(CONSTANT_DRIVER));
059:
060: /*
061: As the name says, this is a generic pool; it returns
062: basic Object-class objects.
063: */
064: final GenericObjectPool pool = new GenericObjectPool(null);
065: pool
066: .setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
067:
068: /*
069: ConnectionFactory creates connections on behalf of the pool.
070: Here, we use the DriverManagerConnectionFactory because that essentially
071: uses DriverManager as the source of connections.
072: */
073: ConnectionFactory factory = new DriverManagerConnectionFactory(
074: connectionProperties.getProperty(CONSTANT_URL),
075: connectionProperties.getProperty(CONSTANT_USER),
076: connectionProperties.getProperty(CONSTANT_PASSWORD));
077:
078: /*
079: Puts pool-specific wrappers on factory connections. For clarification:
080: "[PoolableConnection]Factory," not "Poolable[ConnectionFactory]."
081: */
082: PoolableConnectionFactory pcf = new PoolableConnectionFactory(
083: factory, // ConnectionFactory
084: pool, // ObjectPool
085: null, // KeyedObjectPoolFactory
086: null, // String (validation query)
087: false, // boolean (default to read-only?)
088: true // boolean (default to auto-commit statements?)
089: );
090:
091: /*
092: initialize the pool to 10 connections
093: */
094:
095: for (int ix = 0; ix < 10; ++ix) {
096: pool.addObject();
097: }
098:
099: /*
100: All of this is wrapped in a DataSource, which client code should
101: already know how to handle (since it's the same class of object
102: they'd fetch via the container's JNDI tree
103: */
104:
105: result.setPool(pool);
106:
107: // store the pool, so we can get to it later
108:
109: // System.out.println("pool-"+componentName);
110: ObjectRegistry.getInstance().put("pool-" + jndiName, pool);
111:
112: return (result);
113:
114: }
115:
116: public static void releasePool(String jndiName) {
117: try {
118:
119: final ObjectPool pool = (ObjectPool) ObjectRegistry
120: .getInstance().get("pool-" + jndiName);
121:
122: if (null != pool) {
123: pool.clear();
124: }
125:
126: } catch (Exception ignored) {
127:
128: ErrorUtil.printError("Failed to clear connection pool: "
129: + ignored.getMessage(), ignored);
130:
131: }
132:
133: return;
134:
135: }
136:
137: }
|