[#3229] Improved API
This commit is contained in:
parent
80dc9d197b
commit
c974a26894
@ -54,17 +54,29 @@ public interface TransactionContext {
|
||||
Configuration configuration();
|
||||
|
||||
/**
|
||||
* A user-defined transaction object, obtained from
|
||||
* A user-defined transaction object, possibly obtained from
|
||||
* {@link TransactionProvider#begin(TransactionContext)}.
|
||||
*
|
||||
* @return The transaction object. May be <code>null</code>.
|
||||
*/
|
||||
Transaction transaction();
|
||||
|
||||
/**
|
||||
* Set the user-defined transaction object to the current transaction
|
||||
* context.
|
||||
*/
|
||||
TransactionContext transaction(Transaction transaction);
|
||||
|
||||
/**
|
||||
* The exception that has caused the rollback.
|
||||
*
|
||||
* @return The exception. May be <code>null</code>.
|
||||
*/
|
||||
Exception cause();
|
||||
|
||||
/**
|
||||
* Set the exception that has caused the rollback to the current transaction
|
||||
* context.
|
||||
*/
|
||||
TransactionContext cause(Exception cause);
|
||||
}
|
||||
|
||||
@ -77,11 +77,10 @@ public interface TransactionProvider {
|
||||
*
|
||||
* @param Configuration the configuration scoped to this transaction and its
|
||||
* nested transactions.
|
||||
* @return A user-defined transaction object. May be <code>null</code>.
|
||||
* @throws DataAccessException Any exception issued by the underlying
|
||||
* database.
|
||||
*/
|
||||
Transaction begin(TransactionContext ctx) throws DataAccessException;
|
||||
void begin(TransactionContext ctx) throws DataAccessException;
|
||||
|
||||
/**
|
||||
* @param Configuration the configuration scoped to this transaction and its
|
||||
|
||||
@ -298,13 +298,12 @@ public class DefaultDSLContext implements DSLContext, Serializable {
|
||||
TransactionProvider provider = configuration.derive().transactionProvider();
|
||||
|
||||
try {
|
||||
ctx.transaction = provider.begin(ctx);
|
||||
provider.begin(ctx);
|
||||
result = transactional.run(configuration.derive());
|
||||
provider.commit(ctx);
|
||||
}
|
||||
catch (Exception cause) {
|
||||
ctx.cause = cause;
|
||||
provider.rollback(ctx);
|
||||
provider.rollback(ctx.cause(cause));
|
||||
|
||||
if (cause instanceof RuntimeException) {
|
||||
throw (RuntimeException) cause;
|
||||
|
||||
@ -67,8 +67,20 @@ class DefaultTransactionContext implements TransactionContext {
|
||||
return transaction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final TransactionContext transaction(Transaction t) {
|
||||
transaction = t;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Exception cause() {
|
||||
return cause;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final TransactionContext cause(Exception c) {
|
||||
cause = c;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,7 +50,6 @@ import java.util.Stack;
|
||||
|
||||
import org.jooq.Configuration;
|
||||
import org.jooq.ConnectionProvider;
|
||||
import org.jooq.Transaction;
|
||||
import org.jooq.TransactionContext;
|
||||
import org.jooq.TransactionProvider;
|
||||
|
||||
@ -109,7 +108,7 @@ public class DefaultTransactionProvider implements TransactionProvider {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Transaction begin(TransactionContext ctx) {
|
||||
public final void begin(TransactionContext ctx) {
|
||||
Stack<Savepoint> savepoints = savepoints(ctx.configuration());
|
||||
|
||||
// This is the top-level transaction
|
||||
@ -118,7 +117,6 @@ public class DefaultTransactionProvider implements TransactionProvider {
|
||||
}
|
||||
|
||||
savepoints.push(connection(ctx.configuration()).setSavepoint());
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -40,7 +40,6 @@
|
||||
*/
|
||||
package org.jooq.impl;
|
||||
|
||||
import org.jooq.Transaction;
|
||||
import org.jooq.TransactionContext;
|
||||
import org.jooq.TransactionProvider;
|
||||
|
||||
@ -52,7 +51,7 @@ import org.jooq.TransactionProvider;
|
||||
public class NoTransactionProvider implements TransactionProvider {
|
||||
|
||||
@Override
|
||||
public final Transaction begin(TransactionContext ctx) {
|
||||
public final void begin(TransactionContext ctx) {
|
||||
throw new UnsupportedOperationException("No transaction provider configured");
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user