1 /*
2 * Copyright 2003 - 2013 The eFaps Team
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Revision: $Rev$
17 * Last Changed: $Date$
18 * Last Changed By: $Author$
19 */
20
21 package org.efaps.db.transaction;
22
23 import javax.transaction.HeuristicMixedException;
24 import javax.transaction.HeuristicRollbackException;
25 import javax.transaction.NotSupportedException;
26 import javax.transaction.RollbackException;
27 import javax.transaction.SystemException;
28 import javax.transaction.TransactionManager;
29 import javax.transaction.UserTransaction;
30
31 import org.efaps.db.Context;
32 import org.efaps.util.EFapsException;
33
34 /**
35 * Delegate a UserTransaction to the TransactionManager used by the Context.
36 *
37 * @author The eFaps Team
38 * @version $Id$
39 */
40 public class DelegatingUserTransaction
41 implements UserTransaction
42 {
43 /**
44 * Username of the transaction.
45 */
46 private String userName;
47
48 /**
49 * TransactionManager.
50 */
51 private final TransactionManager transmanager;
52
53 /**
54 * Constructor.
55 *
56 * @param _manager TransactionManager
57 */
58 public DelegatingUserTransaction(final TransactionManager _manager)
59 {
60 this.transmanager = _manager;
61 }
62
63 /**
64 * @see javax.transaction.UserTransaction#begin()
65 * @throws NotSupportedException on error
66 * @throws SystemException on error
67 */
68 @Override
69 public void begin()
70 throws NotSupportedException, SystemException
71 {
72 try {
73 Context.begin(getUserName(), false);
74 } catch (final EFapsException e) {
75 try {
76 if (Context.isTMActive()) {
77 Context.getThreadContext().close();
78 }
79 } catch (final EFapsException e1) {
80 throw new SystemException(e1.getMessage());
81 }
82 throw new SystemException(e.getMessage());
83 }
84 }
85
86 /**
87 * Commit.
88 *
89 * @see javax.transaction.UserTransaction#commit()
90 * @throws HeuristicMixedException on error
91 * @throws HeuristicRollbackException on error
92 * @throws RollbackException on error
93 * @throws SystemException on error
94 */
95 @Override
96 public void commit()
97 throws HeuristicMixedException,
98 HeuristicRollbackException,
99 RollbackException,
100 SystemException
101 {
102 try {
103 if (!Context.isTMNoTransaction()) {
104 if (Context.isTMActive()) {
105 Context.commit();
106 } else {
107 Context.rollback();
108 }
109 }
110 } catch (final EFapsException e) {
111 throw new SystemException(e.getMessage());
112 }
113 }
114
115 /**
116 * @see javax.transaction.UserTransaction#getStatus()
117 * @return Status of the TransactionManager.
118 * @throws SystemException on error
119 */
120 @Override
121 public int getStatus()
122 throws SystemException
123 {
124 return this.transmanager.getStatus();
125 }
126
127 /**
128 * Rollback.
129 *
130 * @see javax.transaction.UserTransaction#rollback()
131 * @throws SystemException on error
132 */
133 @Override
134 public void rollback()
135 throws SystemException
136 {
137 try {
138 Context.rollback();
139 } catch (final EFapsException e) {
140 throw new SystemException(e.getMessage());
141 }
142 }
143
144 /**
145 * Set rollback.
146 *
147 * @see javax.transaction.UserTransaction#setRollbackOnly()
148 * @throws SystemException on error
149 */
150 @Override
151 public void setRollbackOnly()
152 throws SystemException
153 {
154 this.transmanager.setRollbackOnly();
155 }
156
157 /**
158 * @see javax.transaction.UserTransaction#setTransactionTimeout(int)
159 * @param _timeOut TimeOut
160 * @throws SystemException on error
161 */
162 @Override
163 public void setTransactionTimeout(final int _timeOut)
164 throws SystemException
165 {
166 this.transmanager.setTransactionTimeout(_timeOut);
167 }
168
169 /**
170 * @return the userName
171 */
172 public String getUserName()
173 {
174 return this.userName;
175 }
176
177 /**
178 * @param _userName the userName to set
179 */
180 public void setUserName(final String _userName)
181 {
182 this.userName = _userName;
183 }
184 }