mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1245 - Refactor internals - Add to TransactionManager externalRemoveTransaction() and externalBeginTransaction()
These provide a better/tidier way for external transaction managers (like ebean-spring-txn) to begin and end externally managed transactions.
This commit is contained in:
@@ -1,6 +1,29 @@
|
||||
package io.ebeaninternal.api;
|
||||
|
||||
import io.ebean.*;
|
||||
import io.ebean.AutoTune;
|
||||
import io.ebean.BackgroundExecutor;
|
||||
import io.ebean.BeanState;
|
||||
import io.ebean.CallableSql;
|
||||
import io.ebean.DocumentStore;
|
||||
import io.ebean.ExpressionFactory;
|
||||
import io.ebean.Filter;
|
||||
import io.ebean.FutureIds;
|
||||
import io.ebean.FutureList;
|
||||
import io.ebean.FutureRowCount;
|
||||
import io.ebean.PagedList;
|
||||
import io.ebean.PersistenceContextScope;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.QueryIterator;
|
||||
import io.ebean.SqlQuery;
|
||||
import io.ebean.SqlRow;
|
||||
import io.ebean.SqlUpdate;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.TransactionCallback;
|
||||
import io.ebean.TxScope;
|
||||
import io.ebean.Update;
|
||||
import io.ebean.UpdateQuery;
|
||||
import io.ebean.ValuePair;
|
||||
import io.ebean.Version;
|
||||
import io.ebean.annotation.TxIsolation;
|
||||
import io.ebean.bean.BeanCollection;
|
||||
import io.ebean.bean.CallStack;
|
||||
@@ -138,6 +161,11 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpiTransactionManager getTransactionManager() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BeanDescriptor<?>> getBeanDescriptors() {
|
||||
return null;
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package io.ebeaninternal.server.transaction;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.TxScope;
|
||||
import io.ebeaninternal.api.ScopedTransaction;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import io.ebeaninternal.api.SpiTransaction;
|
||||
import io.ebeaninternal.api.SpiTransactionManager;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tests.model.basic.Customer;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TransactionManagerTest extends BaseTestCase {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TransactionManagerTest.class);
|
||||
|
||||
@Test
|
||||
public void beginExternalTransaction() throws SQLException {
|
||||
|
||||
SpiEbeanServer server = spiEbeanServer();
|
||||
|
||||
SpiTransactionManager transactionManager = server.getTransactionManager();
|
||||
|
||||
DataSource dataSource = transactionManager.getDataSource();
|
||||
Connection connection = dataSource.getConnection();
|
||||
|
||||
SpiTransaction externalTxn = new ExternalJdbcTransaction("external0", true, connection, null);
|
||||
|
||||
// push an externally managed transaction onto scope
|
||||
ScopedTransaction scopedTransaction = transactionManager.externalBeginTransaction(externalTxn, TxScope.required());
|
||||
|
||||
Transaction current = Transaction.current();
|
||||
assertThat(current).as("external transaction is in scope").isSameAs(scopedTransaction);
|
||||
|
||||
Customer.find.byName("In external");
|
||||
|
||||
log.info("inner begin");
|
||||
Transaction inner = Ebean.beginTransaction();
|
||||
try {
|
||||
|
||||
current = Transaction.current();
|
||||
assertThat(current).as("still using external transaction, nested transaction pushed").isSameAs(scopedTransaction);
|
||||
|
||||
Customer.find.byName("In inner");
|
||||
log.info("inner commit");
|
||||
inner.commit();
|
||||
} finally {
|
||||
log.info("inner end");
|
||||
inner.end();
|
||||
}
|
||||
|
||||
current = Transaction.current();
|
||||
assertThat(current).as("external transaction still in scope").isSameAs(scopedTransaction);
|
||||
|
||||
Customer.find.byName("external still active");
|
||||
|
||||
|
||||
log.info("external transaction ends with commit and remove");
|
||||
connection.commit();
|
||||
// remove externally managed transaction out of scope
|
||||
transactionManager.externalRemoveTransaction();
|
||||
|
||||
|
||||
current = Transaction.current();
|
||||
assertThat(current).as("external transaction out of scope").isNull();
|
||||
|
||||
Customer.find.byNameStatus("New implicit transaction", Customer.Status.ACTIVE);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user