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