Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
None
-
None
-
None
Description
Getting the semantics of manual transactions right is really hard: http://cayenne.apache.org/docs/3.0/understanding-transactions.html We need something more straightforward and DI based. Proposing the following:
public interface TransactionManager {
/**
- Starts a new transaction (or joins an existing one) calling
- {@link TransactionalOperation#perform(CayenneRuntime)}
, and then
- committing or rolling back the transaction. Frees the user
*/
<T> T performInTransaction(TransactionalOperation<T> op);
}
public interface TransactionalOperation<T> {
/**
- A callback method that
{@link TransactionManager} invokes,
* {@link TransactionManager}will wrap this method call in a single
- thread-bound transaction.
*/
T perform(CayenneRuntime runtime);
}
So a custom block of code to run in TX may look like this:
runtime.getTransactionManager().performInTransaction(new TransactionalOperation<SomeType>() {
SomeType perform(CayenneRuntime runtime)
});
or maybe even:
runtime.performInTransaction(new TransactionalOperation<SomeType>() {
SomeType perform(CayenneRuntime runtime) { // do something }
});
Later we may even use TransactionManager API for internal tx's to achieve uniformity. I can also see the evolution of the current Transaction API (including static bind methods) to gradually move under TransactionManager methods.