/*
* $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/transaction/SlideTransactionManager.java,v 1.5 2001/10/04 02:48:34 remm Exp $
* $Revision: 1.5 $
* $Date: 2001/10/04 02:48:34 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.slide.transaction;
import java.util.Hashtable;
import javax.transaction.UserTransaction;
import javax.transaction.Status;
import javax.transaction.TransactionManager;
import javax.transaction.Transaction;
import javax.transaction.Synchronization;
import javax.transaction.HeuristicCommitException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.InvalidTransactionException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import org.apache.slide.util.Messages;
import org.apache.slide.util.logger.Logger;
import org.apache.slide.util.logger.SimpleLogger;
/**
* JTA Transaction manager implementation.
* <p>
* Implementation notes :
* <ul>
* <li>Does not support nested transactions</li>
* <li>No security</li>
* </ul>
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
* @version $Revision: 1.5 $
*/
public final class SlideTransactionManager implements TransactionManager {
// -------------------------------------------------------------- Constants
protected static final String LOG_CHANNEL =
SlideTransactionManager.class.getName();
public static final int DEFAULT_TRANSACTION_TIMEOUT = 30;
// ------------------------------------------------------------ Constructor
// ----------------------------------------------------- Instance Variables
/**
* Transaction bindings thread id <-> transaction object.
*/
private Hashtable bindings = new Hashtable();
/**
* Transaction bindings thread id <-> transaction timeout.
*/
private Hashtable timeouts = new Hashtable();
/**
* Associated logger.
*/
private Logger logger = new SimpleLogger();
// ------------------------------------------------------------- Properties
// --------------------------------------------------------- Public Methods
/**
* Get the logger associated with the transaction manager.
*/
public Logger getLogger() {
return logger;
}
/**
* Set the logger of the transaction manager.
*/
public void setLogger(Logger logger) {
this.logger = logger;
}
// --------------------------------------------- TransactionManager Methods
/**
* Create a new transaction and associate it with the current thread.
*
* @exception NotSupportedException Thrown if the thread is already
* associated with a transaction and the Transaction Manager
* implementation does not support nested transactions.
* @exception SystemException Thrown if the transaction manager encounters
* an unexpected error condition.
*/
public void begin()
throws NotSupportedException, SystemException {
Transaction currentTransaction = getTransaction();
if (currentTransaction != null)
throw new NotSupportedException();
currentTransaction = new SlideTransaction(this);
bindings.put(Thread.currentThread(), currentTransaction);
if (logger.isEnabled(LOG_CHANNEL, Logger.DEBUG)) {
String logMessage = Messages.format
(SlideTransactionManager.class.getName() + ".begin",
currentTransaction.toString());
logger.log(logMessage, LOG_CHANNEL, Logger.DEBUG);
}
}
/**
* Complete the transaction associated with the current thread. When this
* method completes, the thread becomes associated with no transaction.
*
* @exception RollbackException Thrown to indicate that the transaction
* has been rolled back rather than committed.
* @exception HeuristicMixedException Thrown to indicate that a heuristic
* decision was made and that some relevant updates have been committed
* while others have been rolled back.
* @exception HeuristicRollbackException Thrown to indicate that a
* heuristic decision was made and that some relevant updates have been
* rolled back.
* @exception SecurityException Thrown to indicate that the thread is not
* allowed to commit the transaction.
* @exception IllegalStateException Thrown if the current thread is not
* associated with a transaction.
* @exception SystemException Thrown if the transaction manager encounters
* an unexpected error condition.
*/
public void commit()
throws RollbackException, HeuristicMixedException,
HeuristicRollbackException, SecurityException, IllegalStateException,
SystemException {
Thread currentThread = Thread.currentThread();
Transaction currentTransaction =
(Transaction) bindings.remove(currentThread);
if (currentTransaction == null)
throw new IllegalStateException();
timeouts.remove(currentThread);
if (logger.isEnabled(LOG_CHANNEL, Logger.DEBUG)) {
String logMessage = Messages.format
(SlideTransactionManager.class.getName() + ".commit",
currentTransaction.toString());
logger.log(logMessage, LOG_CHANNEL, Logger.DEBUG);
}
currentTransaction.commit();
}
/**
* Roll back the transaction associated with the current thread. When
* this method completes, the thread becomes associated with no
* transaction.
*
* @exception SecurityException Thrown to indicate that the thread is not
* allowed to commit the transaction.
* @exception IllegalStateException Thrown if the current thread is not
* associated with a transaction.
* @exception SystemException Thrown if the transaction manager encounters
* an unexpected error condition.
*/
public void rollback()
throws SecurityException, IllegalStateException, SystemException {
Thread currentThread = Thread.currentThread();
Transaction currentTransaction =
(Transaction) bindings.remove(currentThread);
if (currentTransaction == null)
throw new IllegalStateException();
timeouts.remove(currentThread);
String logMessage = Messages.format
(SlideTransactionManager.class.getName() + ".rollback",
currentTransaction.toString());
logger.log(logMessage, LOG_CHANNEL, Logger.INFO);
currentTransaction.rollback();
}
/**
* Modify the transaction associated with the current thread such that
* the only possible outcome of the transaction is to roll back the
* transaction.
*
* @exception IllegalStateException Thrown if the current thread is not
* associated with a transaction.
* @exception SystemException Thrown if the transaction manager encounters
* an unexpected error condition.
*/
public void setRollbackOnly()
throws IllegalStateException, SystemException {
Transaction currentTransaction = getTransaction();
if (currentTransaction == null)
throw new IllegalStateException();
String logMessage = Messages.format
(SlideTransactionManager.class.getName() + ".rollbackOnly",
currentTransaction.toString());
logger.log(logMessage, LOG_CHANNEL, Logger.INFO);
currentTransaction.setRollbackOnly();
}
/**
* Obtain the status of the transaction associated with the current thread.
*
* @exception SystemException Thrown if the transaction manager encounters
* an unexpected error condition.
* @return The transaction status. If no transaction is associated with
* the current thread, this method returns the Status.NoTransaction value.
*/
public int getStatus()
throws SystemException {
Transaction currentTransaction = getTransaction();
if (currentTransaction == null)
return Status.STATUS_NO_TRANSACTION;
return currentTransaction.getStatus();
}
/**
* Get the transaction object that represents the transaction context of
* the calling thread.
*
* @return the Transaction object representing the transaction associated
* with the calling thread.
* @exception SystemException Thrown if the transaction manager encounters
* an unexpected error condition.
*/
public Transaction getTransaction()
throws SystemException {
return (Transaction) bindings.get(Thread.currentThread());
}
/**
* Resume the transaction context association of the calling thread with
* the transaction represented by the supplied Transaction object. When
* this method returns, the calling thread is associated with the
* transaction context specified.
*
* @param tobj The Transaction object that represents the transaction to
* be resumed.
* @exception InvalidTransactionException Thrown if the parameter
* transaction object contains an invalid transaction.
* @exception IllegalStateException Thrown if the thread is already
* associated with another transaction.
* @exception SystemException Thrown if the transaction manager encounters
* an unexpected error condition.
*/
public void resume(Transaction tobj)
throws InvalidTransactionException, IllegalStateException,
SystemException {
if (getTransaction() != null)
throw new IllegalStateException();
if (tobj == null)
throw new InvalidTransactionException();
bindings.put(Thread.currentThread(), tobj);
}
/**
* Suspend the transaction currently associated with the calling thread
* and return a Transaction object that represents the transaction
* context being suspended. If the calling thread is not associated with
* a transaction, the method returns a null object reference. When this
* method returns, the calling thread is associated with no transaction.
*
* @return Transaction object representing the suspended transaction.
* @exception SystemException Thrown if the transaction manager encounters
* an unexpected error condition.
*/
public Transaction suspend()
throws SystemException {
Transaction currentTransaction = getTransaction();
if (currentTransaction != null) {
Thread currentThread = Thread.currentThread();
bindings.remove(currentThread);
timeouts.remove(currentThread);
}
return currentTransaction;
}
/**
* Modify the value of the timeout value that is associated with the
* transactions started by the current thread with the begin method.
* <p>
* If an application has not called this method, the transaction service
* uses some default value for the transaction timeout.
*
* @param seconds The value of the timeout in seconds. If the value is
* zero, the transaction service restores the default value.
* @exception SystemException Thrown if the transaction manager encounters
* an unexpected error condition.
*/
public void setTransactionTimeout(int seconds)
throws SystemException {
timeouts.put(Thread.currentThread(), new Integer(seconds));
}
}