1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.spring.transaction; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.transaction.Transaction; |
15 | |
import org.mule.api.transaction.TransactionException; |
16 | |
import org.mule.api.transaction.TransactionFactory; |
17 | |
import org.mule.transaction.AbstractSingleResourceTransaction; |
18 | |
|
19 | |
import org.springframework.jdbc.datasource.ConnectionHolder; |
20 | |
import org.springframework.jms.connection.JmsResourceHolder; |
21 | |
import org.springframework.transaction.PlatformTransactionManager; |
22 | |
import org.springframework.transaction.TransactionStatus; |
23 | |
import org.springframework.transaction.support.TransactionSynchronizationManager; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 8 | public class SpringTransactionFactory implements TransactionFactory |
29 | |
{ |
30 | |
|
31 | |
private PlatformTransactionManager manager; |
32 | |
|
33 | |
public SpringTransactionFactory() |
34 | |
{ |
35 | 4 | super(); |
36 | 4 | } |
37 | |
|
38 | |
public Transaction beginTransaction(MuleContext muleContext) throws TransactionException |
39 | |
{ |
40 | 4 | Transaction tx = new SpringTransaction(); |
41 | 4 | tx.begin(); |
42 | 4 | return tx; |
43 | |
} |
44 | |
|
45 | |
public boolean isTransacted() |
46 | |
{ |
47 | 0 | return true; |
48 | |
} |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
synchronized public PlatformTransactionManager getManager() |
54 | |
{ |
55 | 0 | return manager; |
56 | |
} |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
synchronized public void setManager(PlatformTransactionManager manager) |
62 | |
{ |
63 | 4 | this.manager = manager; |
64 | 4 | } |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public class SpringTransaction extends AbstractSingleResourceTransaction |
70 | |
{ |
71 | |
protected final TransactionStatus status; |
72 | |
|
73 | |
public SpringTransaction() |
74 | 4 | { |
75 | 4 | status = manager.getTransaction(null); |
76 | 4 | } |
77 | |
|
78 | |
protected void doBegin() throws TransactionException |
79 | |
{ |
80 | |
|
81 | 4 | } |
82 | |
|
83 | |
protected void doCommit() throws TransactionException |
84 | |
{ |
85 | 2 | manager.commit(status); |
86 | 2 | } |
87 | |
|
88 | |
protected void doRollback() throws TransactionException |
89 | |
{ |
90 | 2 | manager.rollback(status); |
91 | 2 | } |
92 | |
|
93 | |
public Object getResource(Object key) |
94 | |
{ |
95 | 0 | Object res = TransactionSynchronizationManager.getResource(key); |
96 | 0 | if (res != null) |
97 | |
{ |
98 | 0 | if (!(res instanceof ConnectionHolder)) |
99 | |
{ |
100 | 0 | if (res instanceof JmsResourceHolder) |
101 | |
{ |
102 | 0 | return ((JmsResourceHolder)res).getConnection(); |
103 | |
} |
104 | |
} |
105 | |
else |
106 | |
{ |
107 | 0 | return ((ConnectionHolder)res).getConnection(); |
108 | |
} |
109 | |
} |
110 | 0 | return res; |
111 | |
} |
112 | |
|
113 | |
public boolean hasResource(Object key) |
114 | |
{ |
115 | 0 | return getResource(key) != null; |
116 | |
} |
117 | |
|
118 | |
public void bindResource(Object key, Object resource) throws TransactionException |
119 | |
{ |
120 | 0 | throw new UnsupportedOperationException(); |
121 | |
} |
122 | |
|
123 | |
public void setRollbackOnly() |
124 | |
{ |
125 | 2 | super.setRollbackOnly(); |
126 | 2 | status.setRollbackOnly(); |
127 | 2 | } |
128 | |
|
129 | |
} |
130 | |
|
131 | |
} |