1 /* 2 * Copyright 2002-2008 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.transaction.jta; 18 19 import javax.naming.NamingException; 20 import javax.transaction.SystemException; 21 22 import org.objectweb.jotm.Current; 23 import org.objectweb.jotm.Jotm; 24 25 import org.springframework.beans.factory.DisposableBean; 26 import org.springframework.beans.factory.FactoryBean; 27 28 /** 29 * {@link FactoryBean} that retrieves the JTA UserTransaction/TransactionManager 30 * for ObjectWeb's <a href="http://jotm.objectweb.org">JOTM</a>. Will retrieve 31 * an already active JOTM instance if found (e.g. if running in JOnAS), 32 * else create a new local JOTM instance. 33 * 34 * <p>With JOTM, the same object implements both the 35 * {@link javax.transaction.UserTransaction} and the 36 * {@link javax.transaction.TransactionManager} interface, 37 * as returned by this FactoryBean. 38 * 39 * <p>A local JOTM instance is well-suited for working in conjunction with 40 * ObjectWeb's <a href="http://xapool.experlog.com">XAPool</a>, e.g. with bean 41 * definitions like the following: 42 * 43 * <pre class="code"> 44 * <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/> 45 * 46 * <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> 47 * <property name="userTransaction" ref="jotm"/> 48 * </bean> 49 * 50 * <bean id="innerDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown"> 51 * <property name="transactionManager" ref="jotm"/> 52 * <property name="driverName" value="..."/> 53 * <property name="url" value="..."/> 54 * <property name="user" value="..."/> 55 * <property name="password" value="..."/> 56 * </bean> 57 * 58 * <bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown"> 59 * <property name="dataSource" ref="innerDataSource"/> 60 * <property name="user" value="..."/> 61 * <property name="password" value="..."/> 62 * <property name="maxSize" value="..."/> 63 * </bean></pre> 64 * 65 * Note that Spring's {@link JtaTransactionManager} will automatically detect 66 * that the passed-in UserTransaction reference also implements the 67 * TransactionManager interface. Hence, it is not necessary to specify a 68 * separate reference for JtaTransactionManager's "transactionManager" property. 69 * 70 * <p>Implementation note: This FactoryBean uses JOTM's static access method 71 * to obtain the JOTM {@link org.objectweb.jotm.Current} object, which 72 * implements both the UserTransaction and the TransactionManager interface, 73 * as mentioned above. 74 * 75 * @author Juergen Hoeller 76 * @since 21.01.2004 77 * @see JtaTransactionManager#setUserTransaction 78 * @see JtaTransactionManager#setTransactionManager 79 * @see org.objectweb.jotm.Current 80 */ 81 public class JotmFactoryBean implements FactoryBean, DisposableBean { 82 83 private Current jotmCurrent; 84 85 private Jotm jotm; 86 87 88 public JotmFactoryBean() throws NamingException { 89 // Check for already active JOTM instance. 90 this.jotmCurrent = Current.getCurrent(); 91 92 // If none found, create new local JOTM instance. 93 if (this.jotmCurrent == null) { 94 // Only for use within the current Spring context: 95 // local, not bound to registry. 96 this.jotm = new Jotm(true, false); 97 this.jotmCurrent = Current.getCurrent(); 98 } 99 } 100 101 /** 102 * Set the default transaction timeout for the JOTM instance. 103 * <p>Should only be called for a local JOTM instance, 104 * not when accessing an existing (shared) JOTM instance. 105 */ 106 public void setDefaultTimeout(int defaultTimeout) { 107 this.jotmCurrent.setDefaultTimeout(defaultTimeout); 108 // The following is a JOTM oddity: should be used for demarcation transaction only, 109 // but is required here in order to actually get rid of JOTM's default (60 seconds). 110 try { 111 this.jotmCurrent.setTransactionTimeout(defaultTimeout); 112 } 113 catch (SystemException ex) { 114 // should never happen 115 } 116 } 117 118 119 /** 120 * Return the JOTM instance created by this factory bean, if any. 121 * Will be <code>null</code> if an already active JOTM instance is used. 122 * <p>Application code should never need to access this. 123 */ 124 public Jotm getJotm() { 125 return this.jotm; 126 } 127 128 public Object getObject() { 129 return this.jotmCurrent; 130 } 131 132 public Class getObjectType() { 133 return this.jotmCurrent.getClass(); 134 } 135 136 public boolean isSingleton() { 137 return true; 138 } 139 140 141 /** 142 * Stop the local JOTM instance, if created by this FactoryBean. 143 */ 144 public void destroy() { 145 if (this.jotm != null) { 146 this.jotm.stop(); 147 } 148 } 149 150 }