KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transaction > TransactionTemplate


1 /*
2  * $Id: TransactionTemplate.java 4259 2006-12-14 03:12:07Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.transaction;
12
13 import java.beans.ExceptionListener JavaDoc;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.mule.config.i18n.Message;
18 import org.mule.config.i18n.Messages;
19 import org.mule.umo.UMOTransaction;
20 import org.mule.umo.UMOTransactionConfig;
21
22 public class TransactionTemplate
23 {
24     private static final Log logger = LogFactory.getLog(TransactionTemplate.class);
25
26     private final UMOTransactionConfig config;
27     private final ExceptionListener JavaDoc exceptionListener;
28
29     public TransactionTemplate(UMOTransactionConfig config, ExceptionListener JavaDoc listener)
30     {
31         this.config = config;
32         exceptionListener = listener;
33     }
34
35     public Object JavaDoc execute(TransactionCallback callback) throws Exception JavaDoc
36     {
37         if (config == null)
38         {
39             return callback.doInTransaction();
40         }
41         else
42         {
43             byte action = config.getAction();
44             UMOTransaction tx = TransactionCoordination.getInstance().getTransaction();
45
46             if (action == UMOTransactionConfig.ACTION_NONE && tx != null)
47             {
48                 throw new IllegalTransactionStateException(new Message(Messages.TX_AVAILABLE_BUT_ACTION_IS_X,
49                     "None"));
50             }
51             else if (action == UMOTransactionConfig.ACTION_ALWAYS_BEGIN && tx != null)
52             {
53                 throw new IllegalTransactionStateException(new Message(Messages.TX_AVAILABLE_BUT_ACTION_IS_X,
54                     "Always Begin"));
55             }
56             else if (action == UMOTransactionConfig.ACTION_ALWAYS_JOIN && tx == null)
57             {
58                 throw new IllegalTransactionStateException(new Message(
59                     Messages.TX_NOT_AVAILABLE_BUT_ACTION_IS_X, "Always Join"));
60             }
61
62             if (action == UMOTransactionConfig.ACTION_ALWAYS_BEGIN
63                             || action == UMOTransactionConfig.ACTION_BEGIN_OR_JOIN)
64             {
65                 logger.debug("Beginning transaction");
66                 tx = config.getFactory().beginTransaction();
67                 logger.debug("Transaction successfully started");
68             }
69             else
70             {
71                 tx = null;
72             }
73             try
74             {
75                 Object JavaDoc result = callback.doInTransaction();
76                 if (tx != null)
77                 {
78                     if (tx.isRollbackOnly())
79                     {
80                         logger.debug("Transaction is marked for rollback");
81                         tx.rollback();
82                     }
83                     else
84                     {
85                         logger.debug("Committing transaction");
86                         tx.commit();
87                     }
88                 }
89                 return result;
90             }
91             catch (Exception JavaDoc e)
92             {
93                 if (exceptionListener != null)
94                 {
95                     logger
96                         .info("Exception Caught in Transaction template. Handing off to exception handler: "
97                                         + exceptionListener);
98                     exceptionListener.exceptionThrown(e);
99                 }
100                 else
101                 {
102                     logger
103                         .info("Exception Caught in Transaction template without any exception listeners defined, exception is rethrown.");
104                     if (tx != null)
105                     {
106                         tx.setRollbackOnly();
107                     }
108                 }
109                 if (tx != null)
110                 {
111                     // The exception strategy can choose to route exception
112
// messages
113
// as part of the current transaction. So only rollback the
114
// tx
115
// if it has been marked for rollback (which is the default
116
// case in the
117
// AbstractExceptionListener)
118
if (tx.isRollbackOnly())
119                     {
120                         logger.debug("Exception caught: rollback transaction", e);
121                         tx.rollback();
122                     }
123                     else
124                     {
125                         tx.commit();
126                     }
127                 }
128                 // we've handled this exception above. just return null now
129
if (exceptionListener != null)
130                 {
131                     return null;
132                 }
133                 else
134                 {
135                     throw e;
136                 }
137             }
138             catch (Error JavaDoc e)
139             {
140                 if (tx != null)
141                 {
142                     logger.info("Error caught: rollback transaction", e);
143                     tx.rollback();
144                 }
145                 throw e;
146             }
147         }
148     }
149
150 }
151
Popular Tags