Daulet Kadirbekov
Daulet Kadirbekov

Reputation: 19

Play Framework 2.2.* SecureSocial Service No EntityManager bound to this thread

I am using SecureSocial in my Play Framework project. I want to have JPA in my service(creating, finding users and tokens). And I have error:

[RuntimeException: No EntityManager bound to this thread. Try wrapping this call in JPA.withTransaction, or ensure that the HTTP context is setup on this thread.]

My code:

public Identity doFindByEmailAndProvider(String email, String providerId) {
    List<User> userList = User.findByEmailAndProvider(email, providerId);
    if(userList.size() != 1){
        logger.debug("found a null in doFindByEmailAndProvider");
        return null;
    }....

If I do this with JPA.withTransaction

public Identity doFindByEmailAndProvider(String email, String providerId) {
    List<User> userList;
    JPA.withTransaction(new F.Callback0() {
        @Override
        public void invoke() throws Throwable {
            userList = User.findByEmailAndProvider(email, providerId);
        }
    });

it tells that

Variable 'userList' is accessed from within inner class. Needs to be declared final.

Upvotes: 1

Views: 1351

Answers (1)

mguillermin
mguillermin

Reputation: 4181

You have to execute your code inside a JPA.withTransaction() if you're not in the context of a controller with the @Transactional annotation.

To make your last snippet work, you just have to declare the userList variable final. :

public Identity doFindByEmailAndProvider(String email, String providerId) {
    final List<User> userList;
    JPA.withTransaction(new F.Callback0() {
        @Override
        public void invoke() throws Throwable {
            userList = User.findByEmailAndProvider(email, providerId);
        }
    });
    ...

Upvotes: 1

Related Questions