use of dev.morphia.experimental.MorphiaSession in project morphia by mongodb.
the class TestTransactions method manual.
@Test
public void manual() {
boolean success = false;
int count = 0;
while (!success && count < 5) {
try (MorphiaSession session = getDs().startSession()) {
session.startTransaction();
Rectangle rectangle = new Rectangle(1, 1);
session.save(rectangle);
session.save(new User("transactions", LocalDate.now()));
assertNull(getDs().find(Rectangle.class).first());
assertNull(getDs().find(User.class).first());
assertNotNull(session.find(Rectangle.class).first());
assertNotNull(session.find(User.class).first());
session.commitTransaction();
success = true;
} catch (MongoQueryException e) {
if (e.getErrorCode() == 251 && e.getErrorMessage().contains("has been aborted")) {
count++;
} else {
throw e;
}
} catch (RuntimeException e) {
throw e;
}
}
assertNotNull(getDs().find(Rectangle.class).first());
assertNotNull(getDs().find(User.class).first());
}
Aggregations