[#3229] Add DSLContext.transaction() to implement nested transaction semantics through functional interfaces

This commit is contained in:
Lukas Eder 2014-05-12 15:14:08 +02:00
parent a9021a493a
commit d2123f1c99
7 changed files with 179 additions and 9 deletions

View File

@ -181,7 +181,7 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
create.configuration().set(provider);
Integer result =
create.transaction(c1 -> {
create.transactionResult(c1 -> {
assertAutoCommit(c1.connectionProvider(), false);
inserted[0] =
@ -236,7 +236,7 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
create.configuration().set(provider);
try {
create.transaction(c1 -> {
create.transactionResult(c1 -> {
assertAutoCommit(c1.connectionProvider(), false);
inserted[0] =

View File

@ -166,15 +166,40 @@ public interface DSLContext {
// -------------------------------------------------------------------------
/**
* Run a {@link Transactional} in the context of this
* Run a {@link TransactionalCallable} in the context of this
* <code>DSLContext</code>'s underlying {@link #configuration()}'s
* {@link Configuration#transactionProvider()}, and return the
* <code>transactional</code>'s outcome.
* <p>
* Both javac and Eclipse compilers contain bugs when overloading methods
* that take both "void-compatible" and "value-compatible" functional
* interfaces:
* <ul>
* <li><a
* href="https://bugs.openjdk.java.net/browse/JDK-8029718">JDK-8029718</a></li>
* <li><a
* href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=434642">Eclipse
* 434642</a></li>
* </ul>
* This is why this method was renamed to <code>transactionResult()</code>.
* Future versions of jOOQ may create a better synonym for this, called
* <code>transaction()</code>, which doesn't conflict with
* {@link #transaction(TransactionalRunnable)}
*
* @param transactional The transactional code
* @return The transactional outcome
*/
<T> T transactionResult(TransactionalCallable<T> transactional);
/**
* Run a {@link TransactionalCallable} in the context of this
* <code>DSLContext</code>'s underlying {@link #configuration()}'s
* {@link Configuration#transactionProvider()}, and return the
* <code>transactional</code>'s outcome.
*
* @param transactional The transactional code
* @return The transactional outcome
*/
<T> T transaction(Transactional<T> transactional);
void transaction(TransactionalRunnable transactional);
// -------------------------------------------------------------------------
// XXX RenderContext and BindContext accessors

View File

@ -48,7 +48,8 @@ import org.jooq.impl.DefaultTransactionProvider;
/**
* The <code>TransactionProvider</code> SPI can be used to implement custom
* <code>transaction</code> behaviour that is applied when calling
* {@link DSLContext#transaction(Transactional)}.
* {@link DSLContext#transactionResult(TransactionalCallable)} or
* {@link DSLContext#transaction(TransactionalRunnable)}.
* <p>
* A new {@link Configuration} copy is created from the calling
* {@link DSLContext} for the scope of a single transactions. Implementors may

View File

@ -0,0 +1,67 @@
/**
* Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* This work is dual-licensed
* - under the Apache Software License 2.0 (the "ASL")
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
* =============================================================================
* You may choose which license applies to you:
*
* - If you're using this work with Open Source databases, you may choose
* either ASL or jOOQ License.
* - If you're using this work with at least one commercial database, you must
* choose jOOQ License
*
* For more information, please visit http://www.jooq.org/licenses
*
* Apache Software License 2.0:
* -----------------------------------------------------------------------------
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* jOOQ License and Maintenance Agreement:
* -----------------------------------------------------------------------------
* Data Geekery grants the Customer the non-exclusive, timely limited and
* non-transferable license to install and use the Software under the terms of
* the jOOQ License and Maintenance Agreement.
*
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
*/
package org.jooq;
/**
* An <code>FunctionalInterface</code> that wraps transactional code.
*
* @author Lukas Eder
*/
public interface TransactionalCallable<T> {
/**
* Run the transactional code.
* <p>
* If this method completes normally, and this is not a nested transaction,
* then the transaction will be committed. If this method completes with an
* exception, then the transaction is rolled back to the beginning of this
* <code>TransactionalCallable</code>.
*
* @param configuration The <code>Configuration</code> in whose context the
* transaction is run.
* @return The outcome of the transaction.
* @throws Exception Any exception that will cause a rollback of the code
* contained in this transaction. If this is a nested
* transaction, the rollback may be performed only to the state
* before executing this <code>TransactionalCallable</code>.
*/
T run(Configuration configuration) throws Exception;
}

View File

@ -0,0 +1,66 @@
/**
* Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* This work is dual-licensed
* - under the Apache Software License 2.0 (the "ASL")
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
* =============================================================================
* You may choose which license applies to you:
*
* - If you're using this work with Open Source databases, you may choose
* either ASL or jOOQ License.
* - If you're using this work with at least one commercial database, you must
* choose jOOQ License
*
* For more information, please visit http://www.jooq.org/licenses
*
* Apache Software License 2.0:
* -----------------------------------------------------------------------------
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* jOOQ License and Maintenance Agreement:
* -----------------------------------------------------------------------------
* Data Geekery grants the Customer the non-exclusive, timely limited and
* non-transferable license to install and use the Software under the terms of
* the jOOQ License and Maintenance Agreement.
*
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
*/
package org.jooq;
/**
* An <code>FunctionalInterface</code> that wraps transactional code.
*
* @author Lukas Eder
*/
public interface TransactionalRunnable {
/**
* Run the transactional code.
* <p>
* If this method completes normally, and this is not a nested transaction,
* then the transaction will be committed. If this method completes with an
* exception, then the transaction is rolled back to the beginning of this
* <code>TransactionalRunnable</code>.
*
* @param configuration The <code>Configuration</code> in whose context the
* transaction is run.
* @throws Exception Any exception that will cause a rollback of the code
* contained in this transaction. If this is a nested
* transaction, the rollback may be performed only to the state
* before executing this <code>TransactionalRunnable</code>.
*/
void run(Configuration configuration) throws Exception;
}

View File

@ -181,7 +181,8 @@ import org.jooq.Table;
import org.jooq.TableLike;
import org.jooq.TableRecord;
import org.jooq.TransactionProvider;
import org.jooq.Transactional;
import org.jooq.TransactionalCallable;
import org.jooq.TransactionalRunnable;
import org.jooq.TruncateIdentityStep;
import org.jooq.UDT;
import org.jooq.UDTRecord;
@ -295,7 +296,7 @@ public class DefaultDSLContext implements DSLContext, Serializable {
// -------------------------------------------------------------------------
@Override
public <T> T transaction(Transactional<T> transactional) {
public <T> T transactionResult(TransactionalCallable<T> transactional) {
T result = null;
DefaultTransactionContext ctx = new DefaultTransactionContext(configuration.derive());
@ -320,6 +321,17 @@ public class DefaultDSLContext implements DSLContext, Serializable {
return result;
}
@Override
public void transaction(final TransactionalRunnable transactional) {
transactionResult(new TransactionalCallable<Void>() {
@Override
public Void run(Configuration c) throws Exception {
transactional.run(c);
return null;
}
});
}
// -------------------------------------------------------------------------
// XXX RenderContext and BindContext accessors
// -------------------------------------------------------------------------

View File

@ -63,7 +63,6 @@ import static org.jooq.impl.DSL.replace;
import static org.jooq.impl.DSL.round;
import static org.jooq.impl.DSL.row;
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.DSL.selectOne;
import static org.jooq.impl.DSL.sum;
import static org.jooq.impl.DSL.tableByName;
import static org.jooq.impl.DSL.trueCondition;