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 | 0 | public class SpringTransactionFactory implements UMOTransactionFactory |
28 | |
{ |
29 | |
|
30 | |
private PlatformTransactionManager manager; |
31 | |
|
32 | |
public SpringTransactionFactory() |
33 | |
{ |
34 | 0 | super(); |
35 | 0 | } |
36 | |
|
37 | |
public UMOTransaction beginTransaction() throws TransactionException |
38 | |
{ |
39 | 0 | return new SpringTransaction(); |
40 | |
} |
41 | |
|
42 | |
public boolean isTransacted() |
43 | |
{ |
44 | 0 | return true; |
45 | |
} |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
synchronized public PlatformTransactionManager getManager() |
51 | |
{ |
52 | 0 | return manager; |
53 | |
} |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
synchronized public void setManager(PlatformTransactionManager manager) |
59 | |
{ |
60 | 0 | this.manager = manager; |
61 | 0 | } |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public class SpringTransaction extends AbstractSingleResourceTransaction |
67 | |
{ |
68 | |
protected final TransactionStatus status; |
69 | |
|
70 | |
public SpringTransaction() |
71 | 0 | { |
72 | 0 | status = manager.getTransaction(null); |
73 | 0 | } |
74 | |
|
75 | |
protected void doBegin() throws TransactionException |
76 | |
{ |
77 | |
|
78 | 0 | } |
79 | |
|
80 | |
protected void doCommit() throws TransactionException |
81 | |
{ |
82 | 0 | manager.commit(status); |
83 | 0 | } |
84 | |
|
85 | |
protected void doRollback() throws TransactionException |
86 | |
{ |
87 | 0 | manager.rollback(status); |
88 | 0 | } |
89 | |
|
90 | |
public Object getResource(Object key) |
91 | |
{ |
92 | 0 | Object res = TransactionSynchronizationManager.getResource(key); |
93 | 0 | if (res != null) |
94 | |
{ |
95 | 0 | if (!(res instanceof ConnectionHolder)) |
96 | |
{ |
97 | 0 | if (res instanceof JmsResourceHolder) |
98 | |
{ |
99 | 0 | return ((JmsResourceHolder)res).getConnection(); |
100 | |
} |
101 | |
} |
102 | |
else |
103 | |
{ |
104 | 0 | return ((ConnectionHolder)res).getConnection(); |
105 | |
} |
106 | |
} |
107 | 0 | return res; |
108 | |
} |
109 | |
|
110 | |
public boolean hasResource(Object key) |
111 | |
{ |
112 | 0 | return getResource(key) != null; |
113 | |
} |
114 | |
|
115 | |
public void bindResource(Object key, Object resource) throws TransactionException |
116 | |
{ |
117 | 0 | throw new UnsupportedOperationException(); |
118 | |
} |
119 | |
|
120 | |
public void setRollbackOnly() |
121 | |
{ |
122 | 0 | super.setRollbackOnly(); |
123 | 0 | status.setRollbackOnly(); |
124 | 0 | } |
125 | |
|
126 | |
} |
127 | |
|
128 | |
} |