MultiTransactionStatus.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.HashMap;
  36. import java.util.Map;

  37. import org.springframework.transaction.PlatformTransactionManager;
  38. import org.springframework.transaction.TransactionStatus;

  39. /**
  40.  * This transaction status wraps several {@link TransactionStatus}
  41.  */
  42. public class MultiTransactionStatus implements TransactionStatus
  43. {
  44.     private Map<PlatformTransactionManager, TransactionStatus> _transactionStatuses;
  45.     private PlatformTransactionManager _mainPTM;
  46.     private boolean _bNewSynchonization;

  47.     /**
  48.      * Creates a TransactionStatus that handles several TransactionStatus
  49.      *
  50.      * @param mainPTM
  51.      *            will be default {@link PlatformTransactionManager} for status informations (is*, has* methods)
  52.      */
  53.     public MultiTransactionStatus( PlatformTransactionManager mainPTM )
  54.     {
  55.         _mainPTM = mainPTM;
  56.         _transactionStatuses = new HashMap<>( );
  57.     }

  58.     /**
  59.      * Sets new synchronization to true
  60.      */
  61.     public void setNewSynchonization( )
  62.     {
  63.         this._bNewSynchonization = true;
  64.     }

  65.     /**
  66.      * true if new synchronization
  67.      *
  68.      * @return true if new synchronization
  69.      */
  70.     public boolean isNewSynchonization( )
  71.     {
  72.         return this._bNewSynchonization;
  73.     }

  74.     /**
  75.      * Gets the transaction status for the ptm
  76.      *
  77.      * @param ptm
  78.      *            the {@link PlatformTransactionManager}
  79.      * @return transaction status found
  80.      */
  81.     public TransactionStatus getTransactionStatus( PlatformTransactionManager ptm )
  82.     {
  83.         return _transactionStatuses.get( ptm );
  84.     }

  85.     /**
  86.      *
  87.      * {@inheritDoc}
  88.      */
  89.     public void flush( )
  90.     {
  91.         for ( TransactionStatus ts : _transactionStatuses.values( ) )
  92.         {
  93.             ts.flush( );
  94.         }
  95.     }

  96.     /**
  97.      * Gets the main transaction status
  98.      *
  99.      * @return the transaction status
  100.      */
  101.     private TransactionStatus getMainTransactionStatus( )
  102.     {
  103.         return _transactionStatuses.get( _mainPTM );
  104.     }

  105.     /**
  106.      *
  107.      * {@inheritDoc}
  108.      */
  109.     public boolean hasSavepoint( )
  110.     {
  111.         return getMainTransactionStatus( ).hasSavepoint( );
  112.     }

  113.     /**
  114.      *
  115.      * {@inheritDoc}
  116.      */
  117.     public boolean isCompleted( )
  118.     {
  119.         return getMainTransactionStatus( ).isCompleted( );
  120.     }

  121.     /**
  122.      *
  123.      * {@inheritDoc}
  124.      */
  125.     public boolean isNewTransaction( )
  126.     {
  127.         return getMainTransactionStatus( ).isNewTransaction( );
  128.     }

  129.     /**
  130.      *
  131.      * {@inheritDoc}
  132.      */
  133.     public boolean isRollbackOnly( )
  134.     {
  135.         return getMainTransactionStatus( ).isRollbackOnly( );
  136.     }

  137.     /**
  138.      *
  139.      * {@inheritDoc}
  140.      */
  141.     public void setRollbackOnly( )
  142.     {
  143.         for ( TransactionStatus ts : _transactionStatuses.values( ) )
  144.         {
  145.             ts.setRollbackOnly( );
  146.         }
  147.     }

  148.     /**
  149.      *
  150.      * {@inheritDoc}
  151.      */
  152.     public Object createSavepoint( )
  153.     {
  154.         return null;
  155.     }

  156.     /**
  157.      *
  158.      * {@inheritDoc}
  159.      */
  160.     public void releaseSavepoint( Object savepoint )
  161.     {
  162.         for ( TransactionStatus ts : _transactionStatuses.values( ) )
  163.         {
  164.             ts.releaseSavepoint( savepoint );
  165.         }
  166.     }

  167.     /**
  168.      *
  169.      * {@inheritDoc}
  170.      */
  171.     public void rollbackToSavepoint( Object savepoint )
  172.     {
  173.         for ( TransactionStatus ts : _transactionStatuses.values( ) )
  174.         {
  175.             ts.rollbackToSavepoint( savepoint );
  176.         }
  177.     }

  178.     /**
  179.      * "Getter method" for {@link #_transactionStatuses}
  180.      *
  181.      * @return value of {@link #_transactionStatuses}
  182.      */
  183.     public Map<PlatformTransactionManager, TransactionStatus> getTransactionStatuses( )
  184.     {
  185.         return _transactionStatuses;
  186.     }

  187.     /**
  188.      * "Setter method" for {@link #_transactionStatuses}
  189.      *
  190.      * @param statuses
  191.      *            value of {@link #_transactionStatuses}
  192.      */
  193.     public void setTransactionStatuses( Map<PlatformTransactionManager, TransactionStatus> statuses )
  194.     {
  195.         _transactionStatuses = statuses;
  196.     }
  197. }