Search in sources :

Example 1 with Transaction

use of io.requery.Transaction 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 2 with Transaction

use of io.requery.Transaction in project requery by requery.

the class ThreadLocalTransaction method rollback.

@Override
public void rollback() {
    Transaction transaction = threadLocal.get();
    if (transaction == null) {
        throw new IllegalStateException();
    }
    transaction.rollback();
}
Also used : Transaction(io.requery.Transaction)

Example 3 with Transaction

use of io.requery.Transaction in project requery by requery.

the class FunctionalTest method testTransactionRollback.

@Test
public void testTransactionRollback() {
    ArrayList<Integer> ids = new ArrayList<>();
    try (Transaction transaction = data.transaction().begin()) {
        for (int i = 0; i < 10; i++) {
            Person person = randomPerson();
            data.insert(person);
            assertTrue(person.getId() > 0);
            ids.add(person.getId());
            if (i == 5) {
                throw new Exception("rollback...");
            }
        }
        transaction.commit();
    } catch (Exception ignored) {
    }
    for (Integer id : ids) {
        Person p = data.select(Person.class).where(Person.ID.equal(id)).get().firstOrNull();
        assertNull(p);
    }
}
Also used : Transaction(io.requery.Transaction) ArrayList(java.util.ArrayList) Person(io.requery.test.model.Person) Group_Person(io.requery.test.model.Group_Person) RollbackException(io.requery.RollbackException) SQLException(java.sql.SQLException) PersistenceException(io.requery.PersistenceException) Test(org.junit.Test)

Example 4 with Transaction

use of io.requery.Transaction in project requery by requery.

the class FunctionalTest method testInsertManyToMany.

@Test
public void testInsertManyToMany() {
    Person person = randomPerson();
    data.insert(person);
    assertTrue(person.getGroups().toList().isEmpty());
    List<Group> added = new ArrayList<>();
    try (Transaction transaction = data.transaction().begin()) {
        for (int i = 0; i < 10; i++) {
            Group group = new Group();
            group.setName("Group" + i);
            group.setDescription("Some description");
            group.setType(GroupType.PRIVATE);
            data.insert(group);
            person.getGroups().add(group);
            added.add(group);
        }
        data.update(person);
        transaction.commit();
    }
    data.refresh(person, Person.GROUPS);
    assertTrue(added.containsAll(person.getGroups().toList()));
    for (Group group : added) {
        assertTrue(group.getMembers().toList().contains(person));
    }
}
Also used : Group(io.requery.test.model.Group) Transaction(io.requery.Transaction) ArrayList(java.util.ArrayList) Person(io.requery.test.model.Person) Group_Person(io.requery.test.model.Group_Person) Test(org.junit.Test)

Example 5 with Transaction

use of io.requery.Transaction in project requery by requery.

the class ThreadLocalTransaction method commit.

@Override
public void commit() {
    Transaction transaction = threadLocal.get();
    if (transaction == null) {
        throw new IllegalStateException();
    }
    transaction.commit();
}
Also used : Transaction(io.requery.Transaction)

Aggregations

Transaction (io.requery.Transaction)5 PersistenceException (io.requery.PersistenceException)2 RollbackException (io.requery.RollbackException)2 Group_Person (io.requery.test.model.Group_Person)2 Person (io.requery.test.model.Person)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ReadOnlyException (io.requery.ReadOnlyException)1 TransactionException (io.requery.TransactionException)1 Group (io.requery.test.model.Group)1