Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: nested NOT_SUPPORTED transaction with an inner REQUIRES transaction #3288

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import io.ebeanservice.docstore.api.DocStoreTransaction;
import io.ebeanservice.docstore.api.DocStoreUpdateProcessor;
import io.ebeanservice.docstore.api.DocStoreUpdates;

import jakarta.persistence.PersistenceException;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
Expand Down Expand Up @@ -176,13 +176,6 @@ public final SpiTransaction active() {
return scopeManager.active();
}

/**
* Return the current active transaction as a scoped transaction.
*/
private ScopedTransaction activeScoped() {
return (ScopedTransaction) scopeManager.active();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not longer in use


/**
* Return the current transaction from thread local scope. Note that it may be inactive.
*/
Expand Down Expand Up @@ -499,7 +492,7 @@ public final ScopedTransaction externalBeginTransaction(SpiTransaction transacti
*/
public final ScopedTransaction beginScopedTransaction(TxScope txScope) {
txScope = initTxScope(txScope);
ScopedTransaction txnContainer = activeScoped();
ScopedTransaction txnContainer = (ScopedTransaction) inScope();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible (or allowed) that we will get an instance not implementing ScopedTransaction here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if a DB.save(entityWithPersistAction) creates an implicit transaction, we will fail here, if a callback or @PrePersist action tries to create a nested transaction, because implicit transactions are not nestable


boolean setToScope;
boolean nestedSavepoint;
Expand Down Expand Up @@ -594,6 +587,7 @@ private TxScope initTxScope(TxScope txScope) {
private boolean isCreateNewTransaction(SpiTransaction current, TxType type) {
switch (type) {
case REQUIRED:
return current == null || !current.isActive();
case SUPPORTS:
return current == null;
case REQUIRES_NEW:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.tests.transaction;

import io.ebean.xtest.BaseTestCase;
import io.ebean.DB;
import io.ebean.Transaction;
import io.ebean.TxScope;
import io.ebean.xtest.BaseTestCase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
Expand Down Expand Up @@ -235,4 +236,30 @@ public void testNested_111() {
}
assertModified();
}

@Test
public void test_txn_with_not_supported() {

try (Transaction txn1 = DB.beginTransaction()) {
assertThat(getInScopeTransaction()).isNotNull();
getInScopeTransaction().putUserObject("foo", "bar");
assertThat(Transaction.current()).isNotNull();

try (Transaction txn2 = DB.beginTransaction(TxScope.notSupported())) {
// pause txn1
assertThat(Transaction.current()).isNull();

try (Transaction txn3 = DB.beginTransaction()) {
// create a new Txn scope
assertThat(Transaction.current()).isNotNull();
DB.save(new EBasic());
txn3.commit();
}
DB.save(new EBasic());
txn2.commit();
}
// resume txn1
assertThat(getInScopeTransaction().getUserObject("foo")).isEqualTo("bar");
}
}
}
Loading