Hi!
After digging some more around I realized that TransactionDefinition
basically holds two enums, TransactionPropagationBehaviour and
TransactionIsolationLevel, both mixed into consts. Really confusing as the
DefaultTransactionDefinition only takes propagation behaviour in the ctor.
So having this in mind then the proper refactoring of the
SpringTransactionProvider should be something like this:
/**
* An example <code>TransactionProvider</code> implementing the {@link
TransactionProvider} contract for use with
* Spring.
*
* @author Lukas Eder
*/
public class SpringTransactionProvider extends ThreadLocalTransactionProvider
implements TransactionProvider {
private static final JooqLogger LOGGER =
JooqLogger.getLogger(SpringTransactionProvider.class);
private DataSourceTransactionManager txMgr;
private int propagationBehavior;
private int isolationLevel;
public SpringTransactionProvider(ConnectionProvider provider,
DataSourceTransactionManager txMgr) {
super(provider);
this.txMgr = txMgr;
this.propagationBehavior = PROPAGATION_NESTED;
this.isolationLevel = ISOLATION_DEFAULT;
}
public SpringTransactionProvider(ConnectionProvider provider,
DataSourceTransactionManager txMgr, int propagationBehavior, int
isolationLevel) {
super(provider);
this.txMgr = txMgr;
this.propagationBehavior = propagationBehavior;
this.isolationLevel = isolationLevel;
}
@Override
public void begin(TransactionContext ctx) {
LOGGER.info("Begin transaction");
// This TransactionProvider behaves like jOOQ's
DefaultTransactionProvider,
// which supports nested transactions using Savepoints
DefaultTransactionDefinition def = new
DefaultTransactionDefinition(propagationBehavior);
if(isolationLevel != ISOLATION_DEFAULT) {
def.setIsolationLevel(isolationLevel);
}
TransactionStatus tx = txMgr.getTransaction(def);
ctx.transaction(new SpringTransaction(tx));
}
@Override
public void commit(TransactionContext ctx) {
LOGGER.info("commit transaction");
txMgr.commit(((SpringTransaction) ctx.transaction()).tx);
}
@Override
public void rollback(TransactionContext ctx) {
LOGGER.info("rollback transaction");
txMgr.rollback(((SpringTransaction) ctx.transaction()).tx);
}
public SpringTransactionProvider derive(ConnectionProvider provider, int
propagationBehavior, int isolationLevel) {
return new SpringTransactionProvider(provider, txMgr,
propagationBehavior, isolationLevel);
}
}
Now both propagation behaviour and isolation level can be specified in
special situations.
I use it like this:
try (DSLContext ctx =
dbCon.useCustomTransaction(TransactionDefinition.PROPAGATION_NESTED,
TransactionDefinition.ISOLATION_SERIALIZABLE)) {
ctx.transaction(() -> savedResult.set(
saveEntity(ctx, entity))
);
return savedResult.get();
}
Hope this is the correct way forward.
Cheers,
Steinar.
--
You received this message because you are subscribed to the Google Groups "jOOQ
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.