1
2
3
4
5
6
7
8
9
10
11 package org.mule.transaction;
12
13 import org.mule.api.MuleContext;
14 import org.mule.api.transaction.TransactionException;
15 import org.mule.config.i18n.CoreMessages;
16
17 import java.text.MessageFormat;
18
19 import javax.transaction.Status;
20 import javax.transaction.Synchronization;
21
22
23
24
25 public class ExternalXaTransaction extends XaTransaction
26 {
27 public ExternalXaTransaction(MuleContext muleContext)
28 {
29 super(muleContext);
30 }
31
32 protected void doBegin() throws TransactionException
33 {
34 if (txManager == null)
35 {
36 throw new IllegalStateException(
37 CoreMessages.objectNotRegistered("javax.transaction.TransactionManager", "Transaction Manager").getMessage());
38 }
39
40 try
41 {
42 synchronized (this)
43 {
44 transaction = txManager.getTransaction();
45 transaction.registerSynchronization(new ExternalTransaction(muleContext));
46 }
47 }
48 catch (Exception e)
49 {
50 throw new TransactionException(CoreMessages.cannotStartTransaction("XA"), e);
51 }
52 }
53
54
55
56
57
58 class ExternalTransaction extends AbstractTransaction implements Synchronization
59 {
60 ExternalTransaction(MuleContext muleContext)
61 {
62 super(muleContext);
63 }
64
65
66 public void beforeCompletion()
67 {
68 }
69
70
71 public void afterCompletion(int status)
72 {
73 boolean commit = status == Status.STATUS_COMMITTED;
74
75 try
76 {
77 if (commit)
78 {
79 commit();
80 }
81 else
82 {
83 rollback();
84 }
85 }
86 catch (TransactionException ex)
87 {
88 logger.warn(MessageFormat.format(
89 "Exception while {0} an external transaction {1}", commit ? "committing" : "rolling back", this), ex);
90 }
91 }
92
93 @Override
94 protected void unbindTransaction()
95 {
96
97 }
98
99 @Override
100 protected void doCommit()
101 {
102 delistResources();
103 closeResources();
104 transaction = null;
105 }
106
107 @Override
108 protected void doRollback()
109 {
110 closeResources();
111 transaction = null;
112 }
113
114 @Override
115 protected void doBegin()
116 {
117 }
118
119 @Override
120 public boolean isRollbackOnly() throws TransactionException
121 {
122 return ExternalXaTransaction.this.isRollbackOnly();
123 }
124
125 public int getStatus() throws TransactionException
126 {
127 return ExternalXaTransaction.this.getStatus();
128 }
129
130 public Object getResource(Object key)
131 {
132 return ExternalXaTransaction.this.getResource(key);
133 }
134
135 public boolean hasResource(Object key)
136 {
137 return ExternalXaTransaction.this.hasResource(key);
138 }
139
140 public void bindResource(Object key, Object resource) throws TransactionException
141 {
142 }
143
144 public void setRollbackOnly() throws TransactionException
145 {
146 }
147 }
148 }