KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: JotmFactory.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 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
30 import javax.naming.NamingException JavaDoc;
31 import javax.transaction.TransactionManager JavaDoc;
32 import javax.transaction.UserTransaction JavaDoc;
33
34 import org.objectweb.jotm.Jotm;
35 import org.objectweb.transaction.jta.TMService;
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.entity.GenericEntityException;
38 import org.ofbiz.entity.config.DatasourceInfo;
39 import org.ofbiz.entity.config.EntityConfigUtil;
40 import org.ofbiz.entity.jdbc.ConnectionFactory;
41
42 /**
43  * JotmFactory - Central source for JOTM JTA objects
44  *
45  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  * @version $Rev: 5462 $
47  * @since 2.1
48  */

49 public class JotmFactory implements TransactionFactoryInterface {
50
51     public static final String JavaDoc module = JotmFactory.class.getName();
52     
53     private static TMService jotm;
54     static {
55         try {
56             // creates an instance of JOTM with a local transaction factory which is not bound to a registry
57
jotm = new Jotm(true, false);
58         } catch (NamingException JavaDoc ne) {
59             Debug.logError(ne, "Problems creating JOTM instance", module);
60         }
61     }
62
63     /*
64      * @see org.ofbiz.entity.transaction.TransactionFactoryInterface#getTransactionManager()
65      */

66     public TransactionManager JavaDoc getTransactionManager() {
67         if (jotm != null) {
68          return jotm.getTransactionManager();
69         } else {
70             Debug.logError("Cannot get TransactionManager, JOTM object is null", module);
71             return null;
72         }
73     }
74
75     /*
76      * @see org.ofbiz.entity.transaction.TransactionFactoryInterface#getUserTransaction()
77      */

78     public UserTransaction JavaDoc getUserTransaction() {
79         if (jotm != null) {
80             return jotm.getUserTransaction();
81         } else {
82             Debug.logError("Cannot get UserTransaction, JOTM object is null", module);
83             return null;
84         }
85     }
86     
87     public String JavaDoc getTxMgrName() {
88         return "jotm";
89     }
90     
91     public Connection JavaDoc getConnection(String JavaDoc helperName) throws SQLException JavaDoc, GenericEntityException {
92         DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperName);
93
94         if (datasourceInfo != null && datasourceInfo.inlineJdbcElement != null) {
95             // Use JOTM (xapool.jar) connection pooling
96
try {
97                 Connection JavaDoc con = MinervaConnectionFactory.getConnection(helperName, datasourceInfo.inlineJdbcElement);
98                 if (con != null) return con;
99             } catch (Exception JavaDoc ex) {
100                 Debug.logError(ex, "JOTM is the configured transaction manager but there was an error getting a database Connection through JOTM for the " + helperName + " datasource. Please check your configuration, class path, etc.", module);
101             }
102         
103             Connection JavaDoc otherCon = ConnectionFactory.tryGenericConnectionSources(helperName, datasourceInfo.inlineJdbcElement);
104             return otherCon;
105         } else {
106             Debug.logError("JOTM is the configured transaction manager but no inline-jdbc element was specified in the " + helperName + " datasource. Please check your configuration", module);
107             return null;
108         }
109     }
110     
111     public void shutdown() {
112         MinervaConnectionFactory.closeAll();
113         if (jotm != null) {
114             jotm.stop();
115             jotm = null;
116         }
117     }
118 }
119
Popular Tags