View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.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   * Manages multi transaction
51   * 
52   * @see <a href="http://www.javaworld.com/javaworld/jw-01-2009/jw-01-spring-transactions.html?page=6">spring transactions</a>
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       * Builds a new ChainedTransactionManager
62       */
63      public ChainedTransactionManager( )
64      {
65          // Ctor
66      }
67  
68      /**
69       * Begin a transaction for all transaction managers {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * "Getter method" pour la variable {@link #_transactionManagers}
157      * 
158      * @return La variable {@link #_transactionManagers}
159      */
160     public List<PlatformTransactionManager> getTransactionManagers( )
161     {
162         return _transactionManagers;
163     }
164 
165     /**
166      * "Setter method" pour la variable {@link #_transactionManagers}
167      * 
168      * @param managers
169      *            La nouvelle valeur de la variable {@link #_transactionManagers}
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 }