001: /*
002: * $Id: JotmConnectionFactory.java,v 1.2 2003/08/17 04:56:27 jonesde Exp $
003: *
004: * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
005: *
006: * 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: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * 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: */
025: package org.ofbiz.entity.transaction;
026:
027: import java.sql.Connection;
028: import java.sql.SQLException;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.Map;
032: import java.util.Set;
033:
034: import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
035: import org.enhydra.jdbc.standard.StandardXADataSource;
036: import org.ofbiz.base.util.Debug;
037: import org.ofbiz.base.util.ObjectType;
038: import org.ofbiz.entity.GenericEntityException;
039: import org.w3c.dom.Element;
040:
041: /**
042: * JotmFactory - Central source for JOTM JDBC Objects
043: *
044: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
045: * @version $Revision: 1.2 $
046: * @since 2.1
047: */
048: public class JotmConnectionFactory {
049:
050: public static final String module = JotmConnectionFactory.class
051: .getName();
052:
053: protected static Map dsCache = new HashMap();
054:
055: public static Connection getConnection(String helperName,
056: Element jotmJdbcElement) throws SQLException,
057: GenericEntityException {
058: StandardXAPoolDataSource pds = (StandardXAPoolDataSource) dsCache
059: .get(helperName);
060: if (pds != null) {
061: if (Debug.verboseOn())
062: Debug.logVerbose(helperName + " pool size: "
063: + pds.pool.getCount(), module);
064: return pds.getConnection();
065: }
066:
067: synchronized (JotmConnectionFactory.class) {
068: pds = (StandardXAPoolDataSource) dsCache.get(helperName);
069: if (pds != null) {
070: return pds.getConnection();
071: }
072:
073: // the xapool wrapper class
074: String wrapperClass = jotmJdbcElement
075: .getAttribute("pool-xa-wrapper-class");
076:
077: StandardXADataSource ds = null;
078: try {
079: //ds = new StandardXADataSource();
080: ds = (StandardXADataSource) ObjectType
081: .getInstance(wrapperClass);
082: pds = new StandardXAPoolDataSource();
083: } catch (NoClassDefFoundError e) {
084: throw new GenericEntityException(
085: "Cannot find xapool.jar");
086: } catch (ClassNotFoundException e) {
087: throw new GenericEntityException(
088: "Cannot load wrapper class: " + wrapperClass, e);
089: } catch (InstantiationException e) {
090: throw new GenericEntityException(
091: "Unable to instantiate " + wrapperClass, e);
092: } catch (IllegalAccessException e) {
093: throw new GenericEntityException(
094: "Problems getting instance of " + wrapperClass,
095: e);
096: }
097:
098: if (ds == null)
099: throw new GenericEntityException(
100: "StandardXaDataSource was not created, big problem!");
101:
102: ds.setDriverName(jotmJdbcElement
103: .getAttribute("jdbc-driver"));
104: ds.setUrl(jotmJdbcElement.getAttribute("jdbc-uri"));
105: ds.setUser(jotmJdbcElement.getAttribute("jdbc-username"));
106: ds.setPassword(jotmJdbcElement
107: .getAttribute("jdbc-password"));
108: ds.setDescription(helperName);
109: ds.setTransactionManager(TransactionFactory
110: .getTransactionManager());
111:
112: String transIso = jotmJdbcElement
113: .getAttribute("isolation-level");
114: if (transIso != null && transIso.length() > 0) {
115: if ("Serializable".equals(transIso)) {
116: ((StandardXADataSource) ds)
117: .setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
118: } else if ("RepeatableRead".equals(transIso)) {
119: ((StandardXADataSource) ds)
120: .setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
121: } else if ("ReadUncommitted".equals(transIso)) {
122: ((StandardXADataSource) ds)
123: .setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
124: } else if ("ReadCommitted".equals(transIso)) {
125: ((StandardXADataSource) ds)
126: .setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
127: } else if ("None".equals(transIso)) {
128: ((StandardXADataSource) ds)
129: .setTransactionIsolation(Connection.TRANSACTION_NONE);
130: }
131: }
132:
133: // set the datasource in the pool
134: pds.setDataSource(ds);
135: pds.setDescription(ds.getDescription());
136: pds.setUser(ds.getUser());
137: pds.setPassword(ds.getPassword());
138: Debug.logInfo("XADataSource: " + ds.getClass().getName()
139: + " attached to pool.", module);
140:
141: // set the transaction manager in the pool
142: pds.setTransactionManager(TransactionFactory
143: .getTransactionManager());
144:
145: // configure the pool settings
146: try {
147: pds.setMaxSize(new Integer(jotmJdbcElement
148: .getAttribute("pool-maxsize")).intValue());
149: pds.setMinSize(new Integer(jotmJdbcElement
150: .getAttribute("pool-minsize")).intValue());
151: pds.setSleepTime(new Long(jotmJdbcElement
152: .getAttribute("pool-sleeptime")).longValue());
153: pds.setLifeTime(new Long(jotmJdbcElement
154: .getAttribute("pool-lifetime")).longValue());
155: pds.setDeadLockMaxWait(new Long(jotmJdbcElement
156: .getAttribute("pool-deadlock-maxwait"))
157: .longValue());
158: pds.setDeadLockRetryWait(new Long(jotmJdbcElement
159: .getAttribute("pool-deadlock-retrywait"))
160: .longValue());
161:
162: // set the test statement to test connections
163: String testStmt = jotmJdbcElement
164: .getAttribute("pool-jdbc-test-stmt");
165: if (testStmt != null && testStmt.length() > 0) {
166: pds.setJdbcTestStmt(testStmt);
167: Debug.logInfo("Set JDBC Test Statement : "
168: + testStmt, module);
169: }
170: } catch (NumberFormatException nfe) {
171: Debug
172: .logError(
173: nfe,
174: "Problems with pool settings; the values MUST be numbers, using defaults.",
175: module);
176: } catch (Exception e) {
177: Debug
178: .logError(e, "Problems with pool settings",
179: module);
180: }
181:
182: // cache the pool
183: dsCache.put(helperName, pds);
184:
185: return pds.getConnection();
186: }
187: }
188:
189: public static void closeAll() {
190: Set cacheKeys = dsCache.keySet();
191: Iterator i = cacheKeys.iterator();
192: while (i.hasNext()) {
193: String helperName = (String) i.next();
194: StandardXAPoolDataSource pds = (StandardXAPoolDataSource) dsCache
195: .remove(helperName);
196: pds.shutdown(true);
197: }
198: }
199: }
|