org.apache.torque.util
Class Transaction

java.lang.Object
  extended by org.apache.torque.util.Transaction

public final class Transaction
extends java.lang.Object

Refactored begin/commit/rollback transaction methods away from the BasePeer.

This can be used to handle cases where transaction support is optional. The second parameter of beginOptionalTransaction will determine with a transaction is used or not. If a transaction is not used, the commit and rollback methods do not have any effect. Instead it simply makes the logic easier to follow by cutting down on the if statements based solely on whether a transaction is needed or not.

Version:
$Id: Transaction.java 476550 2006-11-18 16:08:37Z tfischer $
Author:
Stephen Haberman

Method Summary
static java.sql.Connection begin()
          Begin a transaction for the default database.
static java.sql.Connection begin(java.lang.String dbName)
          Begin a transaction.
static java.sql.Connection beginOptional(java.lang.String dbName, boolean useTransaction)
          Begin a transaction.
static void commit(java.sql.Connection con)
          Commit a transaction.
static void rollback(java.sql.Connection con)
          Roll back a transaction in databases that support transactions.
static void safeRollback(java.sql.Connection con)
          Roll back a transaction without throwing errors if they occur.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

begin

public static java.sql.Connection begin()
                                 throws TorqueException
Begin a transaction for the default database. This method will fallback gracefully to return a normal connection, if the database being accessed does not support transactions.

Returns:
The Connection for the transaction.
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

begin

public static java.sql.Connection begin(java.lang.String dbName)
                                 throws TorqueException
Begin a transaction. This method will fallback gracefully to return a normal connection, if the database being accessed does not support transactions.

Parameters:
dbName - Name of database.
Returns:
The Connection for the transaction.
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

beginOptional

public static java.sql.Connection beginOptional(java.lang.String dbName,
                                                boolean useTransaction)
                                         throws TorqueException
Begin a transaction. This method will fallback gracefully to return a normal connection, if the database being accessed does not support transactions.

Parameters:
dbName - Name of database.
useTransaction - If false, a transaction won't be used.
Returns:
The Connection for the transaction.
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

commit

public static void commit(java.sql.Connection con)
                   throws TorqueException
Commit a transaction. This method takes care of releasing the connection after the commit. In databases that do not support transactions, it only returns the connection.

Parameters:
con - The Connection for the transaction.
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

rollback

public static void rollback(java.sql.Connection con)
                     throws TorqueException
Roll back a transaction in databases that support transactions. It also releases the connection. In databases that do not support transactions, this method will log the attempt and release the connection.

Parameters:
con - The Connection for the transaction.
Throws:
TorqueException - Any exceptions caught during processing will be rethrown wrapped into a TorqueException.

safeRollback

public static void safeRollback(java.sql.Connection con)
Roll back a transaction without throwing errors if they occur. A null Connection argument is logged at the debug level and other errors are logged at warn level.

Parameters:
con - The Connection for the transaction.
See Also:
rollback(Connection)


Copyright © 2000-2012 Apache Software Foundation. All Rights Reserved.