Search in sources :

Example 1 with MorphiaSession

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());
}
Also used : User(dev.morphia.test.models.User) MorphiaSession(dev.morphia.experimental.MorphiaSession) Rectangle(dev.morphia.test.models.Rectangle) MongoQueryException(com.mongodb.MongoQueryException) Test(org.testng.annotations.Test)

Aggregations

MongoQueryException (com.mongodb.MongoQueryException)1 MorphiaSession (dev.morphia.experimental.MorphiaSession)1 Rectangle (dev.morphia.test.models.Rectangle)1 User (dev.morphia.test.models.User)1 Test (org.testng.annotations.Test)1