How can I do some db operation on playframework global class method on start.
I would like to put some data to db from other source. For now I have:
My code:
public class Global extends GlobalSettings {
@Override
@Transactional
public void onStart(Application application) {
Akka.system().scheduler().schedule(
Duration.create(1, TimeUnit.SECONDS), // start task delay
Duration.create(24, TimeUnit.HOURS), // between task instance delay
//Duration.create(24, TimeUnit.HOURS), // between task instance delay
new Runnable() {
@Override
@Transactional
public void run() {
System.out.println("Importing CRM data...");
ImportCrmData.start();
System.out.println("... imported");
}
},
Akka.system().dispatcher()
);
I am getting an error:
[info] play - Shutdown application default Akka system.
[info] play - datasource [jdbc:mysql://localhost/svp] bound to JNDI as DefaultDS
[info] play - datasource [jdbc:mysql://192.168.0.4/scrm_customer] bound to JNDI as CRM
[info] play - database [default] connected at jdbc:mysql://localhost/svp
[info] play - database [crm] connected at jdbc:mysql://192.168.0.4/scrm_customer
[info] play - Starting application default Akka system.
[info] play - Application started (Dev)
Importing CRM data...
[ERROR] [12/08/2014 15:57:04.773] [application-akka.actor.default-dispatcher-3] [TaskInvocation] No EntityManager bound to this thread. Try wrapping this call in JPA.withTransaction, or ensure that the HTTP context is setup on this thread.
java.lang.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.
at play.db.jpa.JPA.em(JPA.java:55)
at models.Customer.getByCrmId(Customer.java:185)
at models.CustomerCRM.importCrmData(CustomerCRM.java:84)
at actions.ImportCrmData.start(ImportCrmData.java:28)
at Global$1.run(Global.java:40)
at akka.actor.LightArrayRevolverScheduler$$anon$3$$anon$2.run(Scheduler.scala:241)
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:42)
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:386)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Please give me some help.
I use playframework 2.2.4 with java
-------------------------------------EDIT-----------------------------------------
Afted adding:
JPA.withTransaction(() -> {
ImportCrmData.start();
});
I am getting error.
[info] Compiling 1 Java source to app/modules/common/target/scala-2.10/classes...
[error] app/modules/common/app/Global.java:38: error: illegal start of expression
[error] JPA.withTransaction(() -> {
[error] ^
[error] app/modules/common/app/Global.java:38: error: illegal start of expression
[error] JPA.withTransaction(() -> {
[error] ^
[error] app/modules/common/app/Global.java:38: error: illegal start of expression
[error] JPA.withTransaction(() -> {
[error] ^
[error] app/modules/common/app/Global.java:38: error: ';' expected
[error] JPA.withTransaction(() -> {
[error] ^
[error] app/modules/common/app/Global.java:39: error: illegal start of expression
[error] ImportCrmData.start();
[error] ^
[error] app/modules/common/app/Global.java:39: error: ';' expected
[error] ImportCrmData.start();
[error] ^
[error] app/modules/common/app/Global.java:40: error: illegal start of type
[error] });
[error] ^
[error] app/modules/common/app/Global.java:41: error: ')' expected
[error] }
[error] ^
[error] app/modules/common/app/Global.java:42: error: illegal start of expression
[error] },
[error] ^
[error] app/modules/common/app/Global.java:43: error: ';' expected
[error] Akka.system().dispatcher()
[error] ^
[error] app/modules/common/app/Global.java:43: error: ';' expected
[error] Akka.system().dispatcher()
[error] ^
[error] 11 errors
[error] (common/compile:compile) javac returned nonzero exit code
Finally I've added:
public void run() {
JPA.withTransaction(
ImportCrmData.start()
);
}
Remove
@Transactional
annotations, they are useful only in controllers. Wrap CRM import withJPA.withTransaction
method. Java 8 syntax:And non Java 8 syntax: