ChainedTransactionManager.java

  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. import java.util.Collections;
  36. import java.util.List;

  37. import org.apache.logging.log4j.LogManager;
  38. import org.apache.logging.log4j.Logger;
  39. import org.springframework.transaction.PlatformTransactionManager;
  40. import org.springframework.transaction.TransactionDefinition;
  41. import org.springframework.transaction.TransactionStatus;
  42. import org.springframework.transaction.support.TransactionSynchronizationManager;

  43. import fr.paris.lutece.util.jpa.JPAConstants;

  44. /**
  45.  *
  46.  * Manages multi transaction
  47.  *
  48.  * @see <a href="http://www.javaworld.com/javaworld/jw-01-2009/jw-01-spring-transactions.html?page=6">spring transactions</a>
  49.  *
  50.  */
  51. public class ChainedTransactionManager implements PlatformTransactionManager
  52. {
  53.     private static final Logger _log = LogManager.getLogger( JPAConstants.JPA_LOGGER );
  54.     private List<PlatformTransactionManager> _transactionManagers;

  55.     /**
  56.      * Builds a new ChainedTransactionManager
  57.      */
  58.     public ChainedTransactionManager( )
  59.     {
  60.         // Ctor
  61.     }

  62.     /**
  63.      * Begin a transaction for all transaction managers {@inheritDoc}
  64.      */
  65.     public TransactionStatus getTransaction( TransactionDefinition definition )
  66.     {
  67.         if ( _transactionManagers.isEmpty( ) )
  68.         {
  69.             return null;
  70.         }

  71.         MultiTransactionStatus mts = new MultiTransactionStatus( _transactionManagers.get( 0 ) );

  72.         if ( !TransactionSynchronizationManager.isSynchronizationActive( ) )
  73.         {
  74.             TransactionSynchronizationManager.initSynchronization( );
  75.             mts.setNewSynchonization( );

  76.             if ( _log.isDebugEnabled( ) )
  77.             {
  78.                 _log.debug( "Begin transaction : " + mts.toString( ) );
  79.             }
  80.         }

  81.         for ( PlatformTransactionManager transactionManager : _transactionManagers )
  82.         {
  83.             mts.getTransactionStatuses( ).put( transactionManager, transactionManager.getTransaction( definition ) );
  84.         }

  85.         return mts;
  86.     }

  87.     /**
  88.      *
  89.      * {@inheritDoc}
  90.      */
  91.     public void commit( TransactionStatus status )
  92.     {
  93.         for ( PlatformTransactionManager transactionManager : _transactionManagers )
  94.         {
  95.             TransactionStatus transactionStatus = null;

  96.             try
  97.             {
  98.                 transactionStatus = ( (MultiTransactionStatus) status ).getTransactionStatus( transactionManager );
  99.                 transactionManager.commit( transactionStatus );
  100.             }
  101.             catch( Exception e )
  102.             {
  103.                 _log.error( e.getMessage( ), e );
  104.             }
  105.         }

  106.         if ( _log.isDebugEnabled( ) )
  107.         {
  108.             _log.debug( "Ending transaction : " + status.toString( ) );
  109.         }

  110.         if ( ( (MultiTransactionStatus) status ).isNewSynchonization( ) )
  111.         {
  112.             TransactionSynchronizationManager.clear( );
  113.         }
  114.     }

  115.     /**
  116.      *
  117.      * {@inheritDoc}
  118.      */
  119.     public void rollback( TransactionStatus status )
  120.     {
  121.         for ( PlatformTransactionManager dataSourceManager : _transactionManagers )
  122.         {
  123.             try
  124.             {
  125.                 dataSourceManager.rollback( ( ( (MultiTransactionStatus) status ).getTransactionStatus( dataSourceManager ) ) );
  126.             }
  127.             catch( Exception ex )
  128.             {
  129.                 _log.error( ex.getMessage( ), ex );
  130.             }
  131.         }

  132.         if ( ( (MultiTransactionStatus) status ).isNewSynchonization( ) )
  133.         {
  134.             TransactionSynchronizationManager.clear( );
  135.         }
  136.     }

  137.     /**
  138.      * "Getter method" pour la variable {@link #_transactionManagers}
  139.      *
  140.      * @return La variable {@link #_transactionManagers}
  141.      */
  142.     public List<PlatformTransactionManager> getTransactionManagers( )
  143.     {
  144.         return _transactionManagers;
  145.     }

  146.     /**
  147.      * "Setter method" pour la variable {@link #_transactionManagers}
  148.      *
  149.      * @param managers
  150.      *            La nouvelle valeur de la variable {@link #_transactionManagers}
  151.      */
  152.     public void setTransactionManagers( List<PlatformTransactionManager> managers )
  153.     {
  154.         if ( ( managers == null ) || managers.isEmpty( ) )
  155.         {
  156.             _transactionManagers = Collections.emptyList( );
  157.         }
  158.         else
  159.         {
  160.             _transactionManagers = managers;

  161.             if ( _log.isDebugEnabled( ) )
  162.             {
  163.                 _log.debug( "Transaction Managers : " + _transactionManagers.toString( ) );
  164.             }
  165.         }
  166.     }
  167. }