use of io.micronaut.transaction.exceptions.TransactionSystemException in project micronaut-data by micronaut-projects.
the class DefaultReactiveMongoRepositoryOperations method withTransaction.
@Override
@NonNull
public <T> Publisher<T> withTransaction(@NonNull ReactiveTransactionStatus<ClientSession> status, @NonNull ReactiveTransactionOperations.TransactionalCallback<ClientSession, T> handler) {
Objects.requireNonNull(status, "Transaction status cannot be null");
Objects.requireNonNull(handler, "Callback handler cannot be null");
return Flux.defer(() -> {
try {
return handler.doInTransaction(status);
} catch (Exception e) {
return Flux.error(new TransactionSystemException("Error invoking doInTransaction handler: " + e.getMessage(), e));
}
}).contextWrite(context -> context.put(ReactiveTransactionStatus.STATUS, status));
}
use of io.micronaut.transaction.exceptions.TransactionSystemException in project micronaut-data by micronaut-projects.
the class DataSourceTransactionManager method doRollback.
@Override
protected void doRollback(DefaultTransactionStatus status) {
DataSourceTransactionObject txObject = (DataSourceTransactionObject) status.getTransaction();
Connection con = txObject.getConnectionHolder().getConnection();
if (status.isDebug()) {
logger.debug("Rolling back JDBC transaction on Connection [" + con + "]");
}
try {
con.rollback();
} catch (SQLException ex) {
throw new TransactionSystemException("Could not roll back JDBC transaction", ex);
}
}
use of io.micronaut.transaction.exceptions.TransactionSystemException in project micronaut-data by micronaut-projects.
the class DefaultR2dbcRepositoryOperations method withTransaction.
@Override
@NonNull
public <T> Flux<T> withTransaction(@NonNull TransactionDefinition definition, @NonNull ReactiveTransactionOperations.TransactionalCallback<Connection, T> handler) {
Objects.requireNonNull(definition, "Transaction definition cannot be null");
Objects.requireNonNull(handler, "Callback handler cannot be null");
return Flux.deferContextual(contextView -> {
Object o = contextView.getOrDefault(ReactiveTransactionStatus.STATUS, null);
TransactionDefinition.Propagation propagationBehavior = definition.getPropagationBehavior();
if (o instanceof ReactiveTransactionStatus) {
// existing transaction, use it
if (propagationBehavior == TransactionDefinition.Propagation.NOT_SUPPORTED || propagationBehavior == TransactionDefinition.Propagation.NEVER) {
return Flux.error(new TransactionUsageException("Found an existing transaction but propagation behaviour doesn't support it: " + propagationBehavior));
}
try {
return handler.doInTransaction(((ReactiveTransactionStatus<Connection>) o));
} catch (Exception e) {
return Flux.error(new TransactionSystemException("Error invoking doInTransaction handler: " + e.getMessage(), e));
}
} else {
if (propagationBehavior == TransactionDefinition.Propagation.MANDATORY) {
return Flux.error(new NoTransactionException("Expected an existing transaction, but none was found in the Reactive context."));
}
return withConnection(connection -> {
if (LOG.isDebugEnabled()) {
LOG.debug("Transaction Begin for DataSource: {}", dataSourceName);
}
DefaultReactiveTransactionStatus status = new DefaultReactiveTransactionStatus(connection, true);
Mono<Boolean> resourceSupplier;
if (definition.getIsolationLevel() != TransactionDefinition.DEFAULT.getIsolationLevel()) {
IsolationLevel isolationLevel = getIsolationLevel(definition);
if (LOG.isDebugEnabled()) {
LOG.debug("Setting Isolation Level ({}) for Transaction on DataSource: {}", isolationLevel, dataSourceName);
}
if (isolationLevel != null) {
resourceSupplier = Flux.from(connection.setTransactionIsolationLevel(isolationLevel)).thenMany(connection.beginTransaction()).hasElements();
} else {
resourceSupplier = Flux.from(connection.beginTransaction()).hasElements();
}
} else {
resourceSupplier = Flux.from(connection.beginTransaction()).hasElements();
}
return Flux.usingWhen(resourceSupplier, (b) -> {
try {
return Flux.from(handler.doInTransaction(status)).contextWrite(context -> context.put(ReactiveTransactionStatus.STATUS, status).put(ReactiveTransactionStatus.ATTRIBUTE, definition));
} catch (Exception e) {
return Mono.error(new TransactionSystemException("Error invoking doInTransaction handler: " + e.getMessage(), e));
}
}, (b) -> doCommit(status), (b, throwable) -> {
if (LOG.isWarnEnabled()) {
LOG.warn("Rolling back transaction on error: " + throwable.getMessage(), throwable);
}
return Flux.from(connection.rollbackTransaction()).hasElements().onErrorResume((rollbackError) -> {
if (rollbackError != throwable && LOG.isWarnEnabled()) {
LOG.warn("Error occurred during transaction rollback: " + rollbackError.getMessage(), rollbackError);
}
return Mono.error(throwable);
}).doFinally((sig) -> status.completed = true);
}, (b) -> doCommit(status));
});
}
});
}
use of io.micronaut.transaction.exceptions.TransactionSystemException in project micronaut-data by micronaut-projects.
the class DefaultR2dbcRepositoryOperations method withTransaction.
@NonNull
@Override
public <T> Publisher<T> withTransaction(@NonNull ReactiveTransactionStatus<Connection> status, @NonNull ReactiveTransactionOperations.TransactionalCallback<Connection, T> handler) {
Objects.requireNonNull(status, "Transaction status cannot be null");
Objects.requireNonNull(handler, "Callback handler cannot be null");
return Flux.defer(() -> {
try {
return handler.doInTransaction(status);
} catch (Exception e) {
return Flux.error(new TransactionSystemException("Error invoking doInTransaction handler: " + e.getMessage(), e));
}
}).contextWrite(context -> context.put(ReactiveTransactionStatus.STATUS, status));
}
use of io.micronaut.transaction.exceptions.TransactionSystemException in project micronaut-data by micronaut-projects.
the class HibernateTransactionManager method doRollback.
@Override
protected void doRollback(DefaultTransactionStatus status) {
HibernateTransactionObject txObject = (HibernateTransactionObject) status.getTransaction();
Transaction hibTx = txObject.getSessionHolder().getTransaction();
Objects.requireNonNull(hibTx, "No Hibernate transaction");
if (status.isDebug()) {
logger.debug("Rolling back Hibernate transaction on Session [" + txObject.getSessionHolder().getSession() + "]");
}
try {
hibTx.rollback();
} catch (org.hibernate.TransactionException ex) {
throw new TransactionSystemException("Could not roll back Hibernate transaction", ex);
} finally {
if (!txObject.isNewSession() && !this.hibernateManagedSession) {
// Clear all pending inserts/updates/deletes in the Session.
// Necessary for pre-bound Sessions, to avoid inconsistent state.
txObject.getSessionHolder().getSession().clear();
}
}
}
Aggregations