KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: TransactionFactory.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 package org.ofbiz.entity.transaction;
25
26 import java.sql.Connection JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import javax.transaction.TransactionManager JavaDoc;
29 import javax.transaction.UserTransaction JavaDoc;
30
31 import org.ofbiz.base.util.Debug;
32 import org.ofbiz.entity.GenericEntityException;
33 import org.ofbiz.entity.config.DatasourceInfo;
34 import org.ofbiz.entity.config.EntityConfigUtil;
35 import org.ofbiz.entity.jdbc.CursorConnection;
36
37 /**
38  * TransactionFactory - central source for JTA objects
39  *
40  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
41  * @version $Rev: 5462 $
42  * @since 2.0
43  */

44 public class TransactionFactory {
45
46     public static final String JavaDoc module = TransactionFactory.class.getName();
47     public static TransactionFactoryInterface transactionFactory = null;
48
49     public static TransactionFactoryInterface getTransactionFactory() {
50         if (transactionFactory == null) { // don't want to block here
51
synchronized (TransactionFactory.class) {
52                 // must check if null again as one of the blocked threads can still enter
53
if (transactionFactory == null) {
54                     try {
55                         String JavaDoc className = EntityConfigUtil.getTxFactoryClass();
56
57                         if (className == null) {
58                             throw new IllegalStateException JavaDoc("Could not find transaction factory class name definition");
59                         }
60                         Class JavaDoc tfClass = null;
61
62                         if (className != null && className.length() > 0) {
63                             try {
64                                 ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
65                                 tfClass = loader.loadClass(className);
66                             } catch (ClassNotFoundException JavaDoc e) {
67                                 Debug.logWarning(e, module);
68                                 throw new IllegalStateException JavaDoc("Error loading TransactionFactory class \"" + className + "\": " + e.getMessage());
69                             }
70                         }
71
72                         try {
73                             transactionFactory = (TransactionFactoryInterface) tfClass.newInstance();
74                         } catch (IllegalAccessException JavaDoc e) {
75                             Debug.logWarning(e, module);
76                             throw new IllegalStateException JavaDoc("Error loading TransactionFactory class \"" + className + "\": " + e.getMessage());
77                         } catch (InstantiationException JavaDoc e) {
78                             Debug.logWarning(e, module);
79                             throw new IllegalStateException JavaDoc("Error loading TransactionFactory class \"" + className + "\": " + e.getMessage());
80                         }
81                     } catch (SecurityException JavaDoc e) {
82                         Debug.logError(e, module);
83                         throw new IllegalStateException JavaDoc("Error loading TransactionFactory class: " + e.getMessage());
84                     }
85                 }
86             }
87         }
88         return transactionFactory;
89     }
90
91     public static TransactionManager JavaDoc getTransactionManager() {
92         return getTransactionFactory().getTransactionManager();
93     }
94
95     public static UserTransaction JavaDoc getUserTransaction() {
96         return getTransactionFactory().getUserTransaction();
97     }
98
99     public static String JavaDoc getTxMgrName() {
100         return getTransactionFactory().getTxMgrName();
101     }
102
103     public static Connection JavaDoc getConnection(String JavaDoc helperName) throws SQLException JavaDoc, GenericEntityException {
104         return getTransactionFactory().getConnection(helperName);
105     }
106
107     public static void shutdown() {
108         getTransactionFactory().shutdown();
109     }
110
111     public static Connection JavaDoc getCursorConnection(String JavaDoc helperName, Connection JavaDoc con) {
112         DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperName);
113         if (datasourceInfo == null) {
114             Debug.logWarning("Could not find configuration for " + helperName + " datasource.", module);
115             return con;
116         } else if (datasourceInfo.useProxyCursor) {
117             try {
118                 if (datasourceInfo.resultFetchSize > 1)
119                     con = CursorConnection.newCursorConnection(con, datasourceInfo.cursorName, datasourceInfo.resultFetchSize);
120             } catch (Exception JavaDoc ex) {
121                 Debug.logWarning(ex, "Error creating the cursor connection proxy " + helperName + " datasource.", module);
122             }
123         }
124         return con;
125     }
126 }
127
Popular Tags