/*
 * JBoss, the OpenSource J2EE webOS
 * 
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.tm.integrity;

import java.util.Set;

import org.jboss.tm.TransactionImpl;

/**
 * A transaction integrity that rolls back the transaction
 * if there are other threads associated with it.
 * 
 * @author <a href="adrian@jboss.com">Adrian Brock</a>
 * @version $Revision: 1.1.2.2 $
 */
public class FailIncompleteTransactionIntegrity extends AbstractTransactionIntegrity
{
   public void checkTransactionIntegrity(TransactionImpl transaction)
   {
      // Assert the only thread is ourselves
      Set threads = transaction.getAssociatedThreads();
      String rollbackError = null;
      synchronized (threads)
      {
         if (threads.size() > 1)
            rollbackError = "Too many threads " + threads + " associated with transaction " + transaction;
         else if (threads.size() != 0)
         {
            Thread other = (Thread) threads.iterator().next();
            Thread current = Thread.currentThread();
            if (current.equals(other) == false)
               rollbackError = "Attempt to commit transaction " + transaction + " on thread " + current +
                          " with other threads still associated with the transaction " + other;
         }
      }
      if (rollbackError != null)
      {
         log.error(rollbackError, new IllegalStateException("STACKTRACE"));
         markRollback(transaction);
      }
   }
}
