play, akka + jpa

396 views
Skip to first unread message

Dominik Dorn

unread,
Apr 25, 2011, 12:51:15 PM4/25/11
to play-fr...@googlegroups.com
Hi,

I started with Akka for java a few days ago and successfully created
the first flow of messages.

right now, I need to find out how to access plays JPA from a new
thread, as I an exception like this
when trying to access JPA in an actor:

play.exceptions.JPAException: The JPA context is not initialized. JPA
Entity Manager automatically start when one or more classes annotated
with the @javax.persistence.Entity annotation are found in the
application.
at play.db.jpa.JPA.get(JPA.java:22)
at play.db.jpa.JPA.em(JPA.java:51)
at play.db.jpa.JPQL.em(JPQL.java:16)
at play.db.jpa.JPQL.count(JPQL.java:20)
at models.Balance.count(Balance.java)
at actors.GenericImportDispatcher.onReceive(GenericImportDispatcher.java:80)
at akka.actor.UntypedActor$$anonfun$receive$1.apply(UntypedActor.scala:68)
at akka.actor.UntypedActor$$anonfun$receive$1.apply(UntypedActor.scala:67)
at akka.actor.Actor$class.apply(Actor.scala:449)
at akka.actor.UntypedActor.apply(UntypedActor.scala:63)
at akka.actor.LocalActorRef.invoke(ActorRef.scala:836)
at akka.dispatch.MessageInvocation.invoke(MessageHandling.scala:26)
at akka.dispatch.ExecutableMailbox$class.processMailbox(ExecutorBasedEventDrivenDispatcher.scala:215)
at akka.dispatch.ExecutorBasedEventDrivenDispatcher$$anon$5.processMailbox(ExecutorBasedEventDrivenDispatcher.scala:125)
at akka.dispatch.ExecutableMailbox$class.run(ExecutorBasedEventDrivenDispatcher.scala:188)
at akka.dispatch.ExecutorBasedEventDrivenDispatcher$$anon$5.run(ExecutorBasedEventDrivenDispatcher.scala:125)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
at akka.dispatch.MonitorableThread.run(ThreadPoolBuilder.scala:182)


Any help appreciated.


thanks,
dominik

--
Dominik Dorn
http://dominikdorn.com
http://twitter.com/domdorn

Skripten, Mitschriften, Lernunterlagen, etc. findest Du auf Study
Guru: http://www.studyguru.eu !

Pascal Voitot Dev

unread,
Apr 25, 2011, 1:03:11 PM4/25/11
to play-fr...@googlegroups.com
suggestion: is the JPA plugin already started when akka begins its actors? (I don't know the precedence of both plugins)

Pascal


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.


Dominik Dorn

unread,
Apr 25, 2011, 1:47:33 PM4/25/11
to play-fr...@googlegroups.com
I'm not using the official play akka plugin (its outdated and does not
have any support for JPA either).


The JPA Plugin is already started, but it only attaches to a
web-request or a Job, but not a thread created
by Akka.

my current workaround is to manually do the job of the JPA Plugin in
my actor....
I'm doing it like this right now (which will give me some performance penalties
and it's quite a hack imho ):

public class SomeActor extends UntypedActor {

public void onReceive(Object message){
// checks for other messages
// ...
else if(message instanceof ProfessorStub)
{


try{ // wrapping everything around a try catch
if(JPA.local.get() == null)
{
JPA.local.set(new JPA());
JPA.local.get().entityManager = JPA.newEntityManager();
}

Balance.count(); // sample code that uses JPA
}
finally {
JPA.local.get().entityManager.close();
JPA.local.remove();
}

return;
}

}

}

http://www.studyguru.eu !

Morten Kjetland

unread,
Apr 25, 2011, 2:18:11 PM4/25/11
to play-fr...@googlegroups.com
Have you tried with Play master (at github)?

It has a new JPA impl which initialized JPA lazy - I'm not sure but after looking some at the code this might work:

if you manually initialized Invoker.InvocationContext.current(), then it might work to do JPA-stuff..
But you have to commit the transaction your self..

Let me know how it works for you.

-Morten

Matthew Knights

unread,
Jan 30, 2013, 8:18:55 AM1/30/13
to play-fr...@googlegroups.com
I have a skeleton project of Play 1.2.x and AKK with JPA on github https://github.com/matthewk/Play-nicewith-AKKA. I use: 
JPAPlugin.startTX(true) //true for read-only transaction
...code

JPAPlugin.closeTX(false)//false for don't roll backtransaction

The only problem I have run into is when saving new entities and passing the ID of that entity to the actor, I must call refresh() after save() to ensure that hibernate has the latest version from the db. 

hope this helps.



On Monday, 5 November 2012 15:13:57 UTC, Laura wrote:
have you found a solution for this?
Reply all
Reply to author
Forward
0 new messages