001: /*
002: * $Id: MinervaConnectionFactory.java,v 1.1 2003/10/14 02:49:51 ajzeneski 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.ofbiz.base.util.Debug;
035: import org.ofbiz.entity.GenericEntityException;
036: import org.ofbiz.minerva.pool.jdbc.xa.XAPoolDataSource;
037: import org.ofbiz.minerva.pool.jdbc.xa.wrapper.XADataSourceImpl;
038: import org.w3c.dom.Element;
039:
040: /**
041: * MinervaConnectionFactory - Central source for Minerva JDBC Objects
042: *
043: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
044: * @version $Revision: 1.1 $
045: * @since 3.0
046: */
047: public class MinervaConnectionFactory {
048:
049: public static final String module = JotmConnectionFactory.class
050: .getName();
051:
052: protected static Map dsCache = new HashMap();
053:
054: public static Connection getConnection(String helperName,
055: Element jotmJdbcElement) throws SQLException,
056: GenericEntityException {
057: XAPoolDataSource pds = (XAPoolDataSource) dsCache
058: .get(helperName);
059: if (pds != null) {
060: return pds.getConnection();
061: }
062:
063: synchronized (JotmConnectionFactory.class) {
064: pds = (XAPoolDataSource) dsCache.get(helperName);
065: if (pds != null) {
066: return pds.getConnection();
067: } else {
068: pds = new XAPoolDataSource();
069: pds.setPoolName(helperName);
070: }
071:
072: // the xapool wrapper class
073: String wrapperClass = jotmJdbcElement
074: .getAttribute("pool-xa-wrapper-class");
075:
076: XADataSourceImpl ds = new XADataSourceImpl();
077:
078: /*
079: try {
080: ds = (XADataSourceImpl) ObjectType.getInstance(wrapperClass);
081: pds = new XAPoolDataSource();
082: } catch (NoClassDefFoundError e) {
083: throw new GenericEntityException("Cannot find xapool.jar");
084: } catch (ClassNotFoundException e) {
085: throw new GenericEntityException("Cannot load wrapper class: " + wrapperClass, e);
086: } catch (InstantiationException e) {
087: throw new GenericEntityException("Unable to instantiate " + wrapperClass, e);
088: } catch (IllegalAccessException e) {
089: throw new GenericEntityException("Problems getting instance of " + wrapperClass, e);
090: }
091: */
092:
093: if (ds == null)
094: throw new GenericEntityException(
095: "XADataSource was not created, big problem!");
096:
097: ds.setDriver(jotmJdbcElement.getAttribute("jdbc-driver"));
098: ds.setURL(jotmJdbcElement.getAttribute("jdbc-uri"));
099: //ds.setUser(jotmJdbcElement.getAttribute("jdbc-username"));
100: //ds.setPassword(jotmJdbcElement.getAttribute("jdbc-password"));
101: //ds.setDescription(helperName);
102: //ds.setTransactionManager(TransactionFactory.getTransactionManager());
103:
104: String transIso = jotmJdbcElement
105: .getAttribute("isolation-level");
106: if (transIso != null && transIso.length() > 0) {
107: if ("Serializable".equals(transIso)) {
108: pds
109: .setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
110: } else if ("RepeatableRead".equals(transIso)) {
111: pds
112: .setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
113: } else if ("ReadUncommitted".equals(transIso)) {
114: pds
115: .setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
116: } else if ("ReadCommitted".equals(transIso)) {
117: pds
118: .setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
119: } else if ("None".equals(transIso)) {
120: pds
121: .setTransactionIsolation(Connection.TRANSACTION_NONE);
122: }
123: }
124:
125: // set the datasource in the pool
126: pds.setDataSource(ds);
127: //pds.setDescription(ds.getDescription());
128: pds.setJDBCUser(jotmJdbcElement
129: .getAttribute("jdbc-username"));
130: pds.setJDBCPassword(jotmJdbcElement
131: .getAttribute("jdbc-password"));
132: //Debug.logInfo("XADataSource: " + ds.getClass().getName() + " attached to pool.", module);
133:
134: // set the transaction manager in the pool
135: pds.setTransactionManager(TransactionFactory
136: .getTransactionManager());
137:
138: // configure the pool settings
139: try {
140: pds.setMaxSize(new Integer(jotmJdbcElement
141: .getAttribute("pool-maxsize")).intValue());
142: pds.setMinSize(new Integer(jotmJdbcElement
143: .getAttribute("pool-minsize")).intValue());
144: //pds.setSleepTime(new Long(jotmJdbcElement.getAttribute("pool-sleeptime")).longValue());
145: //pds.setLifeTime(new Long(jotmJdbcElement.getAttribute("pool-lifetime")).longValue());
146: //pds.setDeadLockMaxWait(new Long(jotmJdbcElement.getAttribute("pool-deadlock-maxwait")).longValue());
147: //pds.setDeadLockRetryWait(new Long(jotmJdbcElement.getAttribute("pool-deadlock-retrywait")).longValue());
148:
149: // set the test statement to test connections
150: /*
151: String testStmt = jotmJdbcElement.getAttribute("pool-jdbc-test-stmt");
152: if (testStmt != null && testStmt.length() > 0) {
153: pds.setJdbcTestStmt(testStmt);
154: Debug.logInfo("Set JDBC Test Statement : " + testStmt, module);
155: }
156: */
157: } catch (NumberFormatException nfe) {
158: Debug
159: .logError(
160: nfe,
161: "Problems with pool settings; the values MUST be numbers, using defaults.",
162: module);
163: } catch (Exception e) {
164: Debug
165: .logError(e, "Problems with pool settings",
166: module);
167: }
168:
169: // cache the pool
170: dsCache.put(helperName, pds);
171:
172: return pds.getConnection();
173: }
174: }
175:
176: public static void closeAll() {
177: Set cacheKeys = dsCache.keySet();
178: Iterator i = cacheKeys.iterator();
179: while (i.hasNext()) {
180: String helperName = (String) i.next();
181: XAPoolDataSource pds = (XAPoolDataSource) dsCache
182: .remove(helperName);
183: pds.close();
184: }
185: }
186: }
|