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 particular DataSource.

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.

 TransactionManager transactionMgr = new TransactionManager(dataSource);
 ...
 mgr.callInTransaction(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(DataSource dataSource)
          Constructor for direct java code wiring.
 
Method Summary
<T> T
callInTransaction(Callable<T> callable)
          Execute the Callable class inside of a transaction.
 void initialize()
          If you are using the Spring type wiring, this should be called after all of the set methods.
 void setDataSource(DataSource dataSource)
           
 
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(DataSource dataSource)
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.

setDataSource

public void setDataSource(DataSource dataSource)


Copyright © 2010. All Rights Reserved.