KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jca > UserTransactionProxy


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jca;
31
32 import com.caucho.log.Log;
33 import com.caucho.transaction.TransactionManagerImpl;
34 import com.caucho.util.L10N;
35
36 import javax.transaction.HeuristicMixedException JavaDoc;
37 import javax.transaction.HeuristicRollbackException JavaDoc;
38 import javax.transaction.NotSupportedException JavaDoc;
39 import javax.transaction.RollbackException JavaDoc;
40 import javax.transaction.SystemException JavaDoc;
41 import javax.transaction.UserTransaction JavaDoc;
42 import javax.transaction.xa.XAException JavaDoc;
43 import javax.transaction.xa.XAResource JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  * Implementation of the UserTransactionImpl for a thread instance.
48  */

49 public class UserTransactionProxy implements UserTransaction JavaDoc {
50   private static final Logger JavaDoc log = Log.open(UserTransactionProxy.class);
51   private static final L10N L = new L10N(UserTransactionProxy.class);
52
53   /*
54   private static final EnvironmentLocal<UserTransactionProxy> _localUT =
55     new EnvironmentLocal<UserTransactionProxy>();
56   */

57   private static final UserTransactionProxy _proxy
58     = new UserTransactionProxy();
59
60   private static final ThreadLocal JavaDoc<UserTransactionImpl> _threadTransaction
61     = new ThreadLocal JavaDoc<UserTransactionImpl>();
62
63   /**
64    * Creates the proxy.
65    */

66   private UserTransactionProxy()
67   {
68     /*
69     if (_localUT.getLevel() == null)
70       _localUT.set(this);
71     */

72   }
73
74   /**
75    * Returns the local UT proxy.
76    */

77   public static UserTransactionProxy getInstance()
78   {
79     //return _localUT.get();
80
return _proxy;
81   }
82
83   /**
84    * Gets the thread transaction.
85    */

86   public UserTransactionImpl getTransaction()
87   {
88     UserTransactionImpl xa = _threadTransaction.get();
89
90     if (xa == null) {
91       xa = new UserTransactionImpl(TransactionManagerImpl.getLocal());
92       _threadTransaction.set(xa);
93     }
94
95     return xa;
96   }
97   
98   /**
99    * Sets the transaction's timeout.
100    */

101   public void setTransactionTimeout(int seconds)
102     throws SystemException JavaDoc
103   {
104     getTransaction().setTransactionTimeout(seconds);
105   }
106   
107   /**
108    * Gets the transaction's status
109    */

110   public int getStatus()
111     throws SystemException JavaDoc
112   {
113     return getTransaction().getStatus();
114   }
115   
116   /**
117    * Start the transaction.
118    */

119   public void begin()
120     throws NotSupportedException JavaDoc, SystemException JavaDoc
121   {
122     getTransaction().begin();
123   }
124   
125   /**
126    * Marks the transaction as rollback only.
127    */

128   public void setRollbackOnly()
129     throws IllegalStateException JavaDoc, SystemException JavaDoc
130   {
131     getTransaction().setRollbackOnly();
132   }
133   
134   /**
135    * Commits the transaction
136    */

137   public void commit()
138     throws IllegalStateException JavaDoc, RollbackException JavaDoc, HeuristicMixedException JavaDoc,
139        HeuristicRollbackException JavaDoc, SecurityException JavaDoc, SystemException JavaDoc
140   {
141     getTransaction().commit();
142   }
143   
144   /**
145    * Rolls the transaction back
146    */

147   public void rollback()
148     throws IllegalStateException JavaDoc, SecurityException JavaDoc, SystemException JavaDoc
149   {
150     getTransaction().rollback();
151   }
152
153   /**
154    * Enlist a begin resource
155    */

156   public void enlistBeginResource(BeginResource resource)
157     throws IllegalStateException JavaDoc
158   {
159     getTransaction().enlistBeginResource(resource);
160   }
161
162   /**
163    * Enlist a close resource
164    */

165   public void enlistCloseResource(CloseResource resource)
166     throws IllegalStateException JavaDoc
167   {
168     getTransaction().enlistCloseResource(resource);
169   }
170
171   /**
172    * Finish any transaction.
173    */

174   public void abortTransaction()
175     throws IllegalStateException JavaDoc
176   {
177     UserTransactionImpl xa = _threadTransaction.get();
178
179     if (xa == null)
180       return;
181     
182     xa.abortTransaction();
183   }
184
185   /**
186    * Recovers an XAResource
187    */

188   void recover(XAResource JavaDoc xaRes)
189     throws XAException JavaDoc
190   {
191     TransactionManagerImpl.getLocal().recover(xaRes);
192   }
193
194   public String JavaDoc toString()
195   {
196     return "UserTransactionProxy[]";
197   }
198 }
199
200
Popular Tags