1 package org.hibernate.transaction; 3 4 import java.util.Properties ; 5 6 import javax.naming.InitialContext ; 7 import javax.naming.NamingException ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.hibernate.ConnectionReleaseMode; 12 import org.hibernate.HibernateException; 13 import org.hibernate.Transaction; 14 import org.hibernate.jdbc.JDBCContext; 15 import org.hibernate.cfg.Environment; 16 import org.hibernate.util.NamingHelper; 17 18 24 public class JTATransactionFactory implements TransactionFactory { 25 26 private static final Log log = LogFactory.getLog(JTATransactionFactory.class); 27 private static final String DEFAULT_USER_TRANSACTION_NAME = "java:comp/UserTransaction"; 28 29 private InitialContext context; 30 private String utName; 31 32 public void configure(Properties props) throws HibernateException { 33 try { 34 context = NamingHelper.getInitialContext(props); 35 } 36 catch (NamingException ne) { 37 log.error("Could not obtain initial context", ne); 38 throw new HibernateException( "Could not obtain initial context", ne ); 39 } 40 41 utName = props.getProperty(Environment.USER_TRANSACTION); 42 43 if (utName==null) { 44 TransactionManagerLookup lookup = TransactionManagerLookupFactory.getTransactionManagerLookup(props); 45 if (lookup!=null) utName = lookup.getUserTransactionName(); 46 } 47 48 if (utName==null) utName = DEFAULT_USER_TRANSACTION_NAME; 49 } 50 51 public Transaction beginTransaction(JDBCContext jdbcContext, Context transactionContext) throws HibernateException { 52 JTATransaction tx = new JTATransaction(jdbcContext, transactionContext); 53 tx.begin(context, utName); 54 return tx; 55 } 56 57 public ConnectionReleaseMode getDefaultReleaseMode() { 58 return ConnectionReleaseMode.AFTER_STATEMENT; 59 } 60 61 } 62 | Popular Tags |