1

如何在启动时对 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)

请给我一些帮助。

我在 java 中使用 playframework 2.2.4

- - - - - - - - - - - - - - - - - - -编辑 - - - - - - -----------------------------------------

添加后:

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

1 回答 1

4

删除@Transactional注释,它们仅在控制器中有用。用方法包装 CRM 导入JPA.withTransaction。Java 8 语法:

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

和非 Java 8 语法:

JPA.withTransaction(new Callback0() {

    @Override
    public void invoke() throws Throwable {
        ImportCrmData.start();
    }
});
于 2014-12-08T15:21:03.243 回答