use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.
the class PersistedSequencingMixin method newSequenceValue.
@Override
public Long newSequenceValue() throws SequencingException {
synchronized (this) {
ConcurrentEntityModificationException exc = null;
UnitOfWork uow = uowf.newUnitOfWork();
try {
for (int i = 0; i < 3; i++) {
try {
Property<Long> property = sequence.get().currentValue();
long value = property.get();
value = value + 1;
property.set(value);
uow.complete();
return value;
} catch (ConcurrentEntityModificationException e) {
// Ignore;
exc = e;
}
}
throw new SequencingException("Unable to update sequence value.", exc);
} catch (UnitOfWorkCompletionException e) {
throw new SequencingException("Unable to update sequence value.", exc);
}
}
}
use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.
the class HasUoWFileTest method testRetry.
@Test
public void testRetry() throws IOException, UnitOfWorkCompletionException, InterruptedException {
LOGGER.info("# Test Retry #################################################################################");
final String entityId;
File attachedFile;
// Create new
try (UnitOfWork uow = module.newUnitOfWork()) {
TestedEntity entity = createTestedEntity(uow, "Testing Concurrent Modification");
entityId = entity.identity().get();
attachedFile = entity.attachedFile();
uow.complete();
}
final List<Exception> ex = new ArrayList<>();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
testService.modifyFileWithRetry(entityId, 0, 3000);
} catch (Exception ex1) {
ex.add(ex1);
}
}
}, "job1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
testService.modifyFileWithRetry(entityId, 1000, 0);
} catch (Exception ex1) {
ex.add(ex1);
}
}
}, "job2");
t1.start();
t2.start();
t1.join();
t2.join();
for (Exception eachEx : ex) {
eachEx.printStackTrace();
}
assertTrue("There were errors during TestRetry", ex.isEmpty());
assertTrue("Modified file content was not the good one", isFileFirstLineEqualsTo(attachedFile, "Modification"));
}
use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.
the class HasUoWFilesTest method testRetry.
@Test
public void testRetry() throws IOException, UnitOfWorkCompletionException, InterruptedException {
LOGGER.info("# Test Retry #################################################################################");
final String entityId;
File attachedFile;
// Create new
try (UnitOfWork uow = module.newUnitOfWork()) {
TestedEntity entity = createTestedOneEntityTwoFilesEntity(uow, "Testing Concurrent Modification");
entityId = entity.identity().get();
attachedFile = entity.attachedFile(MyEnum.fileOne);
uow.complete();
}
final List<Exception> ex = new ArrayList<>();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
testService.modifyFileWithRetry(entityId, 0, 10000);
} catch (Exception ex1) {
ex.add(ex1);
}
}
}, "job1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
testService.modifyFileWithRetry(entityId, 5000, 0);
} catch (Exception ex1) {
ex.add(ex1);
}
}
}, "job2");
t1.start();
t2.start();
t1.join();
t2.join();
for (Exception eachEx : ex) {
eachEx.printStackTrace();
}
assertTrue("There were errors during TestRetry", ex.isEmpty());
assertTrue("Modified file content was not the good one", isFileFirstLineEqualsTo(attachedFile, "Modification"));
}
use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.
the class WicketQi4jApplication method handleUnitOfWork.
private void handleUnitOfWork() {
getRequestCycleListeners().add(new AbstractRequestCycleListener() {
@Override
public void onBeginRequest(final RequestCycle requestCycle) {
super.onBeginRequest(requestCycle);
logger.debug("================================");
logger.debug("REQUEST start");
logger.debug(requestCycle.getRequest().toString());
logger.debug(requestCycle.getRequest().getRequestParameters().toString());
UnitOfWork uow = module.newUnitOfWork(UsecaseBuilder.newUsecase("REQUEST"));
logger.debug(" ### NEW " + uow + " ### MODULE: " + qi4jModule);
}
@Override
public void onEndRequest(final RequestCycle requestCycle) {
UnitOfWork uow = module.currentUnitOfWork();
if (uow != null) {
try {
if ("POST".equals(((HttpServletRequest) requestCycle.getRequest().getContainerRequest()).getMethod())) {
// "Save"
logger.debug(" ### COMPLETE " + uow + " ### MODULE: " + qi4jModule);
uow.complete();
} else {
// GET requests
logger.debug(" ### DISCARD " + uow + " ### MODULE: " + qi4jModule);
uow.discard();
}
} catch (UnitOfWorkCompletionException e) {
logger.error(" ### DISCARD " + uow + " ### MODULE: " + qi4jModule);
uow.discard();
e.printStackTrace();
}
}
logger.debug("REQUEST end");
logger.debug("------------------------------------");
}
});
}
use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.
the class EnvisageSample method createTestData.
public void createTestData() {
UnitOfWork uow = module.newUnitOfWork();
try {
createCar("Volvo", "S80", 2007);
createCar("Volvo", "C70", 2006);
createCar("Ford", "Transit", 2007);
createCar("Ford", "Mustang", 2007);
createCar("Ford", "Mustang", 2006);
createCar("Ford", "Mustang", 2005);
createAnimal("Cat", "Miaow");
createAnimal("Duck", "Kwek Kwek");
createAnimal("Dog", "Guk Guk");
createAnimal("Cow", "Moooo");
uow.complete();
} catch (UnitOfWorkCompletionException e) {
// Can not happen.
e.printStackTrace();
}
}
Aggregations