Выполнить запрос JPA для GlobalSetting.onStart() в PLayframework

Как я могу сделать некоторые операции с БД на методе глобального класса playframework при запуске.

Я хотел бы поместить некоторые данные в БД из другого источника. На данный момент у меня есть:

Мой код:

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()
                    );

Я получаю ошибку:

[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)

Пожалуйста, помогите мне.

Я использую Playframework 2.2.4 с Java

-------------------------------------РЕДАКТИРОВАТЬ------------ -----------------------------

После добавления:

JPA.withTransaction(() -> {
    ImportCrmData.start();
});

Я получаю ошибку.

[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

Наконец я добавил:

                        public void run() {
                            JPA.withTransaction(
                                    ImportCrmData.start()
                                    );
                        }

1 ответ

Решение

Удалить @Transactional аннотации, они полезны только в контроллерах. Wrap CRM импорт с JPA.withTransaction метод. Синтаксис Java 8:

JPA.withTransaction(() -> {
    ImportCrmData.start();
});

И не Java 8 синтаксис:

JPA.withTransaction(new Callback0() {

    @Override
    public void invoke() throws Throwable {
        ImportCrmData.start();
    }
});
Другие вопросы по тегам