How to handle models and the wait () function in Play 1.2.x?

My problem is the following (pseudo) code:

Model aModel = Model.findById(1); await(something); aModel.causeALazyLoad(); 

Raises an exception in the last line, "session or session closed." This is because, as far as I can tell, the await () call pauses the request and closes the hibernation session.

What is the right way or best practice to handle this? Most of the time I want to start work, load some objects, and then wait for the result of the task. Now these entities are all from the old session, so it is difficult to use them properly.

Thanks!

+4
source share
2 answers

there may be another thread that does "something" (for example, Callable) in the statement:

 await(something); 

If so, you can try to propagate the current JPA instance to this stream, for example:

 final JPA jpa = JPA.local.get(); await(new Job() { public void doJob() throws Exception { JPA.local.set(jpa); //do something }; }.now()); 
0
source

Maybe too late to help :), but the solution I found was to combine the initially loaded model into a new Hibernate session after waiting ()

 Model aModel = Model.findById(1); await(something); aModel = aModel.merge(); aModel.causeALazyLoad(); 
0
source

Source: https://habr.com/ru/post/1402963/


All Articles