KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > transaction > context > OnlineUserTransaction


1 package org.apache.geronimo.transaction.context;
2
3 import java.io.Serializable JavaDoc;
4 import javax.resource.ResourceException JavaDoc;
5 import javax.transaction.HeuristicMixedException JavaDoc;
6 import javax.transaction.HeuristicRollbackException JavaDoc;
7 import javax.transaction.InvalidTransactionException JavaDoc;
8 import javax.transaction.NotSupportedException JavaDoc;
9 import javax.transaction.RollbackException JavaDoc;
10 import javax.transaction.SystemException JavaDoc;
11 import javax.transaction.UserTransaction JavaDoc;
12
13 import org.apache.geronimo.transaction.TrackedConnectionAssociator;
14
15 public final class OnlineUserTransaction implements UserTransaction JavaDoc, Serializable JavaDoc {
16     private transient TransactionContextManager transactionContextManager;
17     private transient TrackedConnectionAssociator trackedConnectionAssociator;
18
19     boolean isActive() {
20         return transactionContextManager != null;
21     }
22
23     public void setUp(TransactionContextManager transactionContextManager, TrackedConnectionAssociator trackedConnectionAssociator) {
24         this.transactionContextManager = transactionContextManager;
25         this.trackedConnectionAssociator = trackedConnectionAssociator;
26     }
27
28
29     public int getStatus() throws SystemException JavaDoc {
30         return transactionContextManager.getStatus();
31     }
32
33     public void setRollbackOnly() throws IllegalStateException JavaDoc, SystemException JavaDoc {
34         transactionContextManager.setRollbackOnly();
35     }
36
37     public void setTransactionTimeout(int seconds) throws SystemException JavaDoc {
38         if (seconds < 0) {
39             throw new SystemException JavaDoc("transaction timeout must be positive or 0, not " + seconds);
40         }
41         transactionContextManager.setTransactionTimeout(seconds);
42     }
43
44     public void begin() throws NotSupportedException JavaDoc, SystemException JavaDoc {
45         transactionContextManager.newBeanTransactionContext(0L);
46
47         if(trackedConnectionAssociator != null) {
48             try {
49                 trackedConnectionAssociator.newTransaction();
50             } catch (ResourceException JavaDoc e) {
51                 throw (SystemException JavaDoc)new SystemException JavaDoc().initCause(e);
52             }
53         }
54     }
55
56     public void commit() throws HeuristicMixedException JavaDoc, HeuristicRollbackException JavaDoc, IllegalStateException JavaDoc, RollbackException JavaDoc, SecurityException JavaDoc, SystemException JavaDoc {
57         TransactionContext ctx = transactionContextManager.getContext();
58         if (ctx instanceof BeanTransactionContext == false) {
59             throw new IllegalStateException JavaDoc("Transaction has not been started");
60         }
61         BeanTransactionContext beanContext = (BeanTransactionContext) ctx;
62         try {
63             if (!beanContext.commit()) {
64                 throw new RollbackException JavaDoc();
65             }
66         } finally {
67             TransactionContext oldContext = beanContext.getOldContext();
68             transactionContextManager.setContext(oldContext);
69             try {
70                 oldContext.resume();
71             } catch (InvalidTransactionException JavaDoc e) {
72                 throw (SystemException JavaDoc)new SystemException JavaDoc("Unable to resume perexisting transaction context").initCause(e);
73             }
74         }
75     }
76
77     public void rollback() throws IllegalStateException JavaDoc, SecurityException JavaDoc, SystemException JavaDoc {
78         TransactionContext ctx = transactionContextManager.getContext();
79         if (ctx instanceof BeanTransactionContext == false) {
80             throw new IllegalStateException JavaDoc("Transaction has not been started");
81         }
82         BeanTransactionContext beanContext = (BeanTransactionContext) ctx;
83         try {
84             beanContext.rollback();
85         } finally {
86             TransactionContext oldContext = beanContext.getOldContext();
87             transactionContextManager.setContext(oldContext);
88             try {
89                 oldContext.resume();
90             } catch (InvalidTransactionException JavaDoc e) {
91                 throw (SystemException JavaDoc)new SystemException JavaDoc("Unable to resume perexisting transaction context").initCause(e);
92             }
93         }
94     }
95 }
96
Popular Tags