[2.2.1-java] JPA.withTransaction removes entity manager from current thread

1,335 views
Skip to first unread message

Dmitry Zadonsky

unread,
Nov 14, 2013, 7:53:21 AM11/14/13
to play-fr...@googlegroups.com
I need to calculate some jobs with database data in parallel and  merge results with database too.
Example with only one job (in my project i have more of course):
@Transactional(readOnly=true)
    public Result main() {
        F.Promise<String> helloPromise = Akka.future(new Callable<String>() {
            @Override
            public String call() throws Exception {
                try {
                    return JPA.withTransaction(new F.Function0<String>() {
                        @Override
                        public String apply() throws Throwable {
                            return "hello";//work with database
                        }
                    });
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
                return "error";
            }
        });
        String hello = helloPromise.get();
        JPA.em(); // try to get em for working with database
         //......
        return ok();
    }

This code work fine with 2.1.0, but throws RuntimeException: No EntityManager bound to this thread. at  JPA.em(); with 2.2.1.
Repleace future with promise not helps:
@Transactional(readOnly=true)
    public Result main() {
        F.Promise<String> helloPromise = promise(new F.Function0<String>() {
            @Override
            public String apply() throws Throwable {
                return JPA.withTransaction(new F.Function0<String>() {
                    @Override
                    public String apply() throws Throwable {
                        return "hello"; 
                    }
                });
            }
        });
        String hello = helloPromise.get(1000);
        JPA.em();
        return ok();
    }

Its a bug or feature 2.2.1 version ?

Dmitry Zadonsky

unread,
Nov 14, 2013, 3:09:20 PM11/14/13
to play-fr...@googlegroups.com
So.. This is bug or not ? Should i create an issue ?

Dmitry Zadonsky

unread,
Nov 15, 2013, 3:15:31 AM11/15/13
to play-fr...@googlegroups.com

Dmitry Zadonsky

unread,
Nov 18, 2013, 8:54:04 AM11/18/13
to play-fr...@googlegroups.com
It seems that is not a bug. But can someone tell me what wrong with this commit https://github.com/zadonskiyd/play2jpaProblemExample/commit/dafb7898adcb7559475800065b8a664d3184deff
Why this code not working ? With 2.1.0 same code (based on future instead promises) works fine.
Documentation (http://www.playframework.com/documentation/2.2.x/JavaAsync)  says that "A simple way to execute a block of code asynchronously and to get a Promise is to use the promise() helper".
My code tries to execute a block asynchronously with DB access. Whats wrong ?...

Joao Antunes

unread,
Oct 3, 2015, 11:38:44 AM10/3/15
to play-framework
Dmitry, I know that this is an old issue, but while searching for another problem, i found both your problem and its solution.

In case you missed out on it, check this custom JPA helper implementation:

Reply all
Reply to author
Forward
0 new messages