1 /*
2 * Copyright (c) 2002-2025, City of Paris
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice
10 * and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice
13 * and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * License 1.0
33 */
34 package fr.paris.lutece.util.jpa.transaction;
35
36 import java.util.HashMap;
37 import java.util.Map;
38
39 import org.springframework.transaction.PlatformTransactionManager;
40 import org.springframework.transaction.TransactionStatus;
41
42 /**
43 * This transaction status wraps several {@link TransactionStatus}
44 */
45 public class MultiTransactionStatus implements TransactionStatus
46 {
47 private Map<PlatformTransactionManager, TransactionStatus> _transactionStatuses;
48 private PlatformTransactionManager _mainPTM;
49 private boolean _bNewSynchonization;
50
51 /**
52 * Creates a TransactionStatus that handles several TransactionStatus
53 *
54 * @param mainPTM
55 * will be default {@link PlatformTransactionManager} for status informations (is*, has* methods)
56 */
57 public MultiTransactionStatus( PlatformTransactionManager mainPTM )
58 {
59 _mainPTM = mainPTM;
60 _transactionStatuses = new HashMap<>( );
61 }
62
63 /**
64 * Sets new synchronization to true
65 */
66 public void setNewSynchonization( )
67 {
68 this._bNewSynchonization = true;
69 }
70
71 /**
72 * true if new synchronization
73 *
74 * @return true if new synchronization
75 */
76 public boolean isNewSynchonization( )
77 {
78 return this._bNewSynchonization;
79 }
80
81 /**
82 * Gets the transaction status for the ptm
83 *
84 * @param ptm
85 * the {@link PlatformTransactionManager}
86 * @return transaction status found
87 */
88 public TransactionStatus getTransactionStatus( PlatformTransactionManager ptm )
89 {
90 return _transactionStatuses.get( ptm );
91 }
92
93 /**
94 *
95 * {@inheritDoc}
96 */
97 public void flush( )
98 {
99 for ( TransactionStatus ts : _transactionStatuses.values( ) )
100 {
101 ts.flush( );
102 }
103 }
104
105 /**
106 * Gets the main transaction status
107 *
108 * @return the transaction status
109 */
110 private TransactionStatus getMainTransactionStatus( )
111 {
112 return _transactionStatuses.get( _mainPTM );
113 }
114
115 /**
116 *
117 * {@inheritDoc}
118 */
119 public boolean hasSavepoint( )
120 {
121 return getMainTransactionStatus( ).hasSavepoint( );
122 }
123
124 /**
125 *
126 * {@inheritDoc}
127 */
128 public boolean isCompleted( )
129 {
130 return getMainTransactionStatus( ).isCompleted( );
131 }
132
133 /**
134 *
135 * {@inheritDoc}
136 */
137 public boolean isNewTransaction( )
138 {
139 return getMainTransactionStatus( ).isNewTransaction( );
140 }
141
142 /**
143 *
144 * {@inheritDoc}
145 */
146 public boolean isRollbackOnly( )
147 {
148 return getMainTransactionStatus( ).isRollbackOnly( );
149 }
150
151 /**
152 *
153 * {@inheritDoc}
154 */
155 public void setRollbackOnly( )
156 {
157 for ( TransactionStatus ts : _transactionStatuses.values( ) )
158 {
159 ts.setRollbackOnly( );
160 }
161 }
162
163 /**
164 *
165 * {@inheritDoc}
166 */
167 public Object createSavepoint( )
168 {
169 return null;
170 }
171
172 /**
173 *
174 * {@inheritDoc}
175 */
176 public void releaseSavepoint( Object savepoint )
177 {
178 for ( TransactionStatus ts : _transactionStatuses.values( ) )
179 {
180 ts.releaseSavepoint( savepoint );
181 }
182 }
183
184 /**
185 *
186 * {@inheritDoc}
187 */
188 public void rollbackToSavepoint( Object savepoint )
189 {
190 for ( TransactionStatus ts : _transactionStatuses.values( ) )
191 {
192 ts.rollbackToSavepoint( savepoint );
193 }
194 }
195
196 /**
197 * "Getter method" for {@link #_transactionStatuses}
198 *
199 * @return value of {@link #_transactionStatuses}
200 */
201 public Map<PlatformTransactionManager, TransactionStatus> getTransactionStatuses( )
202 {
203 return _transactionStatuses;
204 }
205
206 /**
207 * "Setter method" for {@link #_transactionStatuses}
208 *
209 * @param statuses
210 * value of {@link #_transactionStatuses}
211 */
212 public void setTransactionStatuses( Map<PlatformTransactionManager, TransactionStatus> statuses )
213 {
214 _transactionStatuses = statuses;
215 }
216 }