KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transaction > LocalTransaction


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.transaction;
19
20 import java.io.IOException JavaDoc;
21
22 import javax.transaction.xa.XAException JavaDoc;
23
24 import org.apache.activemq.broker.ConnectionContext;
25 import org.apache.activemq.command.LocalTransactionId;
26 import org.apache.activemq.command.TransactionId;
27 import org.apache.activemq.store.TransactionStore;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 /**
32  * @version $Revision: 1.3 $
33  */

34 public class LocalTransaction extends Transaction {
35     
36     private static final Log log = LogFactory.getLog(LocalTransaction.class);
37     
38     private final TransactionStore transactionStore;
39     private final LocalTransactionId xid;
40     private final ConnectionContext context;
41
42     public LocalTransaction(TransactionStore transactionStore, LocalTransactionId xid, ConnectionContext context) {
43         this.transactionStore = transactionStore;
44         this.xid = xid;
45         this.context = context;
46     }
47
48     public void commit(boolean onePhase) throws XAException JavaDoc, IOException JavaDoc {
49         // Get ready for commit.
50
try {
51             prePrepare();
52         }
53         catch (XAException JavaDoc e) {
54             throw e;
55         }
56         catch (Throwable JavaDoc e) {
57             log.warn("COMMIT FAILED: ", e);
58             rollback();
59             // Let them know we rolled back.
60
XAException JavaDoc xae = new XAException JavaDoc("COMMIT FAILED: Transaction rolled back.");
61             xae.errorCode = XAException.XA_RBOTHER;
62             xae.initCause(e);
63             throw xae;
64         }
65
66         setState(Transaction.FINISHED_STATE);
67         context.getTransactions().remove(xid);
68         transactionStore.commit(getTransactionId(), false);
69         
70         try {
71             fireAfterCommit();
72         }
73         catch (Throwable JavaDoc e) {
74             // I guess this could happen. Post commit task failed
75
// to execute properly.
76
log.warn("POST COMMIT FAILED: ", e);
77             XAException JavaDoc xae = new XAException JavaDoc("POST COMMIT FAILED");
78             xae.errorCode = XAException.XAER_RMERR;
79             xae.initCause(e);
80             throw xae;
81         }
82     }
83
84     public void rollback() throws XAException JavaDoc, IOException JavaDoc {
85
86         setState(Transaction.FINISHED_STATE);
87         context.getTransactions().remove(xid);
88         transactionStore.rollback(getTransactionId());
89
90         try {
91             fireAfterRollback();
92         }
93         catch (Throwable JavaDoc e) {
94             log.warn("POST ROLLBACK FAILED: ", e);
95             XAException JavaDoc xae = new XAException JavaDoc("POST ROLLBACK FAILED");
96             xae.errorCode = XAException.XAER_RMERR;
97             xae.initCause(e);
98             throw xae;
99         }
100     }
101
102     public int prepare() throws XAException JavaDoc {
103         XAException JavaDoc xae = new XAException JavaDoc("Prepare not implemented on Local Transactions.");
104         xae.errorCode = XAException.XAER_RMERR;
105         throw xae;
106     }
107
108     public TransactionId getTransactionId() {
109         return xid;
110     }
111
112 }
113
Popular Tags