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>
WARNING: For most of the methods in this class, it is up to you to properly synchronize them if multiple threads are using a single database connection -- this includes a connection-source which works gives out a single-connection. The reason why this is necessary is that multiple operations are performed on the connection and race-conditions will exist with multiple threads working on the same connection.
Constructor and Description |
---|
TransactionManager()
Constructor for Spring type wiring if you are using the set methods.
|
TransactionManager(ConnectionSource connectionSource)
Constructor for direct java code wiring.
|
Modifier and Type | Method and Description |
---|---|
<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 with database-type. |
static <T> T |
callInTransaction(DatabaseConnection connection,
DatabaseType databaseType,
Callable<T> callable)
Same as
callInTransaction(Callable) except as a static method on a connection with database-type. |
<T> T |
callInTransaction(String tableName,
Callable<T> callable)
Same as
callInTransaction(Callable) except as a this has a table-name specified. |
static <T> T |
callInTransaction(String tableName,
ConnectionSource connectionSource,
Callable<T> callable)
Same as
callInTransaction(ConnectionSource, Callable) except this has a table-name. |
void |
initialize()
If you are using the Spring type wiring, this should be called after all of the set methods.
|
void |
setConnectionSource(ConnectionSource connectionSource) |
public TransactionManager()
public TransactionManager(ConnectionSource connectionSource)
public void initialize()
public <T> T callInTransaction(Callable<T> callable) throws SQLException
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.
WARNING: it is up to you to properly synchronize around this method if multiple threads are using a connection-source which works gives out a single-connection. The reason why this is necessary is that multiple operations are performed on the connection and race-conditions will exist with multiple threads working on the same connection.
callable
- Callable to execute inside of the transaction.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.public <T> T callInTransaction(String tableName, Callable<T> callable) throws SQLException
callInTransaction(Callable)
except as a this has a table-name specified.
WARNING: it is up to you to properly synchronize around this method if multiple threads are using a connection-source which works gives out a single-connection. The reason why this is necessary is that multiple operations are performed on the connection and race-conditions will exist with multiple threads working on the same connection.
SQLException
public static <T> T callInTransaction(ConnectionSource connectionSource, Callable<T> callable) throws SQLException
callInTransaction(Callable)
except as a static method with a connection source.
WARNING: it is up to you to properly synchronize around this method if multiple threads are using a connection-source which works gives out a single-connection. The reason why this is necessary is that multiple operations are performed on the connection and race-conditions will exist with multiple threads working on the same connection.
SQLException
public static <T> T callInTransaction(String tableName, ConnectionSource connectionSource, Callable<T> callable) throws SQLException
callInTransaction(ConnectionSource, Callable)
except this has a table-name.
WARNING: it is up to the caller to properly synchronize around this method if multiple threads are using a connection-source which works gives out a single-connection. The reason why this is necessary is that multiple operations are performed on the connection and race-conditions will exist with multiple threads working on the same connection.
SQLException
public static <T> T callInTransaction(DatabaseConnection connection, DatabaseType databaseType, Callable<T> callable) throws SQLException
callInTransaction(Callable)
except as a static method on a connection with database-type.
WARNING: it is up to the caller to properly synchronize around this method if multiple threads are using the same database connection or if your connection-source is single-connection. The reason why this is necessary is that multiple operations are performed on the connection and race-conditions will exist with multiple threads working on the same connection.
SQLException
public static <T> T callInTransaction(DatabaseConnection connection, boolean saved, DatabaseType databaseType, Callable<T> callable) throws SQLException
callInTransaction(Callable)
except as a static method on a connection with database-type.
WARNING: it is up to you to properly synchronize around this method if multiple threads are using the same database connection or if your connection-source is single-connection. The reason why this is necessary is that multiple operations are performed on the connection and race-conditions will exist with multiple threads working on the same connection.
SQLException
public void setConnectionSource(ConnectionSource connectionSource)
This documentation is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.