gpt4 book ai didi

java - 在 Play 2.6.X 中使用数据库调度程序线程池时出现错误

转载 作者:行者123 更新时间:2023-11-29 17:52:29 25 4
gpt4 key购买 nike

我正在使用 Play 框架 2.6.12 进行 CRUD 练习。不知何故,当我对数据模型运行测试时,记录已成功插入数据库,但出现如下错误:

[error] i.e.s.t.TransactionManager - NotifyOfCommit failed. L2 Cache potentially not notified.
java.util.concurrent.RejectedExecutionException: Task io.ebeaninternal.server.transaction.PostCommitProcessing$$Lambda$572/1466216168@39a29ab4 rejected from java.util.concurrent.ThreadPoolExecutor@7f63572[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
at io.ebeaninternal.server.lib.DaemonExecutorService.execute(DaemonExecutorService.java:41)
at io.ebeaninternal.server.core.DefaultBackgroundExecutor.execute(DefaultBackgroundExecutor.java:36)
at io.ebeaninternal.server.transaction.TransactionManager.notifyOfCommit(TransactionManager.java:338)
at io.ebeaninternal.server.transaction.JdbcTransaction.notifyCommit(JdbcTransaction.java:927)
at io.ebeaninternal.server.transaction.JdbcTransaction.flushCommitAndNotify(JdbcTransaction.java:984)
at io.ebeaninternal.server.transaction.JdbcTransaction.commit(JdbcTransaction.java:1031)
at io.ebeaninternal.server.transaction.TransactionMap$State.commit(TransactionMap.java:82)

谁能告诉我为什么吗?

这是我的环境:

  • Mac OSX 10.13.3
  • MySQL 5.7.18
  • JDK 1.8
  • JDBC 连接器 5.1.42
  • sbt 1.1.1
  • playframework 2.6.12
  • Intellij IDEA

还有我的 application.conf 文件:

## Database configuration
db {
default.driver=com.mysql.jdbc.Driver
default.url="jdbc:mysql://localhost/crud"
default.username=root
default.password="Pa55word"
}

## Ebean default server
ebean.default = ["models.*"]

## Public Assets
play.assets {
path = "/public"
urlPrefix = "/assets"
}

# Number of database connections
fixedConnectionPool = 9

# Set Hikari to fixed size
play.db {
prototype {
hikaricp.minimumIdle = null ##${fixedConnectionPool} or null
hikaricp.maximumPoolSize = 9 ##${fixedConnectionPool}
}
}

# Job queue sized to HikariCP Connection Pool
database.dispatcher {
executor = "thread-pool-executor"
throughput = 1
thread-pool-executor {
fixed-pool-size = 9 ##${fixedConnectionPool}
}
}

我是否配置错误?

以下是我的练习代码。

类(class)人员:

package models;

import io.ebean.Finder;
import io.ebean.Model;
import play.data.validation.Constraints;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Person extends Model{

@Id
public Long id;

@Constraints.Required
public String name;

public static final Finder<Long, Person> find = new Finder<>(Person.class);

public Person(String name){
this.name = name;
}
}

类 PersonRepository:

package repository;

import io.ebean.*;
import models.Person;
import play.db.ebean.EbeanConfig;
import javax.inject.Inject;
import java.util.concurrent.CompletionStage;
import static java.util.concurrent.CompletableFuture.supplyAsync;

public class PersonRepository {

private final EbeanServer ebeanServer;
private final DatabaseExecutionContext dbec;

@Inject
public PersonRepository(EbeanConfig ebeanConfig,
DatabaseExecutionContext dbec){
this.ebeanServer = Ebean.getServer(ebeanConfig.defaultServer());
this.dbec = dbec;
}

public CompletionStage<Long> insert(Person person){
return supplyAsync(() -> {
ebeanServer.insert(person);
return System.currentTimeMillis(); //just for test
}, dbec);
}

}

类DatabaseExecutionContext:

package repository;

import akka.actor.ActorSystem;
import play.libs.concurrent.CustomExecutionContext;
import javax.inject.Inject;

public class DatabaseExecutionContext extends CustomExecutionContext{

@Inject
public DatabaseExecutionContext(ActorSystem actorSystem){
super(actorSystem, "database.dispatcher");
}
}

和测试代码:

import models.Person;
import org.junit.Test;
import play.test.WithApplication;
import repository.PersonRepository;

public class ModelTest extends WithApplication{

@Test
public void createPersons(){
final PersonRepository repo = app.injector().instanceOf(PersonRepository.class);

Person p1 = new Person("Tom");
repo.insert(p1);

Person p2 = new Person("John");
repo.insert(p2);

Person p3 = new Person("Peter");
repo.insert(p3);
}
}

请帮助我,提前致谢。

最佳答案

原来我的 build.sbt 文件中有错误。我曾经这样声明库依赖项:

libraryDependencies ++= Seq(guice, jdbc, ..., ehcache)

但是在我删除“ehcache”之后,错误消失了,似乎存在库冲突。

关于java - 在 Play 2.6.X 中使用数据库调度程序线程池时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49186997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com