[jboss-cvs] jboss-seam/src/ioc/org/jboss/seam/ioc/spring ...

Michael Youngstrom youngm at gmail.com
Mon Jul 9 19:10:18 EDT 2007


  User: myoungstrom
  Date: 07/07/09 19:10:18

  Modified:    src/ioc/org/jboss/seam/ioc/spring   spring-2.0.xsd
                        SpringTransaction.java
  Log:
  Fixed dangling transaction on rollback.
  
  Revision  Changes    Path
  1.3       +9 -0      jboss-seam/src/ioc/org/jboss/seam/ioc/spring/spring-2.0.xsd
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: spring-2.0.xsd
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/ioc/org/jboss/seam/ioc/spring/spring-2.0.xsd,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- spring-2.0.xsd	3 Jul 2007 20:36:29 -0000	1.2
  +++ spring-2.0.xsd	9 Jul 2007 23:10:18 -0000	1.3
  @@ -65,6 +65,15 @@
   						</xs:documentation>
   					</xs:annotation>
   			</xs:attribute>
  +			<xs:attribute name="join-transaction" type="xs:boolean" use="optional">
  +				<xs:annotation>
  +					<xs:annotation>
  +						<xs:documentation>
  +							Should this transaction manager participate in request to join a transaction.  For JTA
  +							transactions set to true. 
  +						</xs:documentation>
  +					</xs:annotation>
  +			</xs:attribute>
   		</xs:complexType>
   	</xs:element>
   </xs:schema>
  
  
  
  1.3       +123 -48   jboss-seam/src/ioc/org/jboss/seam/ioc/spring/SpringTransaction.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: SpringTransaction.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/ioc/org/jboss/seam/ioc/spring/SpringTransaction.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- SpringTransaction.java	6 Jul 2007 17:39:03 -0000	1.2
  +++ SpringTransaction.java	9 Jul 2007 23:10:18 -0000	1.3
  @@ -2,6 +2,7 @@
   
   import static org.jboss.seam.annotations.Install.FRAMEWORK;
   
  +import javax.persistence.EntityManager;
   import javax.transaction.HeuristicMixedException;
   import javax.transaction.HeuristicRollbackException;
   import javax.transaction.NotSupportedException;
  @@ -11,6 +12,7 @@
   import javax.transaction.SystemException;
   
   import org.jboss.seam.ScopeType;
  +import org.jboss.seam.annotations.Destroy;
   import org.jboss.seam.annotations.Install;
   import org.jboss.seam.annotations.Name;
   import org.jboss.seam.annotations.Scope;
  @@ -19,6 +21,7 @@
   import org.jboss.seam.log.LogProvider;
   import org.jboss.seam.log.Logging;
   import org.jboss.seam.transaction.AbstractUserTransaction;
  +import org.springframework.orm.jpa.JpaTransactionManager;
   import org.springframework.transaction.HeuristicCompletionException;
   import org.springframework.transaction.PlatformTransactionManager;
   import org.springframework.transaction.TransactionException;
  @@ -36,18 +39,29 @@
   public class SpringTransaction extends AbstractUserTransaction
   {
      private static final LogProvider log = Logging.getLogProvider(SpringTransaction.class);
  +
      private ValueExpression<PlatformTransactionManager> platformTransactionManager;
  +
      private DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
  +
      private boolean conversationContextRequired = true;
  +
      private TransactionStatus currentTransaction;
   
  +   private Boolean joinTransaction;
  +
      @Override
      public void registerSynchronization(final Synchronization sync)
      {
  -      if(TransactionSynchronizationManager.isSynchronizationActive()) {
  -         TransactionSynchronizationManager.registerSynchronization(new JtaSpringSynchronizationAdapter(sync));
  -      } else {
  -         throw new IllegalStateException("TransactionSynchronization not available with this Spring Transaction Manager");
  +      if (TransactionSynchronizationManager.isSynchronizationActive())
  +      {
  +         TransactionSynchronizationManager
  +                  .registerSynchronization(new JtaSpringSynchronizationAdapter(sync));
  +      }
  +      else
  +      {
  +         throw new IllegalStateException(
  +                  "TransactionSynchronization not available with this Spring Transaction Manager");
         }
      }
   
  @@ -71,53 +85,75 @@
         catch (HeuristicCompletionException e)
         {
            
  -         switch(e.getOutcomeState()) {
  -            case HeuristicCompletionException.STATE_ROLLED_BACK :
  +         switch (e.getOutcomeState())
  +         {
  +            case HeuristicCompletionException.STATE_ROLLED_BACK:
                  log.error("Exception cause:", e);
                  throw new HeuristicRollbackException(e.getMessage());
  -            default :
  +            default:
                  log.error("Exception cause:", e);
                  throw new HeuristicMixedException(e.getMessage());
            }
  -      } catch (TransactionSystemException e) {
  +      }
  +      catch (TransactionSystemException e)
  +      {
            log.error("Exception cause:", e);
            throw new SystemException(e.getMessage());
  -      } catch(UnexpectedRollbackException e) {
  +      }
  +      catch (UnexpectedRollbackException e)
  +      {
            log.error("Exception cause:", e);
            throw new RollbackException(e.getMessage());
  -      } finally {
  +      }
  +      finally
  +      {
            currentTransaction = null;
         }
      }
   
      public int getStatus() throws SystemException
      {
  -      if(TransactionSynchronizationManager.isActualTransactionActive()) {
  +      if (TransactionSynchronizationManager.isActualTransactionActive())
  +      {
            TransactionStatus transaction = null;
  -         try {
  -            if(currentTransaction == null) {
  +         try
  +         {
  +            if (currentTransaction == null)
  +            {
                  transaction = platformTransactionManager.getValue().getTransaction(definition);
  -               if(transaction.isNewTransaction()) {
  +               if (transaction.isNewTransaction())
  +               {
                     return Status.STATUS_COMMITTED;
                  }
  -            } else {
  +            }
  +            else
  +            {
                  transaction = currentTransaction;
               }
  -            //If SynchronizationManager things it has an active transaction but our transaction is a new one
  -            //then we must be in the middle of committing
  -            if(transaction.isCompleted()) {
  -               if(transaction.isRollbackOnly()) {
  +            // If SynchronizationManager things it has an active transaction but
  +            // our transaction is a new one
  +            // then we must be in the middle of committing
  +            if (transaction.isCompleted())
  +            {
  +               if (transaction.isRollbackOnly())
  +               {
                     return Status.STATUS_ROLLEDBACK;
                  }
                  return Status.STATUS_COMMITTED;
  -            } else {
  -               if(transaction.isRollbackOnly()) {
  +            }
  +            else
  +            {
  +               if (transaction.isRollbackOnly())
  +               {
                     return Status.STATUS_MARKED_ROLLBACK;
                  }
                  return Status.STATUS_ACTIVE;
               }
  -         } finally {
  -            if(currentTransaction == null) {
  +         }
  +         finally
  +         {
  +            if (currentTransaction == null)
  +            {
                  platformTransactionManager.getValue().commit(transaction);
               }
            }
  @@ -130,14 +166,15 @@
         assertActive();
         try
         {
  -         TransactionStatus transaction = platformTransactionManager.getValue().getTransaction(definition);
  -         platformTransactionManager.getValue().rollback(transaction);
  +         platformTransactionManager.getValue().rollback(currentTransaction);
         }
         catch (TransactionException e)
         {
            log.error("Exception cause:", e);
            throw new SystemException(e.getMessage());
  -      } finally {
  +      }
  +      finally
  +      {
            currentTransaction = null;
         }
      }
  @@ -147,23 +184,29 @@
       */
      private void assertActive()
      {
  -      if(!TransactionSynchronizationManager.isActualTransactionActive() || currentTransaction == null) {
  -         throw new IllegalStateException("No transaction currently active that Seam started." +
  -         		"Seam should only be able to committ or rollback transactions it started.");
  +      if (!TransactionSynchronizationManager.isActualTransactionActive()
  +               || currentTransaction == null)
  +      {
  +         throw new IllegalStateException("No transaction currently active that Seam started."
  +                  + "Seam should only be able to committ or rollback transactions it started.");
         }
      }
   
      public void setRollbackOnly() throws IllegalStateException, SystemException
      {
  -      if(!TransactionSynchronizationManager.isActualTransactionActive()) {
  +      if (!TransactionSynchronizationManager.isActualTransactionActive())
  +      {
            throw new IllegalStateException("No Spring Transaction is currently available.");
         }
         TransactionStatus transaction = null;
         try
         {
  -         if(currentTransaction == null) {
  +         if (currentTransaction == null)
  +         {
               transaction = platformTransactionManager.getValue().getTransaction(definition);
  -         } else {
  +         }
  +         else
  +         {
               transaction = currentTransaction;
            }
            transaction.setRollbackOnly();
  @@ -172,26 +215,55 @@
         {
            log.error("Exception cause:", e);
            throw new SystemException(e.getMessage());
  -      } finally {
  -         if(currentTransaction == null) {
  -            if(transaction.isNewTransaction()) {
  -               throw new IllegalStateException("Our transactions are in a bad state");
  -            } else {
  -               platformTransactionManager.getValue().commit(transaction);
               }
  +      finally
  +      {
  +         if (currentTransaction == null)
  +         {
  +            platformTransactionManager.getValue().commit(transaction);
            }
         }
      }
   
      public void setTransactionTimeout(int timeout) throws SystemException
      {
  -      if(TransactionSynchronizationManager.isActualTransactionActive()) {
  -         //cannot set timeout on already running transaction
  +      if (TransactionSynchronizationManager.isActualTransactionActive())
  +      {
  +         // cannot set timeout on already running transaction
            return;
         }
         definition.setTimeout(timeout);
      }
      
  +   @Override
  +   public void enlist(EntityManager entityManager) throws SystemException
  +   {
  +      if (joinTransaction == null)
  +      {
  +         // If not set attempt to detect if we should join or not
  +         if (!(platformTransactionManager.getValue() instanceof JpaTransactionManager))
  +         {
  +            super.enlist(entityManager);
  +         }
  +      }
  +      else if (joinTransaction)
  +      {
  +         super.enlist(entityManager);
  +      }
  +   }
  +   
  +   @Destroy
  +   public void cleanupCurrentTransaction() {
  +      if(currentTransaction != null) {
  +         try {
  +            log.debug("Attempting to rollback left over transaction.  Should never be called.");
  +            platformTransactionManager.getValue().rollback(currentTransaction);
  +         } catch(Throwable e) {
  +            //ignore
  +         }
  +      }
  +   }
  +
      public void setPlatformTransactionManager(
               ValueExpression<PlatformTransactionManager> platformTransactionManager)
      {
  @@ -209,14 +281,17 @@
         this.conversationContextRequired = conversationContextRequired;
      }
      
  +   public void setJoinTransaction(Boolean joinTransaction)
  +   {
  +      this.joinTransaction = joinTransaction;
  +   }
      
      public class JtaSpringSynchronizationAdapter extends TransactionSynchronizationAdapter
      {
  -      
         @Override
         public int getOrder()
         {
  -         return SeamLifecycleUtils.SEAM_LIFECYCLE_SYNCHRONIZATION_ORDER-1;
  +         return SeamLifecycleUtils.SEAM_LIFECYCLE_SYNCHRONIZATION_ORDER - 1;
         }
         
         private final Synchronization sync;
  
  
  



More information about the jboss-cvs-commits mailing list