KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > transaction > JTATransactionFactory


1 //$Id: JTATransactionFactory.java,v 1.5 2005/05/04 00:13:27 oneovthafew Exp $
2
package org.hibernate.transaction;
3
4 import java.util.Properties JavaDoc;
5
6 import javax.naming.InitialContext JavaDoc;
7 import javax.naming.NamingException JavaDoc;
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 /**
19  * Factory for <tt>JTATransaction</tt>.
20  *
21  * @see JTATransaction
22  * @author Gavin King
23  */

24 public class JTATransactionFactory implements TransactionFactory {
25
26     private static final Log log = LogFactory.getLog(JTATransactionFactory.class);
27     private static final String JavaDoc DEFAULT_USER_TRANSACTION_NAME = "java:comp/UserTransaction";
28
29     private InitialContext JavaDoc context;
30     private String JavaDoc utName;
31
32     public void configure(Properties JavaDoc props) throws HibernateException {
33         try {
34             context = NamingHelper.getInitialContext(props);
35         }
36         catch (NamingException JavaDoc 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 JavaDoc 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