use of io.requery.TransactionException in project requery by requery.
the class ConnectionTransaction method begin.
@Override
public Transaction begin(TransactionIsolation isolation) {
if (active()) {
throw new IllegalStateException("transaction already active");
}
try {
transactionListener.beforeBegin(isolation);
connection = connectionProvider.getConnection();
uncloseableConnection = new UncloseableConnection(connection);
if (supportsTransaction) {
connection.setAutoCommit(false);
if (isolation != null) {
previousIsolationLevel = connection.getTransactionIsolation();
int level;
switch(isolation) {
case NONE:
level = Connection.TRANSACTION_NONE;
break;
case READ_UNCOMMITTED:
level = Connection.TRANSACTION_READ_UNCOMMITTED;
break;
case READ_COMMITTED:
level = Connection.TRANSACTION_READ_COMMITTED;
break;
case REPEATABLE_READ:
level = Connection.TRANSACTION_REPEATABLE_READ;
break;
case SERIALIZABLE:
level = Connection.TRANSACTION_SERIALIZABLE;
break;
default:
throw new UnsupportedOperationException();
}
connection.setTransactionIsolation(level);
}
}
committed = false;
rolledBack = false;
entities.clear();
transactionListener.afterBegin(isolation);
} catch (SQLException e) {
throw new TransactionException(e);
}
return this;
}
use of io.requery.TransactionException in project requery by requery.
the class ConnectionTransaction method rollback.
@Override
public void rollback() {
try {
transactionListener.beforeRollback(entities.types());
if (supportsTransaction) {
connection.rollback();
rolledBack = true;
entities.clearAndInvalidate();
}
transactionListener.afterRollback(entities.types());
entities.clear();
} catch (SQLException e) {
throw new TransactionException(e);
} finally {
resetConnection();
}
}
use of io.requery.TransactionException in project requery by requery.
the class EntityDataStore method runInTransaction.
@Override
public <V> V runInTransaction(Callable<V> callable, @Nullable TransactionIsolation isolation) {
Objects.requireNotNull(callable);
checkClosed();
Transaction transaction = transactionProvider.get();
if (transaction == null) {
throw new TransactionException("no transaction");
}
try {
transaction.begin(isolation);
V result = callable.call();
transaction.commit();
return result;
} catch (Exception e) {
transaction.rollback();
throw new RollbackException(e);
}
}
use of io.requery.TransactionException in project requery by requery.
the class ManagedTransaction method commit.
@Override
public void commit() {
if (initiatedTransaction) {
try {
transactionListener.beforeCommit(entities.types());
getUserTransaction().commit();
transactionListener.afterCommit(entities.types());
} catch (RollbackException | SystemException | HeuristicMixedException | HeuristicRollbackException e) {
throw new TransactionException(e);
}
}
try {
entities.clear();
} finally {
close();
}
}
use of io.requery.TransactionException in project requery by requery.
the class ManagedTransaction method rollback.
@Override
public void rollback() {
if (!rolledBack) {
try {
if (!completed) {
transactionListener.beforeRollback(entities.types());
if (initiatedTransaction) {
try {
getUserTransaction().rollback();
} catch (SystemException e) {
throw new TransactionException(e);
}
} else if (active()) {
getSynchronizationRegistry().setRollbackOnly();
}
transactionListener.afterRollback(entities.types());
}
} finally {
rolledBack = true;
entities.clearAndInvalidate();
}
}
}
Aggregations