I am working on the code which is calling, transaction after transaction.
@Asynchronous
@TransactionTimeout(value = 12, unit = TimeUnit.HOURS)
public void runExport(Long id) {
/*
* some stuff that takes time
*/
em.persist(objectTest);
manager.runAnotherTransaction(id);
}
@Asynchronous
@TransactionTimeout(value = 1, unit = TimeUnit.HOURS)
public void runCopy(Long id) {
if(manager.isPreviousValueCommited(id)){
//do some stuff
}
/*
* some stuff that takes time
*/
}
//other class
public void runAnotherTransaction(long id){
//...
superBean.runCopy(id);
}
public void isPreviousValueCommited(long id){
//Thread.sleep(1000) if we sleep here, object wont be null
SomeObject obj = em.find(SomeObject.class, id); //is null,
}
As you can see in code, first transaction method is persisting object, and after that we are calling other method via some kind of manager
, and second transaction want to check some stuff on the previously commited object. But as you probably suspect it cant, because object does not exist in database (first transaction was not able to save it before we ask db). When I give some sleep before finding this object everything is ok.
This sample code is pretty simple, and it is probably make sense to not pass id, but object itself(true?) to avoid this problem, but I have question. But I would like to listen another ideas or pros/cons(possibly there is much more cons) of this approach of connecting Transaction as I have in example.