001: /*
002: * $Id: DBCPConnectionFactory.java,v 1.3 2003/09/18 16:01:22 jonesde Exp $
003: *
004: * <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
005: *
006: * <p>Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * <p>The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.entity.transaction;
025:
026: import java.sql.Connection;
027: import java.sql.SQLException;
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: import javax.sql.DataSource;
032:
033: import org.apache.commons.dbcp.DriverManagerConnectionFactory;
034: import org.apache.commons.dbcp.PoolingDataSource;
035: import org.apache.commons.pool.ObjectPool;
036: import org.apache.commons.pool.impl.GenericObjectPool;
037: import org.ofbiz.base.util.Debug;
038: import org.ofbiz.entity.GenericEntityException;
039: import org.w3c.dom.Element;
040:
041: /**
042: * DBCP ConnectionFactory - central source for JDBC connections from DBCP
043: *
044: * This is currently non transactional as DBCP doesn't seem to support
045: * transactional datasources yet (DBCP 1.0).
046: *
047: * @author <a href="mailto:mike@atlassian.com">Mike Cannon-Brookes</a>
048: * @version $Revision: 1.3 $
049: * @since 2.0
050: */
051: public class DBCPConnectionFactory {
052:
053: public static final String module = DBCPConnectionFactory.class
054: .getName();
055: protected static Map dsCache = new HashMap();
056:
057: public static Connection getConnection(String helperName,
058: Element dbcpJdbcElement) throws SQLException,
059: GenericEntityException {
060: // the PooledDataSource implementation
061: DataSource dataSource = (DataSource) dsCache.get(helperName);
062:
063: if (dataSource != null) {
064: return dataSource.getConnection();
065: }
066:
067: try {
068: synchronized (DBCPConnectionFactory.class) {
069: //try again inside the synch just in case someone when through while we were waiting
070: dataSource = (DataSource) dsCache.get(helperName);
071: if (dataSource != null) {
072: return dataSource.getConnection();
073: }
074:
075: // First, we'll need a ObjectPool that serves as the actual pool of connections.
076: ObjectPool connectionPool = new GenericObjectPool(null);
077:
078: // Next, we'll create a ConnectionFactory that the pool will use to create Connections.
079: String connectURI = dbcpJdbcElement
080: .getAttribute("jdbc-uri");
081:
082: // String driver = dbcpJdbcElement.getAttribute("jdbc-driver");
083: String username = dbcpJdbcElement
084: .getAttribute("jdbc-username");
085: String password = dbcpJdbcElement
086: .getAttribute("jdbc-password");
087:
088: String driverClassName = dbcpJdbcElement
089: .getAttribute("jdbc-driver");
090: ClassLoader loader = Thread.currentThread()
091: .getContextClassLoader();
092: Class clazz = loader.loadClass(driverClassName);
093: clazz.newInstance();
094: org.apache.commons.dbcp.ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
095: connectURI, username, password);
096:
097: // Now we'll create the PoolableConnectionFactory, which wraps
098: // the "real" Connections created by the ConnectionFactory with
099: // the classes that implement the pooling functionality.
100: //PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
101:
102: // Finally, we create the PoolingDriver itself,
103: // passing in the object pool we created.
104: dataSource = new PoolingDataSource(connectionPool);
105: dataSource.setLogWriter(Debug.getPrintWriter());
106: dsCache.put(helperName, dataSource);
107: return dataSource.getConnection();
108: }
109: } catch (Exception e) {
110: String errorMsg = "Error getting datasource via DBCP.";
111: Debug.logError(e, errorMsg, module);
112: }
113:
114: return null;
115: }
116: }
|