[Play-2.5.4] EntityManager/Session is closed

904 views
Skip to first unread message

Víctor Borges Jussiani

unread,
Jul 8, 2016, 11:12:22 AM7/8/16
to play-framework
I`m trying to retrieve the users from database with JPA into a async context, here is my controller:

public class UserController extends Controller {

   
@Inject
    private UserRepository repository;

   
@Inject
    private HttpExecutionContext executionContext;

    @Transactional
    public CompletionStage<Result> getUsers() {
       
return CompletableFuture.supplyAsync(() -> repository.getUsers(), executionContext.current())
               
.thenApplyAsync(Json::toJson)
               
.thenApplyAsync(Results::ok);
    }

}

and my repository looks like:


public class UserRepository {

   
@Inject
   
private JPAApi jpaApi;
   
    public List<User> getUsers(){
       
EntityManager entityManager = jpaApi.em();
        // Logic
        return query.getResultList();
    }

}



and i`m getting the following error:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: java.lang.IllegalStateException: Session/EntityManager is closed]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:280)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:206)
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:98)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)


Someone could help, am i doing something wrong?

Tavi

unread,
Aug 8, 2016, 11:04:14 AM8/8/16
to play-framework
Hello,

I have the exact same problem. Did you find a solution yet?

gustavo....@trt14.jus.br

unread,
Aug 8, 2016, 12:39:22 PM8/8/16
to play-framework
Hi

Try to inject a JPAApi in controller like this

public class AnnyController extends Controller{

     
private JPAApi     JPAapi;
   
   
@Inject
   
public AnnyControoler(JPAApi api) {
        arquivo
= new Arquivo();
       
this.JPAapi = api;
   
}
}



and pass the JPAapi.em() like a param to getUsers() method and remove


   
@Inject
   
private JPAApi jpaApi;

from your UserRepository bean

Tavi

unread,
Aug 9, 2016, 3:18:36 AM8/9/16
to play-framework
Hi,

It's the same.

[CompletionException: org.hibernate.SessionException: Session is closed!]

gustavo....@trt14.jus.br

unread,
Aug 9, 2016, 9:57:38 AM8/9/16
to play-framework

please.. post the code

Ala Schneider

unread,
Aug 11, 2016, 4:13:42 AM8/11/16
to play-framework
I think that you should not use  @Transactional annotation, but jpa.withTransaction instead. All the rest seems ok.
Reply all
Reply to author
Forward
0 new messages