KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: MinervaConnectionFactory.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.ofbiz.base.util.Debug;
35 import org.ofbiz.entity.GenericEntityException;
36 import org.ofbiz.minerva.pool.jdbc.xa.XAPoolDataSource;
37 import org.ofbiz.minerva.pool.jdbc.xa.wrapper.XADataSourceImpl;
38 import org.w3c.dom.Element JavaDoc;
39
40 /**
41  * MinervaConnectionFactory - Central source for Minerva JDBC Objects
42  *
43  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
44  * @version $Rev: 6609 $
45  * @since 3.0
46  */

47 public class MinervaConnectionFactory {
48         
49     public static final String JavaDoc module = MinervaConnectionFactory.class.getName();
50         
51     protected static Map JavaDoc dsCache = new HashMap JavaDoc();
52     
53     public static Connection JavaDoc getConnection(String JavaDoc helperName, Element JavaDoc jotmJdbcElement) throws SQLException JavaDoc, GenericEntityException {
54         XAPoolDataSource pds = (XAPoolDataSource) dsCache.get(helperName);
55         if (pds != null) {
56             return TransactionFactory.getCursorConnection(helperName, pds.getConnection());
57         }
58         
59         synchronized (MinervaConnectionFactory.class) {
60             pds = (XAPoolDataSource) dsCache.get(helperName);
61             if (pds != null) {
62                 return pds.getConnection();
63             } else {
64                 pds = new XAPoolDataSource();
65                 pds.setPoolName(helperName);
66             }
67
68             XADataSourceImpl ds = new XADataSourceImpl();
69
70             if (ds == null)
71                 throw new GenericEntityException("XADataSource was not created, big problem!");
72             
73             ds.setDriver(jotmJdbcElement.getAttribute("jdbc-driver"));
74             ds.setURL(jotmJdbcElement.getAttribute("jdbc-uri"));
75             
76             String JavaDoc transIso = jotmJdbcElement.getAttribute("isolation-level");
77             if (transIso != null && transIso.length() > 0) {
78                 if ("Serializable".equals(transIso)) {
79                     pds.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
80                 } else if ("RepeatableRead".equals(transIso)) {
81                     pds.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
82                 } else if ("ReadUncommitted".equals(transIso)) {
83                     pds.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
84                 } else if ("ReadCommitted".equals(transIso)) {
85                     pds.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
86                 } else if ("None".equals(transIso)) {
87                     pds.setTransactionIsolation(Connection.TRANSACTION_NONE);
88                 }
89             }
90             
91             // set the datasource in the pool
92
pds.setDataSource(ds);
93             pds.setJDBCUser(jotmJdbcElement.getAttribute("jdbc-username"));
94             pds.setJDBCPassword(jotmJdbcElement.getAttribute("jdbc-password"));
95             
96             // set the transaction manager in the pool
97
pds.setTransactionManager(TransactionFactory.getTransactionManager());
98             
99             // configure the pool settings
100
try {
101                 pds.setMaxSize(Integer.parseInt(jotmJdbcElement.getAttribute("pool-maxsize")));
102             } catch (NumberFormatException JavaDoc nfe) {
103                 Debug.logError("Problems with pool settings [pool-maxsize=" + jotmJdbcElement.getAttribute("pool-maxsize") + "]; the values MUST be numbers, using default of 20.", module);
104                 pds.setMaxSize(20);
105             } catch (Exception JavaDoc e) {
106                 Debug.logError(e, "Problems with pool settings", module);
107                 pds.setMaxSize(20);
108             }
109             try {
110                 pds.setMinSize(Integer.parseInt(jotmJdbcElement.getAttribute("pool-minsize")));
111             } catch (NumberFormatException JavaDoc nfe) {
112                 Debug.logError("Problems with pool settings [pool-minsize=" + jotmJdbcElement.getAttribute("pool-minsize") + "]; the values MUST be numbers, using default of 5.", module);
113                 pds.setMinSize(2);
114             } catch (Exception JavaDoc e) {
115                 Debug.logError(e, "Problems with pool settings", module);
116                 pds.setMinSize(2);
117             }
118                                   
119             // cache the pool
120
dsCache.put(helperName, pds);
121                                                       
122             return TransactionFactory.getCursorConnection(helperName, pds.getConnection());
123         }
124     }
125     
126     public static void closeAll() {
127         Set JavaDoc cacheKeys = dsCache.keySet();
128         Iterator JavaDoc i = cacheKeys.iterator();
129         while (i.hasNext()) {
130             String JavaDoc helperName = (String JavaDoc) i.next();
131             XAPoolDataSource pds = (XAPoolDataSource) dsCache.remove(helperName);
132             pds.close();
133         }
134     }
135 }
136
Popular Tags