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