1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.jms; |
8 | |
|
9 | |
import org.mule.api.MuleContext; |
10 | |
import org.mule.api.transaction.TransactionException; |
11 | |
import org.mule.config.i18n.CoreMessages; |
12 | |
import org.mule.transaction.AbstractSingleResourceTransaction; |
13 | |
import org.mule.transaction.IllegalTransactionStateException; |
14 | |
import org.mule.transport.jms.i18n.JmsMessages; |
15 | |
|
16 | |
import javax.jms.Connection; |
17 | |
import javax.jms.JMSException; |
18 | |
import javax.jms.Session; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
public class JmsTransaction extends AbstractSingleResourceTransaction |
26 | |
{ |
27 | |
|
28 | |
public JmsTransaction(MuleContext muleContext) |
29 | |
{ |
30 | 0 | super(muleContext); |
31 | 0 | } |
32 | |
|
33 | |
public void bindResource(Object key, Object resource) throws TransactionException |
34 | |
{ |
35 | 0 | if (!(key instanceof Connection) || !(resource instanceof Session)) |
36 | |
{ |
37 | 0 | throw new IllegalTransactionStateException( |
38 | |
CoreMessages.transactionCanOnlyBindToResources("javax.jms.Connection/javax.jms.Session")); |
39 | |
} |
40 | |
|
41 | 0 | Session session = (Session)resource; |
42 | |
try |
43 | |
{ |
44 | 0 | if (!session.getTransacted()) |
45 | |
{ |
46 | 0 | throw new IllegalTransactionStateException(JmsMessages.sessionShouldBeTransacted()); |
47 | |
} |
48 | |
} |
49 | 0 | catch (JMSException e) |
50 | |
{ |
51 | 0 | throw new IllegalTransactionStateException(CoreMessages.transactionCannotReadState(), e); |
52 | 0 | } |
53 | |
|
54 | 0 | super.bindResource(key, resource); |
55 | 0 | } |
56 | |
|
57 | |
protected void doBegin() throws TransactionException |
58 | |
{ |
59 | |
|
60 | 0 | } |
61 | |
|
62 | |
protected void doCommit() throws TransactionException |
63 | |
{ |
64 | 0 | if (resource == null) |
65 | |
{ |
66 | 0 | logger.warn(CoreMessages.commitTxButNoResource(this)); |
67 | 0 | return; |
68 | |
} |
69 | |
|
70 | |
try |
71 | |
{ |
72 | 0 | ((Session) resource).commit(); |
73 | 0 | } |
74 | 0 | catch (JMSException e) |
75 | |
{ |
76 | 0 | throw new TransactionException(CoreMessages.transactionCommitFailed(), e); |
77 | |
} |
78 | |
finally |
79 | |
{ |
80 | 0 | try |
81 | |
{ |
82 | 0 | ((Session) resource).close(); |
83 | |
} |
84 | 0 | catch (JMSException e) |
85 | |
{ |
86 | 0 | logger.warn("could not close jms session", e); |
87 | 0 | } |
88 | 0 | } |
89 | 0 | } |
90 | |
|
91 | |
protected void doRollback() throws TransactionException |
92 | |
{ |
93 | 0 | if (resource == null) |
94 | |
{ |
95 | 0 | logger.warn(CoreMessages.rollbackTxButNoResource(this)); |
96 | 0 | return; |
97 | |
} |
98 | |
|
99 | |
try |
100 | |
{ |
101 | 0 | if (logger.isDebugEnabled()) |
102 | |
{ |
103 | 0 | logger.debug("Rolling back transaction: " + getId()); |
104 | |
} |
105 | 0 | ((Session) resource).rollback(); |
106 | 0 | } |
107 | 0 | catch (JMSException e) |
108 | |
{ |
109 | 0 | throw new TransactionException(CoreMessages.transactionRollbackFailed(), e); |
110 | |
} |
111 | |
finally |
112 | |
{ |
113 | 0 | try |
114 | |
{ |
115 | 0 | ((Session) resource).close(); |
116 | |
} |
117 | 0 | catch (JMSException e) |
118 | |
{ |
119 | 0 | logger.warn("could not close jms session", e); |
120 | 0 | } |
121 | 0 | } |
122 | 0 | } |
123 | |
|
124 | |
} |