KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > transaction > TransactionUtil


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.transaction;
18
19 import javax.transaction.RollbackException JavaDoc;
20 import javax.transaction.Status JavaDoc;
21 import javax.transaction.UserTransaction JavaDoc;
22
23 import org.alfresco.error.AlfrescoRuntimeException;
24 import org.alfresco.service.transaction.TransactionService;
25 import org.alfresco.util.ParameterCheck;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30  * Class containing transactions helper methods and interfaces.
31  *
32  * @author Roy Wetherall
33  */

34 public class TransactionUtil
35 {
36     private static Log logger = LogFactory.getLog(TransactionUtil.class);
37
38     /**
39      * Transaction work interface.
40      * <p>
41      * This interface encapsulates a unit of work that should be done within a
42      * transaction.
43      */

44     public interface TransactionWork<Result>
45     {
46         /**
47          * Method containing the work to be done in the user transaction.
48          *
49          * @return Return the result of the operation
50          */

51         Result doWork() throws Exception JavaDoc;
52     }
53
54     /**
55      * Flush transaction.
56      */

57     public static void flush()
58     {
59         AlfrescoTransactionSupport.flush();
60     }
61     
62     /**
63      * Execute the transaction work in a user transaction
64      *
65      * @param transactionService the transaction service
66      * @param transactionWork the transaction work
67      *
68      * @throws java.lang.RuntimeException if the transaction was rolled back
69      */

70     public static <R> R executeInUserTransaction(
71             TransactionService transactionService,
72             TransactionWork<R> transactionWork)
73     {
74         return executeInTransaction(transactionService, transactionWork, false, false);
75     }
76
77     /**
78      * Execute the transaction work in a user transaction.
79      * Any current transaction will be continued.
80      *
81      * @param transactionService the transaction service
82      * @param transactionWork the transaction work
83      * @param readOnly true if the transaction should be read-only
84      *
85      * @throws java.lang.RuntimeException if the transaction was rolled back
86      */

87     public static <R> R executeInUserTransaction(
88             TransactionService transactionService,
89             TransactionWork<R> transactionWork,
90             boolean readOnly)
91     {
92         return executeInTransaction(transactionService, transactionWork, false, readOnly);
93     }
94
95     /**
96      * Execute the transaction work in a <b>writable</b>, non-propagating user transaction.
97      * Any current transaction will be suspended a new one started.
98      *
99      * @param transactionService the transaction service
100      * @param transactionWork the transaction work
101      *
102      * @throws java.lang.RuntimeException if the transaction was rolled back
103      */

104     public static <R> R executeInNonPropagatingUserTransaction(
105             TransactionService transactionService,
106             TransactionWork<R> transactionWork)
107     {
108         return executeInTransaction(transactionService, transactionWork, true, false);
109     }
110
111     /**
112      * Execute the transaction work in a non-propagating user transaction.
113      * Any current transaction will be suspended a new one started.
114      *
115      * @param transactionService the transaction service
116      * @param transactionWork the transaction work
117      * @param readOnly true if the transaction should be read-only
118      *
119      * @throws java.lang.RuntimeException if the transaction was rolled back
120      */

121     public static <R> R executeInNonPropagatingUserTransaction(
122             TransactionService transactionService,
123             TransactionWork<R> transactionWork,
124             boolean readOnly)
125     {
126         return executeInTransaction(transactionService, transactionWork, true, readOnly);
127     }
128
129     /**
130      * Execute the transaction work in a user transaction of a specified type
131      *
132      * @param transactionService the transaction service
133      * @param transactionWork the transaction work
134      * @param ignoreException indicates whether errors raised in the work are
135      * ignored or re-thrown
136      * @param nonPropagatingUserTransaction indicates whether the transaction
137      * should be non propigating or not
138      * @param readOnly true if the transaction should be read-only
139      *
140      * @throws java.lang.RuntimeException if the transaction was rolled back
141      */

142     private static <R> R executeInTransaction(
143             TransactionService transactionService,
144             TransactionWork<R> transactionWork,
145             boolean nonPropagatingUserTransaction,
146             boolean readOnly)
147     {
148         ParameterCheck.mandatory("transactionWork", transactionWork);
149
150         R result = null;
151
152         // Get the right type of user transaction
153
UserTransaction JavaDoc txn = null;
154         if (nonPropagatingUserTransaction == true)
155         {
156             txn = transactionService.getNonPropagatingUserTransaction();
157         }
158         else
159         {
160             txn = transactionService.getUserTransaction(readOnly);
161         }
162
163         try
164         {
165             // Begin the transaction, do the work and then commit the
166
// transaction
167
txn.begin();
168             result = transactionWork.doWork();
169             // rollback or commit
170
if (txn.getStatus() == Status.STATUS_MARKED_ROLLBACK)
171             {
172                 // something caused the transaction to be marked for rollback
173
txn.rollback();
174             }
175             else
176             {
177                 // transaction should still commit
178
txn.commit();
179             }
180         }
181         catch (RollbackException JavaDoc exception)
182         {
183             // commit failed
184
throw new AlfrescoRuntimeException(
185                     "Unexpected rollback of exception: \n" + exception.getMessage(),
186                     exception);
187         }
188         catch (Throwable JavaDoc exception)
189         {
190             try
191             {
192                 // Roll back the exception
193
txn.rollback();
194             }
195             catch (Throwable JavaDoc rollbackException)
196             {
197                 // just dump the exception - we are already in a failure state
198
logger.error("Error rolling back transaction", rollbackException);
199             }
200
201             // Re-throw the exception
202
if (exception instanceof RuntimeException JavaDoc)
203             {
204                 throw (RuntimeException JavaDoc) exception;
205             }
206             else
207             {
208                 throw new RuntimeException JavaDoc("Error during execution of transaction.", exception);
209             }
210         }
211
212         return result;
213     }
214 }
Popular Tags