use of io.cdap.cdap.api.Transactional in project cdap by caskdata.
the class SearchHelper method createTransactional.
/**
* Create a transactional for metadata datasets.
*
* @param txClient transaction client
* @return transactional for the metadata tables
*/
private Transactional createTransactional(TransactionSystemClient txClient) {
return new Transactional() {
@Override
public void execute(io.cdap.cdap.api.TxRunnable runnable) throws TransactionFailureException {
TransactionContext txContext = new TransactionContext(txClient);
try (MetaOnlyDatasetContext datasetContext = new MetaOnlyDatasetContext(txContext)) {
txContext.start();
finishExecute(txContext, datasetContext, runnable);
} catch (Exception e) {
Throwables.propagateIfPossible(e, TransactionFailureException.class);
}
}
@Override
public void execute(int timeout, io.cdap.cdap.api.TxRunnable runnable) throws TransactionFailureException {
TransactionContext txContext = new TransactionContext(txClient);
try (MetaOnlyDatasetContext datasetContext = new MetaOnlyDatasetContext(txContext)) {
txContext.start(timeout);
finishExecute(txContext, datasetContext, runnable);
} catch (Exception e) {
Throwables.propagateIfPossible(e, TransactionFailureException.class);
}
}
private void finishExecute(TransactionContext txContext, io.cdap.cdap.api.data.DatasetContext dsContext, io.cdap.cdap.api.TxRunnable runnable) throws TransactionFailureException {
try {
runnable.run(dsContext);
} catch (Exception e) {
txContext.abort(new TransactionFailureException("Exception raised from TxRunnable.run() " + runnable, e));
}
// The call the txContext.abort above will always have exception thrown
// Hence we'll only reach here if and only if the runnable.run() returns normally.
txContext.finish();
}
};
}
use of io.cdap.cdap.api.Transactional in project cdap by caskdata.
the class NoSQLTransactionals method createTransactional.
/**
* Create a transactional for an entity table. The regular {@link io.cdap.cdap.api.Transactionals} class cannot be
* used due to cyclic dependency between dataset service and NoSQL StructuredTable.
*
* @param txClient transaction client
* @param datasetSupplier supplies the dataset for the entity table
* @return transactional for the entity table
*/
public static Transactional createTransactional(TransactionSystemClient txClient, TableDatasetSupplier datasetSupplier) {
return new Transactional() {
@Override
public void execute(io.cdap.cdap.api.TxRunnable runnable) throws TransactionFailureException {
TransactionContext txContext = new TransactionContext(txClient);
try (EntityTableDatasetContext datasetContext = new EntityTableDatasetContext(txContext, datasetSupplier)) {
txContext.start();
finishExecute(txContext, datasetContext, runnable);
} catch (Exception e) {
Throwables.propagateIfPossible(e, TransactionFailureException.class);
}
}
@Override
public void execute(int timeout, io.cdap.cdap.api.TxRunnable runnable) throws TransactionFailureException {
TransactionContext txContext = new TransactionContext(txClient);
try (EntityTableDatasetContext datasetContext = new EntityTableDatasetContext(txContext, datasetSupplier)) {
txContext.start(timeout);
finishExecute(txContext, datasetContext, runnable);
} catch (Exception e) {
Throwables.propagateIfPossible(e, TransactionFailureException.class);
}
}
private void finishExecute(TransactionContext txContext, DatasetContext dsContext, io.cdap.cdap.api.TxRunnable runnable) throws TransactionFailureException {
try {
runnable.run(dsContext);
} catch (Exception e) {
txContext.abort(new TransactionFailureException("Exception raised from TxRunnable.run() " + runnable, e));
}
// The call the txContext.abort above will always have exception thrown
// Hence we'll only reach here if and only if the runnable.run() returns normally.
txContext.finish();
}
};
}
use of io.cdap.cdap.api.Transactional in project cdap by caskdata.
the class AbstractContext method execute.
/**
* Execute in a transaction with optional retry on conflict.
*/
public void execute(final TxRunnable runnable, boolean retryOnConflict) throws TransactionFailureException {
ClassLoader oldClassLoader = ClassLoaders.setContextClassLoader(getClass().getClassLoader());
try {
Transactional txnl = retryOnConflict ? Transactions.createTransactionalWithRetry(transactional, RetryStrategies.retryOnConflict(20, 100)) : transactional;
txnl.execute(context -> {
ClassLoader oldClassLoader1 = ClassLoaders.setContextClassLoader(getProgramInvocationClassLoader());
try {
runnable.run(context);
} finally {
ClassLoaders.setContextClassLoader(oldClassLoader1);
}
});
} finally {
ClassLoaders.setContextClassLoader(oldClassLoader);
}
}
Aggregations