1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.util.jpa.transaction;
35
36 import fr.paris.lutece.util.jpa.JPAConstants;
37
38 import org.apache.log4j.Logger;
39
40 import org.springframework.transaction.PlatformTransactionManager;
41 import org.springframework.transaction.TransactionDefinition;
42 import org.springframework.transaction.TransactionException;
43 import org.springframework.transaction.TransactionStatus;
44 import org.springframework.transaction.support.TransactionSynchronizationManager;
45
46 import java.util.Collections;
47 import java.util.List;
48
49
50
51
52
53
54
55
56 public class ChainedTransactionManager implements PlatformTransactionManager
57 {
58 private static final Logger _log = Logger.getLogger( JPAConstants.JPA_LOGGER );
59 private List<PlatformTransactionManager> _transactionManagers;
60
61
62
63
64 public ChainedTransactionManager( )
65 {
66 }
67
68
69
70
71 public TransactionStatus getTransaction( TransactionDefinition definition )
72 throws TransactionException
73 {
74 if ( _transactionManagers.isEmpty( ) )
75 {
76 return null;
77 }
78
79 MultiTransactionStatus mts = new MultiTransactionStatus( _transactionManagers.get( 0 ) );
80
81 if ( !TransactionSynchronizationManager.isSynchronizationActive( ) )
82 {
83 TransactionSynchronizationManager.initSynchronization( );
84 mts.setNewSynchonization( );
85
86 if ( _log.isDebugEnabled( ) )
87 {
88 _log.debug( "Begin transaction : " + mts.toString( ) );
89 }
90 }
91
92 for ( PlatformTransactionManager transactionManager : _transactionManagers )
93 {
94 mts.getTransactionStatuses( ).put( transactionManager, transactionManager.getTransaction( definition ) );
95 }
96
97 return mts;
98 }
99
100
101
102
103
104 public void commit( TransactionStatus status ) throws TransactionException
105 {
106 for ( PlatformTransactionManager transactionManager : _transactionManagers )
107 {
108 TransactionStatus transactionStatus = null;
109
110 try
111 {
112 transactionStatus = ( (MultiTransactionStatus) status ).getTransactionStatus( transactionManager );
113 transactionManager.commit( transactionStatus );
114 }
115 catch ( Exception e )
116 {
117 _log.error( e.getMessage( ), e );
118
119
120 }
121 }
122
123 if ( _log.isDebugEnabled( ) )
124 {
125 _log.debug( "Ending transaction : " + status.toString( ) );
126 }
127
128 if ( ( (MultiTransactionStatus) status ).isNewSynchonization( ) )
129 {
130 TransactionSynchronizationManager.clear( );
131 }
132 }
133
134
135
136
137
138 public void rollback( TransactionStatus status ) throws TransactionException
139 {
140 for ( PlatformTransactionManager dataSourceManager : _transactionManagers )
141 {
142 try
143 {
144 dataSourceManager.rollback( ( ( (MultiTransactionStatus) status ).getTransactionStatus(
145 dataSourceManager ) ) );
146 }
147 catch ( Exception ex )
148 {
149 _log.error( ex.getMessage( ), ex );
150 }
151 }
152
153 if ( ( (MultiTransactionStatus) status ).isNewSynchonization( ) )
154 {
155 TransactionSynchronizationManager.clear( );
156 }
157 }
158
159
160
161
162
163 public List<PlatformTransactionManager> getTransactionManagers( )
164 {
165 return _transactionManagers;
166 }
167
168
169
170
171
172 public void setTransactionManagers( List<PlatformTransactionManager> managers )
173 {
174 if ( ( managers == null ) || managers.isEmpty( ) )
175 {
176 _transactionManagers = Collections.emptyList( );
177 }
178 else
179 {
180 _transactionManagers = managers;
181
182 if ( _log.isDebugEnabled( ) )
183 {
184 _log.debug( "Transaction Managers : " + _transactionManagers.toString( ) );
185 }
186 }
187 }
188 }