KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > dods > xa > XATransactionFactory


1 package org.enhydra.dods.xa;
2
3 import java.sql.SQLException JavaDoc;
4 import java.util.Hashtable JavaDoc;
5
6 import javax.naming.InitialContext JavaDoc;
7 import javax.transaction.Transaction JavaDoc;
8
9 import org.enhydra.dods.CommonConstants;
10 import org.enhydra.dods.DODS;
11
12 import com.lutris.appserver.server.sql.AbstractDBTransactionFactory;
13 import com.lutris.appserver.server.sql.DBConnection;
14 import com.lutris.appserver.server.sql.DBTransaction;
15 import com.lutris.appserver.server.sql.DBTransactionFactoryCreator;
16 import com.lutris.appserver.server.sql.LogicalDatabase;
17 import com.lutris.appserver.server.sql.standard.DatabaseConfiguration;
18 import com.lutris.appserver.server.sql.standard.StandardLogicalDatabase;
19 import com.lutris.logging.Logger;
20
21 /**
22  * An AbstractDBTransactionFactory implementation which creates
23  * DBTransactions compatible (and bound into J2EE transaction manager).
24  *
25  * @author V.Puskas
26  * @version 0.11
27  */

28
29
30 public class XATransactionFactory implements AbstractDBTransactionFactory {
31
32     /**
33      *
34      */

35     AbstractDBTransactionFactory adbtf;
36     
37     Hashtable JavaDoc dodsTransactions = new Hashtable JavaDoc(128);
38
39     public static final int XA_INGORE = 0;
40     public static final int XA_WARN = 1;
41     public static final int XA_WARN_WITH_TRACE = 2;
42     public static final int XA_THROW_EXCEPTION = 3;
43     public static final int XA_THROW_ERROR = 4;
44     
45     
46     private boolean configured = false;
47     private int defaultTimeout = CommonConstants.DEFAULT_XA_DEFAULT_TIMEOUT;
48     private int xaUsageCase = XA_INGORE;
49     private String JavaDoc strTransactionManagerLookupName;
50     private String JavaDoc strWrappedImplFactory;
51
52     /**
53      *
54      */

55     private LogicalDatabase logicalDb = null;
56
57     /**
58      *
59      */

60     public void setLogicalDb(LogicalDatabase ldb) {
61         logicalDb = ldb;
62     }
63
64     /**
65      *
66      */

67     public LogicalDatabase getLogicalDb() {
68         return logicalDb;
69     }
70 ;
71
72     /**
73      *
74      */

75     public int getDefaultTimeout() {
76         return defaultTimeout;
77     }
78
79     
80
81     public DBTransaction getTransaction(DBConnection conn) throws SQLException JavaDoc {
82         if(!configured) {
83             try {
84                 DatabaseConfiguration dbco = ((StandardLogicalDatabase)getLogicalDb()).getDatabaseConfiguration();
85                 defaultTimeout = dbco.getXaDefaultTimeout();
86                 strTransactionManagerLookupName = dbco.getXaTransactonManagerLookupName();
87                 strWrappedImplFactory = dbco.getXaWrappedTransImplFactory();
88                 xaUsageCase = dbco.getXaUsageCase();
89                 configured = true;
90             } catch (Exception JavaDoc e) {
91                 DODS.getLogChannel().write(Logger.CRITICAL,"Creating XATransactionFactory faild" );
92                 e.printStackTrace();
93             }
94         }
95         synchronized (XATransactionFactory.class) {
96             if (null == adbtf) {
97                 adbtf = DBTransactionFactoryCreator.getDBTransactionFactory(strWrappedImplFactory,getLogicalDb());
98             }
99         }
100         Transaction JavaDoc xaTrans = null;
101         try {
102             InitialContext JavaDoc ictx = new InitialContext JavaDoc();
103             javax.transaction.TransactionManager JavaDoc tm = (javax.transaction.TransactionManager JavaDoc) ictx.lookup(strTransactionManagerLookupName);
104             tm.setTransactionTimeout(defaultTimeout);
105             xaTrans = tm.getTransaction();
106         } catch (Exception JavaDoc e) {
107             e.printStackTrace();
108             throw new SQLException JavaDoc(e.getMessage());
109         }
110         XATransaction ret = new XATransaction(xaTrans,adbtf,conn,xaUsageCase,defaultTimeout,dodsTransactions);
111         try {
112             xaTrans.enlistResource(ret);
113         } catch (Exception JavaDoc e) {
114             e.printStackTrace();
115             throw new SQLException JavaDoc(e.getMessage());
116         }
117         return ret;
118     }
119
120 }
121
Popular Tags