Skip to content

Commit

Permalink
Introduce TransactionOperations.execute(Runnable) convenience method
Browse files Browse the repository at this point in the history
Closes gh-23250
  • Loading branch information
jhoeller committed Jul 17, 2019
1 parent 7f33344 commit 2c33c11
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ public interface TransactionOperations {
@Nullable
<T> T execute(TransactionCallback<T> action) throws TransactionException;

/**
* Execute the action specified by the given {@link Runnable} within a transaction.
* <p>If you need to return an object from the callback or access the
* {@link org.springframework.transaction.TransactionStatus} from within the callback,
* use {@link #execute(TransactionCallback)} instead.
* <p>This variant is analogous to using a {@link TransactionCallbackWithoutResult}
* but with a simplified signature for common cases - and conveniently usable with
* Java 8 lambda expressions.
* @param action the Runnable that specifies the transactional action
* @throws TransactionException in case of initialization, rollback, or system errors
* @throws RuntimeException if thrown by the Runnable
* @since 5.2
* @see #execute(TransactionCallback)
* @see TransactionCallbackWithoutResult
*/
default void execute(Runnable action) throws TransactionException {
execute(status -> {
action.run();
return null;
});
}


/**
* Return an implementation of the {@code TransactionOperations} interface which
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@ final class WithoutTransactionOperations implements TransactionOperations {

static final WithoutTransactionOperations INSTANCE = new WithoutTransactionOperations();


private WithoutTransactionOperations() {
}


@Override
@Nullable
public <T> T execute(TransactionCallback<T> action) throws TransactionException {
return action.doInTransaction(new SimpleTransactionStatus(false));
}

@Override
public void execute(Runnable action) throws TransactionException {
action.run();
}

}

0 comments on commit 2c33c11

Please sign in to comment.