001: /*
002: * $Id: JotmFactory.java,v 1.5 2003/10/14 02:49:51 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 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:
030: import javax.naming.NamingException;
031: import javax.transaction.TransactionManager;
032: import javax.transaction.UserTransaction;
033:
034: import org.objectweb.jotm.Jotm;
035: import org.objectweb.transaction.jta.TMService;
036: import org.ofbiz.base.util.Debug;
037: import org.ofbiz.entity.GenericEntityException;
038: import org.ofbiz.entity.config.EntityConfigUtil;
039: import org.ofbiz.entity.jdbc.ConnectionFactory;
040:
041: /**
042: * JotmFactory - Central source for JOTM JTA objects
043: *
044: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
045: * @version $Revision: 1.5 $
046: * @since 2.1
047: */
048: public class JotmFactory implements TransactionFactoryInterface {
049:
050: public static final String module = JotmFactory.class.getName();
051:
052: private static TMService jotm;
053: static {
054: try {
055: // creates an instance of JOTM with a local transaction factory which is not bound to a registry
056: jotm = new Jotm(true, false);
057: } catch (NamingException ne) {
058: Debug.logError(ne, "Problems creating JOTM instance",
059: module);
060: }
061: }
062:
063: /*
064: * @see org.ofbiz.entity.transaction.TransactionFactoryInterface#getTransactionManager()
065: */
066: public TransactionManager getTransactionManager() {
067: if (jotm != null) {
068: return jotm.getTransactionManager();
069: } else {
070: Debug
071: .logError(
072: "Cannot get TransactionManager, JOTM object is null",
073: module);
074: return null;
075: }
076: }
077:
078: /*
079: * @see org.ofbiz.entity.transaction.TransactionFactoryInterface#getUserTransaction()
080: */
081: public UserTransaction getUserTransaction() {
082: if (jotm != null) {
083: return jotm.getUserTransaction();
084: } else {
085: Debug.logError(
086: "Cannot get UserTransaction, JOTM object is null",
087: module);
088: return null;
089: }
090: }
091:
092: public String getTxMgrName() {
093: return "jotm";
094: }
095:
096: public Connection getConnection(String helperName)
097: throws SQLException, GenericEntityException {
098: EntityConfigUtil.DatasourceInfo datasourceInfo = EntityConfigUtil
099: .getDatasourceInfo(helperName);
100:
101: if (datasourceInfo != null
102: && datasourceInfo.inlineJdbcElement != null) {
103: // Use JOTM (xapool.jar) connection pooling
104: try {
105: Connection con = MinervaConnectionFactory
106: .getConnection(helperName,
107: datasourceInfo.inlineJdbcElement);
108: if (con != null)
109: return con;
110: } catch (Exception ex) {
111: Debug
112: .logError(
113: ex,
114: "JOTM is the configured transaction manager but there was an error getting a database Connection through JOTM for the "
115: + helperName
116: + " datasource. Please check your configuration, class path, etc.",
117: module);
118: }
119:
120: Connection otherCon = ConnectionFactory
121: .tryGenericConnectionSources(helperName,
122: datasourceInfo.inlineJdbcElement);
123: return otherCon;
124: } else {
125: Debug
126: .logError(
127: "JOTM is the configured transaction manager but no inline-jdbc element was specified in the "
128: + helperName
129: + " datasource. Please check your configuration",
130: module);
131: return null;
132: }
133: }
134:
135: public void shutdown() {
136: MinervaConnectionFactory.closeAll();
137: if (jotm != null) {
138: jotm.stop();
139: jotm = null;
140: }
141: }
142: }
|