001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.geronimo;
019:
020: import java.sql.Connection;
021: import java.sql.SQLException;
022: import java.util.Collection;
023:
024: import javax.transaction.TransactionManager;
025: import javax.transaction.UserTransaction;
026: import javax.transaction.xa.XAException;
027:
028: import org.apache.geronimo.transaction.ExtendedTransactionManager;
029: import org.apache.geronimo.transaction.context.GeronimoTransactionManager;
030: import org.apache.geronimo.transaction.context.TransactionContextManager;
031: import org.apache.geronimo.transaction.log.UnrecoverableLog;
032: import org.apache.geronimo.transaction.manager.TransactionLog;
033: import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
034: import org.apache.geronimo.transaction.manager.XidImporter;
035: import org.ofbiz.base.util.Debug;
036: import org.ofbiz.entity.GenericEntityException;
037: import org.ofbiz.entity.config.DatasourceInfo;
038: import org.ofbiz.entity.config.EntityConfigUtil;
039: import org.ofbiz.entity.jdbc.ConnectionFactory;
040: import org.ofbiz.entity.transaction.MinervaConnectionFactory;
041: import org.ofbiz.entity.transaction.TransactionFactoryInterface;
042:
043: /**
044: * GeronimoTransactionFactory
045: */
046: public class GeronimoTransactionFactory implements
047: TransactionFactoryInterface {
048:
049: public static final String module = GeronimoTransactionFactory.class
050: .getName();
051:
052: // just use the transactionManager for this private static XidImporter xidImporter;
053: private static ExtendedTransactionManager transactionManager;
054: private static TransactionContextManager transactionContextManager;
055: private static int defaultTransactionTimeoutSeconds = 60;
056: private static TransactionLog transactionLog;
057: private static Collection resourceManagers = null;
058: private static GeronimoTransactionManager geronimoTransactionManager;
059:
060: static {
061: // creates an instance of Geronimo transaction context, etc with a local transaction factory which is not bound to a registry
062: try {
063: transactionLog = new UnrecoverableLog();
064: transactionManager = new TransactionManagerImpl(
065: defaultTransactionTimeoutSeconds, transactionLog,
066: resourceManagers);
067: transactionContextManager = new TransactionContextManager(
068: transactionManager,
069: (XidImporter) transactionManager);
070: geronimoTransactionManager = new GeronimoTransactionManager(
071: transactionContextManager);
072: } catch (XAException e) {
073: Debug.logError(e,
074: "Error initializing Geronimo transaction manager: "
075: + e.toString(), module);
076: }
077: }
078:
079: /*
080: * @see org.ofbiz.entity.transaction.TransactionFactoryInterface#getTransactionManager()
081: */
082: public TransactionManager getTransactionManager() {
083: return geronimoTransactionManager;
084: }
085:
086: /*
087: * @see org.ofbiz.entity.transaction.TransactionFactoryInterface#getUserTransaction()
088: */
089: public UserTransaction getUserTransaction() {
090: return geronimoTransactionManager;
091: }
092:
093: public String getTxMgrName() {
094: return "geronimo";
095: }
096:
097: public Connection getConnection(String helperName)
098: throws SQLException, GenericEntityException {
099: DatasourceInfo datasourceInfo = EntityConfigUtil
100: .getDatasourceInfo(helperName);
101:
102: if (datasourceInfo != null
103: && datasourceInfo.inlineJdbcElement != null) {
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: "Geronimo is the configured transaction manager but there was an error getting a database Connection through Geronimo 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: "Geronimo 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 (transactionContextManager != null) {
138: // TODO: need to do anything for this?
139: transactionContextManager = null;
140: }
141: }
142: }
|