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);
}
}
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();
}
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);
}
}
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));
}
}
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();
}
Aggregations