com.j256.ormlite.misc
Class TransactionManager

java.lang.Object
  extended by com.j256.ormlite.misc.TransactionManager

public class TransactionManager
extends Object

Provides basic transaction support for a ConnectionSource.

NOTE: For transactions to work, the database being used must support the functionality.

NOTE: If you are using the Spring type wiring in Java, initialize() should be called after all of the set methods. In Spring XML, init-method="initialize" should be used.

You can call this as an instance with a new TransactionManager(dataSource); or you can call it as a static like the below example:

 TransactionManager.callInTransaction(dataSource, new Callable<Void>() {
        public Void call() throws Exception {
                // delete both objects but make sure that if either one fails, the transaction is rolled back
                // and both objects are "restored" to the database
                fooDao.delete(foo);
                barDao.delete(bar);
                return null;
        }
 });
 

For Spring wiring of a Transaction Manager bean, we would do something like the following:

 <bean id="transactionManager" class="com.j256.ormlite.misc.TransactionManager" init-method="initialize">
        <property name="dataSource" ref="dataSource" />
 </bean>
 

Author:
graywatson

Constructor Summary
TransactionManager()
          Constructor for Spring type wiring if you are using the set methods.
TransactionManager(ConnectionSource connectionSource)
          Constructor for direct java code wiring.
 
Method Summary
<T> T
callInTransaction(Callable<T> callable)
          Execute the Callable class inside of a transaction.
static
<T> T
callInTransaction(ConnectionSource connectionSource, Callable<T> callable)
          Same as callInTransaction(Callable) except as a static method with a connection source.
static
<T> T
callInTransaction(DatabaseConnection connection, boolean saved, DatabaseType databaseType, Callable<T> callable)
          Same as callInTransaction(Callable) except as a static method on a connection.
static
<T> T
callInTransaction(DatabaseConnection connection, DatabaseType databaseType, Callable<T> callable)
          Same as callInTransaction(Callable) except as a static method on a connection.
 void initialize()
          If you are using the Spring type wiring, this should be called after all of the set methods.
 void setConnectionSource(ConnectionSource connectionSource)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TransactionManager

public TransactionManager()
Constructor for Spring type wiring if you are using the set methods.


TransactionManager

public TransactionManager(ConnectionSource connectionSource)
Constructor for direct java code wiring.

Method Detail

initialize

public void initialize()
If you are using the Spring type wiring, this should be called after all of the set methods.


callInTransaction

public <T> T callInTransaction(Callable<T> callable)
                    throws SQLException
Execute the Callable class inside of a transaction. If the callable returns then the transaction is committed. If the callable throws an exception then the transaction is rolled back and a SQLException is thrown by this method.

NOTE: If your callable block really doesn't have a return object then use the Void class and return null from the call method.

Parameters:
callable - Callable to execute inside of the transaction.
Returns:
The object returned by the callable.
Throws:
SQLException - If the callable threw an exception then the transaction is rolled back and a SQLException wraps the callable exception and is thrown by this method.

callInTransaction

public static <T> T callInTransaction(ConnectionSource connectionSource,
                                      Callable<T> callable)
                           throws SQLException
Same as callInTransaction(Callable) except as a static method with a connection source.

Throws:
SQLException

callInTransaction

public static <T> T callInTransaction(DatabaseConnection connection,
                                      DatabaseType databaseType,
                                      Callable<T> callable)
                           throws SQLException
Same as callInTransaction(Callable) except as a static method on a connection.

Throws:
SQLException

callInTransaction

public static <T> T callInTransaction(DatabaseConnection connection,
                                      boolean saved,
                                      DatabaseType databaseType,
                                      Callable<T> callable)
                           throws SQLException
Same as callInTransaction(Callable) except as a static method on a connection.

Throws:
SQLException

setConnectionSource

public void setConnectionSource(ConnectionSource connectionSource)


This documentation is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.