Search in sources :

Example 1 with ConcurrentEntityModificationException

use of org.qi4j.api.unitofwork.ConcurrentEntityModificationException in project qi4j-sdk by Qi4j.

the class AbstractEntityStoreTest method givenConcurrentUnitOfWorksWhenUoWCompletesThenCheckConcurrentModification.

@Test
public void givenConcurrentUnitOfWorksWhenUoWCompletesThenCheckConcurrentModification() throws UnitOfWorkCompletionException {
    TestEntity testEntity;
    {
        UnitOfWork unitOfWork = module.newUnitOfWork();
        EntityBuilder<TestEntity> builder = unitOfWork.newEntityBuilder(TestEntity.class);
        testEntity = builder.newInstance();
        unitOfWork.complete();
    }
    UnitOfWork unitOfWork1;
    TestEntity testEntity1;
    String version;
    {
        // Start working with Entity in one UoW
        unitOfWork1 = module.newUnitOfWork();
        testEntity1 = unitOfWork1.get(testEntity);
        version = spi.entityStateOf(testEntity1).version();
        if (version.equals("")) {
            unitOfWork1.discard();
            // Store doesn't track versions - no point in testing it
            return;
        }
        testEntity1.name().set("A");
        testEntity1.unsetName().set("A");
    }
    {
        // Start working with same Entity in another UoW, and complete it
        UnitOfWork unitOfWork = module.newUnitOfWork();
        TestEntity testEntity2 = unitOfWork.get(testEntity);
        assertThat("version is correct", spi.entityStateOf(testEntity1).version(), equalTo(version));
        testEntity2.name().set("B");
        unitOfWork.complete();
    }
    {
        // Try to complete first UnitOfWork
        try {
            unitOfWork1.complete();
            fail("Should have thrown concurrent modification exception");
        } catch (ConcurrentEntityModificationException e) {
            unitOfWork1.discard();
        }
    }
    {
        // Check values
        unitOfWork1 = module.newUnitOfWork();
        testEntity1 = unitOfWork1.get(testEntity);
        assertThat("property name has not been set", testEntity1.name().get(), equalTo("B"));
        assertThat("version is incorrect", spi.entityStateOf(testEntity1).version(), not(equalTo(version)));
        unitOfWork1.discard();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) EntityBuilder(org.qi4j.api.entity.EntityBuilder) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 2 with ConcurrentEntityModificationException

use of org.qi4j.api.unitofwork.ConcurrentEntityModificationException in project qi4j-sdk by Qi4j.

the class PieroTest method testPersistence.

@Test
public void testPersistence() {
    UnitOfWork uow = module.newUnitOfWork();
    try {
        String carId1 = createCar("Volvo", "S80", 2007);
        String carId2 = createCar("Volvo", "C70", 2006);
        String carId3 = createCar("Ford", "Transit", 2007);
        String carId4 = createCar("Ford", "Mustang", 2007);
        String carId5 = createCar("Ford", "Mustang", 2006);
        String carId6 = createCar("Ford", "Mustang", 2005);
        uow.complete();
        uow = module.newUnitOfWork();
        System.out.println(uow.get(Car.class, carId1));
        System.out.println(uow.get(Car.class, carId2));
        System.out.println(uow.get(Car.class, carId3));
        System.out.println(uow.get(Car.class, carId4));
        System.out.println(uow.get(Car.class, carId5));
        System.out.println(uow.get(Car.class, carId6));
        uow.discard();
    } catch (ConcurrentEntityModificationException e) {
        // Can not happen.
        e.printStackTrace();
    } catch (UnitOfWorkCompletionException e) {
        e.printStackTrace();
    }
    uow = module.newUnitOfWork();
    QueryBuilder<Car> qb = module.newQueryBuilder(Car.class);
    Car template = QueryExpressions.templateFor(Car.class);
    qb.where(QueryExpressions.eq(template.year(), 2007));
    Query<Car> query = uow.newQuery(qb);
    query.orderBy(orderBy(template.manufacturer()), orderBy(template.model()));
    System.out.println("Cars from 2007");
    for (Car car : query) {
        System.out.println(car.manufacturer().get() + " " + car.model().get() + ", " + car.year().get());
    }
    uow.discard();
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 3 with ConcurrentEntityModificationException

use of org.qi4j.api.unitofwork.ConcurrentEntityModificationException in project qi4j-sdk by Qi4j.

the class TraceServiceMixin method traceException.

@Override
public void traceException(Class compositeType, Composite object, Method method, Object[] args, Throwable t, long entryTime, long durationNano) {
    UnitOfWork uow = uowf.newUnitOfWork();
    try {
        createTraceRecord(uow, compositeType, object, method, args, entryTime, durationNano, t);
        uow.complete();
    } catch (ConcurrentEntityModificationException e) {
    // ignore for now. Perhaps discard() and try again.
    } catch (UnitOfWorkCompletionException e) {
    // ignore for now. Perhaps discard() and try again.
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException)

Example 4 with ConcurrentEntityModificationException

use of org.qi4j.api.unitofwork.ConcurrentEntityModificationException in project qi4j-sdk by Qi4j.

the class TraceServiceMixin method traceSuccess.

@Override
public void traceSuccess(Class compositeType, Composite object, Method method, Object[] args, Object result, long entryTime, long durationNano) {
    UnitOfWork uow = uowf.newUnitOfWork();
    try {
        createTraceRecord(uow, compositeType, object, method, args, entryTime, durationNano, null);
        uow.complete();
    } catch (ConcurrentEntityModificationException e) {
    // ignore for now. Perhaps discard() and try again.
    } catch (UnitOfWorkCompletionException e) {
    // ignore for now. Perhaps discard() and try again.
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException)

Example 5 with ConcurrentEntityModificationException

use of org.qi4j.api.unitofwork.ConcurrentEntityModificationException in project qi4j-sdk by Qi4j.

the class DebuggingTest method whenCallingMethodThenExpectDebugEntityCreated.

@Test
public void whenCallingMethodThenExpectDebugEntityCreated() {
    UnitOfWork uow = module.newUnitOfWork();
    try {
        // There is no Query capability available for Libraries, since that sits in Extensions.
        // Obtaining the EntityStore directly is a very ugly hack to get around this problem, and only related
        // to the test sitting in qi4j-libraries source repository.
        // QueryBuilder<DebugRecord> builder = module.newQueryBuilder( DebugRecord.class );
        // Query<DebugRecord> query = builder.newQuery( uow );
        // assertEquals( 0, query.count() );
        Some service = (Some) module.findService(Some.class).get();
        String message = service.doSomething("World!", 10);
        assertEquals(message, "Hello!");
        EntityStore es = (EntityStore) module.findService(EntityStore.class).get();
        final String[] result = new String[1];
        es.entityStates(module).transferTo(Transforms.map(new Function<EntityState, EntityState>() {

            public EntityState map(EntityState entityState) {
                if (ServiceDebugRecordEntity.class.getName().equals(first(entityState.entityDescriptor().types()).getName())) {
                    result[0] = entityState.identity().identity();
                }
                return entityState;
            }
        }, Outputs.<EntityState>noop()));
        ServiceDebugRecordEntity debugEntry = uow.get(ServiceDebugRecordEntity.class, result[0]);
        String mess = debugEntry.message().get();
        System.out.println(mess);
        assertEquals("some message.", mess);
        uow.complete();
    } catch (ConcurrentEntityModificationException e) {
        e.printStackTrace();
        uow.discard();
    } catch (UnitOfWorkCompletionException e) {
        e.printStackTrace();
        uow.discard();
    } finally {
        if (uow.isOpen()) {
            uow.discard();
        }
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Function(org.qi4j.functional.Function) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) EntityStore(org.qi4j.spi.entitystore.EntityStore) EntityState(org.qi4j.spi.entity.EntityState) ServiceDebugRecordEntity(org.qi4j.logging.debug.records.ServiceDebugRecordEntity) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Aggregations

ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)16 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)15 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)14 ArrayList (java.util.ArrayList)5 Serializable (java.io.Serializable)4 Test (org.junit.Test)4 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 AbstractRequestCycleListener (org.apache.wicket.request.cycle.AbstractRequestCycleListener)2 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)2 Collection (java.util.Collection)1 EntityBuilder (org.qi4j.api.entity.EntityBuilder)1 EntityComposite (org.qi4j.api.entity.EntityComposite)1 EntityReference (org.qi4j.api.entity.EntityReference)1 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)1 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)1 UnitOfWorkException (org.qi4j.api.unitofwork.UnitOfWorkException)1 Usecase (org.qi4j.api.usecase.Usecase)1 Function (org.qi4j.functional.Function)1 ServiceDebugRecordEntity (org.qi4j.logging.debug.records.ServiceDebugRecordEntity)1