KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > transaction > DBCPConnectionFactory


1 /*
2  * $Id: DBCPConnectionFactory.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
5  *
6  * <p>Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * <p>The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.entity.transaction;
25
26 import java.sql.Connection JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.sql.DataSource JavaDoc;
32
33 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
34 import org.apache.commons.dbcp.PoolingDataSource;
35 import org.apache.commons.pool.ObjectPool;
36 import org.apache.commons.pool.impl.GenericObjectPool;
37 import org.ofbiz.base.util.Debug;
38 import org.ofbiz.entity.GenericEntityException;
39 import org.w3c.dom.Element JavaDoc;
40
41 /**
42  * DBCP ConnectionFactory - central source for JDBC connections from DBCP
43  *
44  * This is currently non transactional as DBCP doesn't seem to support
45  * transactional datasources yet (DBCP 1.0).
46  *
47  * @author <a HREF="mailto:mike@atlassian.com">Mike Cannon-Brookes</a>
48  * @version $Rev: 5462 $
49  * @since 2.0
50  */

51 public class DBCPConnectionFactory {
52
53     public static final String JavaDoc module = DBCPConnectionFactory.class.getName();
54     protected static Map JavaDoc dsCache = new HashMap JavaDoc();
55
56     public static Connection JavaDoc getConnection(String JavaDoc helperName, Element JavaDoc dbcpJdbcElement) throws SQLException JavaDoc, GenericEntityException {
57         // the PooledDataSource implementation
58
DataSource JavaDoc dataSource = (DataSource JavaDoc)dsCache.get(helperName);
59
60         if (dataSource != null) {
61             return TransactionFactory.getCursorConnection(helperName, dataSource.getConnection());
62         }
63
64         try {
65             synchronized (DBCPConnectionFactory.class) {
66                 //try again inside the synch just in case someone when through while we were waiting
67
dataSource = (DataSource JavaDoc)dsCache.get(helperName);
68                 if (dataSource != null) {
69                     return dataSource.getConnection();
70                 }
71
72                 // First, we'll need a ObjectPool that serves as the actual pool of connections.
73
ObjectPool connectionPool = new GenericObjectPool(null);
74
75                 // Next, we'll create a ConnectionFactory that the pool will use to create Connections.
76
String JavaDoc connectURI = dbcpJdbcElement.getAttribute("jdbc-uri");
77
78                 // String driver = dbcpJdbcElement.getAttribute("jdbc-driver");
79
String JavaDoc username = dbcpJdbcElement.getAttribute("jdbc-username");
80                 String JavaDoc password = dbcpJdbcElement.getAttribute("jdbc-password");
81
82                 String JavaDoc driverClassName = dbcpJdbcElement.getAttribute("jdbc-driver");
83                 ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
84                 Class JavaDoc clazz = loader.loadClass(driverClassName);
85                 clazz.newInstance();
86                 org.apache.commons.dbcp.ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,username,password);
87
88                 // Now we'll create the PoolableConnectionFactory, which wraps
89
// the "real" Connections created by the ConnectionFactory with
90
// the classes that implement the pooling functionality.
91
//PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
92

93                 // Finally, we create the PoolingDriver itself,
94
// passing in the object pool we created.
95
dataSource = new PoolingDataSource(connectionPool);
96                 dataSource.setLogWriter(Debug.getPrintWriter());
97                 dsCache.put(helperName, dataSource);
98                 return TransactionFactory.getCursorConnection(helperName, dataSource.getConnection());
99             }
100         } catch (Exception JavaDoc e) {
101             String JavaDoc errorMsg = "Error getting datasource via DBCP.";
102             Debug.logError(e, errorMsg, module);
103         }
104
105         return null;
106     }
107 }
108
Popular Tags