[#3229] Improved API

This commit is contained in:
Lukas Eder 2014-05-06 15:02:14 +02:00
parent 80dc9d197b
commit c974a26894
6 changed files with 30 additions and 11 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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

View File

@ -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");
}