KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: XaPoolConnectionFactory.java 6609 2006-01-29 09:50:01Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * 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  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * 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  */

25 package org.ofbiz.entity.transaction;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
35 import org.enhydra.jdbc.standard.StandardXADataSource;
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.base.util.ObjectType;
38 import org.ofbiz.entity.GenericEntityException;
39 import org.w3c.dom.Element JavaDoc;
40
41 /**
42  * JotmFactory - Central source for JOTM JDBC Objects
43  *
44  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
45  * @version $Rev: 6609 $
46  * @since 2.1
47  */

48 public class XaPoolConnectionFactory {
49         
50     public static final String JavaDoc module = XaPoolConnectionFactory.class.getName();
51         
52     protected static Map JavaDoc dsCache = new HashMap JavaDoc();
53     
54     public static Connection JavaDoc getConnection(String JavaDoc helperName, Element JavaDoc jotmJdbcElement) throws SQLException JavaDoc, GenericEntityException {
55         StandardXAPoolDataSource pds = (StandardXAPoolDataSource) dsCache.get(helperName);
56         if (pds != null) {
57             if (Debug.verboseOn()) Debug.logVerbose(helperName + " pool size: " + pds.pool.getCount(), module);
58             return TransactionFactory.getCursorConnection(helperName, pds.getConnection());
59         }
60         
61         synchronized (XaPoolConnectionFactory.class) {
62             pds = (StandardXAPoolDataSource) dsCache.get(helperName);
63             if (pds != null) {
64                 return pds.getConnection();
65             }
66             
67             // the xapool wrapper class
68
String JavaDoc wrapperClass = jotmJdbcElement.getAttribute("pool-xa-wrapper-class");
69             
70             StandardXADataSource ds = null;
71             try {
72                 //ds = new StandardXADataSource();
73
ds = (StandardXADataSource) ObjectType.getInstance(wrapperClass);
74                 pds = new StandardXAPoolDataSource();
75             } catch (NoClassDefFoundError JavaDoc e) {
76                 throw new GenericEntityException("Cannot find xapool.jar");
77             } catch (ClassNotFoundException JavaDoc e) {
78                 throw new GenericEntityException("Cannot load wrapper class: " + wrapperClass, e);
79             } catch (InstantiationException JavaDoc e) {
80                 throw new GenericEntityException("Unable to instantiate " + wrapperClass, e);
81             } catch (IllegalAccessException JavaDoc e) {
82                 throw new GenericEntityException("Problems getting instance of " + wrapperClass, e);
83             }
84             
85             if (ds == null)
86                 throw new GenericEntityException("StandardXaDataSource was not created, big problem!");
87             
88             ds.setDriverName(jotmJdbcElement.getAttribute("jdbc-driver"));
89             ds.setUrl(jotmJdbcElement.getAttribute("jdbc-uri"));
90             ds.setUser(jotmJdbcElement.getAttribute("jdbc-username"));
91             ds.setPassword(jotmJdbcElement.getAttribute("jdbc-password"));
92             ds.setDescription(helperName);
93             ds.setTransactionManager(TransactionFactory.getTransactionManager());
94             
95             String JavaDoc transIso = jotmJdbcElement.getAttribute("isolation-level");
96             if (transIso != null && transIso.length() > 0) {
97                 if ("Serializable".equals(transIso)) {
98                     ((StandardXADataSource) ds).setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
99                 } else if ("RepeatableRead".equals(transIso)) {
100                     ((StandardXADataSource) ds).setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
101                 } else if ("ReadUncommitted".equals(transIso)) {
102                     ((StandardXADataSource) ds).setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
103                 } else if ("ReadCommitted".equals(transIso)) {
104                     ((StandardXADataSource) ds).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
105                 } else if ("None".equals(transIso)) {
106                     ((StandardXADataSource) ds).setTransactionIsolation(Connection.TRANSACTION_NONE);
107                 }
108             }
109             
110             // set the datasource in the pool
111
pds.setDataSource(ds);
112             pds.setDescription(ds.getDescription());
113             pds.setUser(ds.getUser());
114             pds.setPassword(ds.getPassword());
115             Debug.logInfo("XADataSource: " + ds.getClass().getName() + " attached to pool.", module);
116             
117             // set the transaction manager in the pool
118
pds.setTransactionManager(TransactionFactory.getTransactionManager());
119             
120             // configure the pool settings
121
try {
122                 pds.setMaxSize(new Integer JavaDoc(jotmJdbcElement.getAttribute("pool-maxsize")).intValue());
123                 pds.setMinSize(new Integer JavaDoc(jotmJdbcElement.getAttribute("pool-minsize")).intValue());
124                 pds.setSleepTime(new Long JavaDoc(jotmJdbcElement.getAttribute("pool-sleeptime")).longValue());
125                 pds.setLifeTime(new Long JavaDoc(jotmJdbcElement.getAttribute("pool-lifetime")).longValue());
126                 pds.setDeadLockMaxWait(new Long JavaDoc(jotmJdbcElement.getAttribute("pool-deadlock-maxwait")).longValue());
127                 pds.setDeadLockRetryWait(new Long JavaDoc(jotmJdbcElement.getAttribute("pool-deadlock-retrywait")).longValue());
128                 
129                 // set the test statement to test connections
130
String JavaDoc testStmt = jotmJdbcElement.getAttribute("pool-jdbc-test-stmt");
131                 if (testStmt != null && testStmt.length() > 0) {
132                     pds.setJdbcTestStmt(testStmt);
133                     Debug.logInfo("Set JDBC Test Statement : " + testStmt, module);
134                 }
135             } catch (NumberFormatException JavaDoc nfe) {
136                 Debug.logError(nfe, "Problems with pool settings; the values MUST be numbers, using defaults.", module);
137             } catch (Exception JavaDoc e) {
138                 Debug.logError(e, "Problems with pool settings", module);
139             }
140                                   
141             // cache the pool
142
dsCache.put(helperName, pds);
143                                                       
144             return TransactionFactory.getCursorConnection(helperName, pds.getConnection());
145         }
146     }
147     
148     public static void closeAll() {
149         Set JavaDoc cacheKeys = dsCache.keySet();
150         Iterator JavaDoc i = cacheKeys.iterator();
151         while (i.hasNext()) {
152             String JavaDoc helperName = (String JavaDoc) i.next();
153             StandardXAPoolDataSource pds = (StandardXAPoolDataSource) dsCache.remove(helperName);
154             pds.shutdown(true);
155         }
156     }
157 }
158
Popular Tags