#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:
Rob Bygrave
2018-01-31 13:28:42 +13:00
parent 71f66005e7
commit f9cd955f7a
7 changed files with 178 additions and 3 deletions
@@ -80,6 +80,11 @@ public interface SpiEbeanServer extends EbeanServer, BeanLoader, BeanCollectionL
*/
void clearQueryStatistics();
/**
* Return the transaction manager.
*/
SpiTransactionManager getTransactionManager();
/**
* Return all the descriptors.
*/
@@ -0,0 +1,37 @@
package io.ebeaninternal.api;
import io.ebean.TxScope;
import javax.sql.DataSource;
/**
* Service provider interface for the transaction manager.
*/
public interface SpiTransactionManager {
/**
* Return the main DataSource.
*/
DataSource getDataSource();
/**
* Return the read only DataSource (if defined).
*/
DataSource getReadOnlyDataSource();
/**
* Return the currently active transaction (can be null).
*/
SpiTransaction get();
/**
* Push an externally managed transaction into scope (e.g. Spring managed transaction).
*/
ScopedTransaction externalBeginTransaction(SpiTransaction transaction, TxScope txScope);
/**
* Called when an externally managed transaction has completed.
*/
void externalRemoveTransaction();
}
@@ -61,6 +61,7 @@ import io.ebeaninternal.api.SpiJsonContext;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.api.SpiQuery.Type;
import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.api.SpiTransactionManager;
import io.ebeaninternal.api.TransactionEventTable;
import io.ebeaninternal.dbmigration.DdlGenerator;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlHandler;
@@ -1879,6 +1880,14 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
return beanDescriptorManager.getBeanDescriptorList();
}
/**
* Return the transaction manager.
*/
@Override
public SpiTransactionManager getTransactionManager() {
return transactionManager;
}
public void register(BeanPersistController c) {
List<BeanDescriptor<?>> list = beanDescriptorManager.getBeanDescriptorList();
for (BeanDescriptor<?> aList : list) {
@@ -16,6 +16,7 @@ import io.ebeaninternal.api.ScopeTrans;
import io.ebeaninternal.api.ScopedTransaction;
import io.ebeaninternal.api.SpiProfileHandler;
import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.api.SpiTransactionManager;
import io.ebeaninternal.api.TransactionEvent;
import io.ebeaninternal.api.TransactionEventTable;
import io.ebeaninternal.api.TransactionEventTable.TableIUD;
@@ -46,7 +47,7 @@ import java.util.Set;
* Keeps the Cache and Cluster in synch when transactions are committed.
* </p>
*/
public class TransactionManager {
public class TransactionManager implements SpiTransactionManager {
private static final Logger logger = LoggerFactory.getLogger(TransactionManager.class);
@@ -558,6 +559,23 @@ public class TransactionManager {
}
}
@Override
public void externalRemoveTransaction() {
scopeManager.replace(null);
}
/**
* Push an externally created transaction into scope. This transaction is usually managed externally
* (e.g. Spring managed transaction).
*/
@Override
public ScopedTransaction externalBeginTransaction(SpiTransaction transaction, TxScope txScope) {
ScopedTransaction scopedTxn = new ScopedTransaction(scopeManager);
scopedTxn.push(new ScopeTrans(rollbackOnChecked, false, transaction, txScope));
scopeManager.set(scopedTxn);
return scopedTxn;
}
/**
* Begin a scoped transaction.
*/