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