001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jca;
031:
032: import com.caucho.log.Log;
033: import com.caucho.transaction.TransactionManagerImpl;
034: import com.caucho.util.L10N;
035: import com.caucho.webbeans.component.*;
036:
037: import javax.transaction.*;
038: import javax.transaction.xa.*;
039: import java.util.logging.Logger;
040:
041: /**
042: * Implementation of the UserTransactionImpl for a thread instance.
043: */
044: public class UserTransactionProxy implements UserTransaction,
045: TransactionManager, java.io.Serializable {
046: private static final Logger log = Logger
047: .getLogger(UserTransactionProxy.class.getName());
048: private static final L10N L = new L10N(UserTransactionProxy.class);
049:
050: /*
051: private static final EnvironmentLocal<UserTransactionProxy> _localUT =
052: new EnvironmentLocal<UserTransactionProxy>();
053: */
054: private static final UserTransactionProxy _proxy = new UserTransactionProxy();
055:
056: private static final ThreadLocal<UserTransactionImpl> _threadTransaction = new ThreadLocal<UserTransactionImpl>();
057:
058: /**
059: * Creates the proxy.
060: */
061: private UserTransactionProxy() {
062: /*
063: if (_localUT.getLevel() == null)
064: _localUT.set(this);
065: */
066: }
067:
068: /**
069: * Returns the local UT proxy.
070: */
071: public static UserTransactionProxy getInstance() {
072: //return _localUT.get();
073: return _proxy;
074: }
075:
076: /**
077: * Gets the thread transaction.
078: */
079: public UserTransactionImpl getUserTransaction() {
080: UserTransactionImpl xa = _threadTransaction.get();
081:
082: if (xa == null) {
083: xa = new UserTransactionImpl(TransactionManagerImpl
084: .getLocal());
085: _threadTransaction.set(xa);
086: }
087:
088: return xa;
089: }
090:
091: /**
092: * Sets the transaction's timeout.
093: */
094: public void setTransactionTimeout(int seconds)
095: throws SystemException {
096: getUserTransaction().setTransactionTimeout(seconds);
097: }
098:
099: /**
100: * Gets the transaction's status
101: */
102: public int getStatus() throws SystemException {
103: return getUserTransaction().getStatus();
104: }
105:
106: /**
107: * Start the transaction.
108: */
109: public void begin() throws NotSupportedException, SystemException {
110: getUserTransaction().begin();
111: }
112:
113: /**
114: * Marks the transaction as rollback only.
115: */
116: public void setRollbackOnly() throws IllegalStateException,
117: SystemException {
118: getUserTransaction().setRollbackOnly();
119: }
120:
121: /**
122: * Marks the transaction as rollback only.
123: */
124: public void setRollbackOnly(Exception e)
125: throws IllegalStateException {
126: getUserTransaction().setRollbackOnly(e);
127: }
128:
129: /**
130: * Commits the transaction
131: */
132: public void commit() throws IllegalStateException,
133: RollbackException, HeuristicMixedException,
134: HeuristicRollbackException, SecurityException,
135: SystemException {
136: getUserTransaction().commit();
137: }
138:
139: /**
140: * Rolls the transaction back
141: */
142: public void rollback() throws IllegalStateException,
143: SecurityException, SystemException {
144: getUserTransaction().rollback();
145: }
146:
147: /**
148: * Enlist a begin resource
149: */
150: public void enlistBeginResource(BeginResource resource)
151: throws IllegalStateException {
152: getUserTransaction().enlistBeginResource(resource);
153: }
154:
155: /**
156: * Enlist a close resource
157: */
158: public void enlistCloseResource(CloseResource resource)
159: throws IllegalStateException {
160: getUserTransaction().enlistCloseResource(resource);
161: }
162:
163: /**
164: * Finish any transaction.
165: */
166: public void abortTransaction() throws IllegalStateException {
167: UserTransactionImpl xa = _threadTransaction.get();
168:
169: if (xa == null)
170: return;
171:
172: xa.abortTransaction();
173: }
174:
175: /**
176: * Recovers an XAResource
177: */
178: void recover(XAResource xaRes) throws XAException {
179: TransactionManagerImpl.getLocal().recover(xaRes);
180: }
181:
182: //
183: // TransactionManager compatibility. These should not be used by
184: // application code.
185: //
186:
187: /**
188: * Returns the current transaction.
189: */
190: public Transaction getTransaction() throws SystemException {
191: return TransactionManagerImpl.getLocal().getTransaction();
192: }
193:
194: /**
195: * Suspends the transaction.
196: */
197: public Transaction suspend() throws SystemException {
198: return TransactionManagerImpl.getLocal().suspend();
199: }
200:
201: /**
202: * Resume a transaction.
203: */
204: public void resume(Transaction transaction)
205: throws IllegalStateException, InvalidTransactionException,
206: SystemException {
207: TransactionManagerImpl.getLocal().resume(transaction);
208: }
209:
210: /**
211: * Serialization handle
212: */
213: private Object writeReplace() {
214: return new WebBeansHandle(UserTransaction.class);
215: }
216:
217: public String toString() {
218: return "UserTransactionProxy[]";
219: }
220: }
|