Search in sources :

Example 1 with TransactionException

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;
}
Also used : TransactionException(io.requery.TransactionException) SQLException(java.sql.SQLException)

Example 2 with TransactionException

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();
    }
}
Also used : TransactionException(io.requery.TransactionException) SQLException(java.sql.SQLException)

Example 3 with TransactionException

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);
    }
}
Also used : TransactionException(io.requery.TransactionException) Transaction(io.requery.Transaction) RollbackException(io.requery.RollbackException) ReadOnlyException(io.requery.ReadOnlyException) RollbackException(io.requery.RollbackException) SQLException(java.sql.SQLException) TransactionException(io.requery.TransactionException) PersistenceException(io.requery.PersistenceException)

Example 4 with TransactionException

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();
    }
}
Also used : HeuristicRollbackException(javax.transaction.HeuristicRollbackException) TransactionException(io.requery.TransactionException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException)

Example 5 with TransactionException

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();
        }
    }
}
Also used : TransactionException(io.requery.TransactionException) SystemException(javax.transaction.SystemException)

Aggregations

TransactionException (io.requery.TransactionException)7 SQLException (java.sql.SQLException)5 SystemException (javax.transaction.SystemException)3 PersistenceException (io.requery.PersistenceException)1 ReadOnlyException (io.requery.ReadOnlyException)1 RollbackException (io.requery.RollbackException)1 Transaction (io.requery.Transaction)1 HeuristicMixedException (javax.transaction.HeuristicMixedException)1 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)1 NotSupportedException (javax.transaction.NotSupportedException)1 RollbackException (javax.transaction.RollbackException)1