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 | 0 | public class SpringTransactionFactory implements TransactionFactory |
29 | |
{ |
30 | |
|
31 | |
private PlatformTransactionManager manager; |
32 | |
|
33 | |
public SpringTransactionFactory() |
34 | |
{ |
35 | 0 | super(); |
36 | 0 | } |
37 | |
|
38 | |
public Transaction beginTransaction(MuleContext muleContext) throws TransactionException |
39 | |
{ |
40 | 0 | Transaction tx = new SpringTransaction(muleContext); |
41 | 0 | tx.begin(); |
42 | 0 | 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 | 0 | this.manager = manager; |
64 | 0 | } |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public class SpringTransaction extends AbstractSingleResourceTransaction |
70 | |
{ |
71 | |
protected final TransactionStatus status; |
72 | |
|
73 | |
public SpringTransaction(MuleContext muleContext) |
74 | 0 | { |
75 | 0 | super(muleContext); |
76 | 0 | status = manager.getTransaction(null); |
77 | 0 | } |
78 | |
|
79 | |
protected void doBegin() throws TransactionException |
80 | |
{ |
81 | |
|
82 | 0 | } |
83 | |
|
84 | |
protected void doCommit() throws TransactionException |
85 | |
{ |
86 | 0 | manager.commit(status); |
87 | 0 | } |
88 | |
|
89 | |
protected void doRollback() throws TransactionException |
90 | |
{ |
91 | 0 | manager.rollback(status); |
92 | 0 | } |
93 | |
|
94 | |
public Object getResource(Object key) |
95 | |
{ |
96 | 0 | Object res = TransactionSynchronizationManager.getResource(key); |
97 | 0 | if (res != null) |
98 | |
{ |
99 | 0 | if (!(res instanceof ConnectionHolder)) |
100 | |
{ |
101 | 0 | if (res instanceof JmsResourceHolder) |
102 | |
{ |
103 | 0 | return ((JmsResourceHolder)res).getConnection(); |
104 | |
} |
105 | |
} |
106 | |
else |
107 | |
{ |
108 | 0 | return ((ConnectionHolder)res).getConnection(); |
109 | |
} |
110 | |
} |
111 | 0 | return res; |
112 | |
} |
113 | |
|
114 | |
public boolean hasResource(Object key) |
115 | |
{ |
116 | 0 | return getResource(key) != null; |
117 | |
} |
118 | |
|
119 | |
public void bindResource(Object key, Object resource) throws TransactionException |
120 | |
{ |
121 | 0 | throw new UnsupportedOperationException(); |
122 | |
} |
123 | |
|
124 | |
public void setRollbackOnly() |
125 | |
{ |
126 | 0 | super.setRollbackOnly(); |
127 | 0 | status.setRollbackOnly(); |
128 | 0 | } |
129 | |
|
130 | |
} |
131 | |
|
132 | |
} |