java 用Ebean实现Play框架中的双数据库

zdwk9cvp  于 2023-02-28  发布在  Java
关注(0)|答案(1)|浏览(106)

我是全新的Play FrameworkEbean。我需要配置一个播放应用程序使用两个不同的数据库示例,一个用于阅读,另一个用于写入。我一直在寻找它似乎是一个反复出现的问题,我已经尝试了所有可能的解决方案,我已经能够找到,即使是一次对某人工作,对我不工作,我确信我做错了什么,因为这个错误与classloader有关,或者看起来像classloader
build.sbt

lazy val root = (project in file(".")).enablePlugins(PlayJava, PlayEbean)
val playEbean = "6.0.0"
libraryDependencies ++= Seq(
...
"com.typesafe.play" % "play-ebean_2.13" % playEbean,
...

application.conf

db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost:5432/db_writer"
db.default.username=username
db.default.password=pass

db.reader.driver=org.postgresql.Driver
db.reader.url="jdbc:postgresql://localhost:5432/db_reader"
db.reader.username=username
db.reader.password=pass

ebean.default = ["models.db_writer.*"]
ebean.reader = ["models.db_reader.*"]

play.ebean.defaultDatasource = "db_writer"

EbeanServerReader

import io.ebean.Ebean;
import io.ebean.EbeanServer;

public class EbeanServerReader {

    public static EbeanServer getReader() {
        return Ebean.getServer("reader");
    }
}

myDaoClass

public class myDaoClass {

    private final FetchConfig fetchConfig = new FetchConfig();
    private final EbeanServer reader = EbeanServerReader.getReader();
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
      public PagedList<SomeClass> findAllWithNoLimit(int pageIndex, int pageSize, String type) {
        return reader.find(SomeClass.class)
                .fetch("path", fetchConfig.lazy(100))
                .fetch("path", fetchConfig.lazy(100))
                .fetch("path", fetchConfig.lazy(100))
                .where()
                .eq("propertyName", type)
                .order()
                .desc("updatedAt")
                .setFirstRow(pageIndex * pageSize)
                .setMaxRows(pageSize)
                .findPagedList();
    }

错误跟踪:

1) Error injecting constructor, java.lang.NoClassDefFoundError: Could not initialize class io.ebean.Ebean
  at models.myDaoClass.<init>(myDaoClass.java:12)
  while locating models.myDaoClass
    for the 1st parameter of models.myDaoClass.<init>(myDaoClass.java:33)
  while locating models.myDaoClass

1 error
    at com.google.inject.internal.InternalProvisionException.toProvisionException(InternalProvisionException.java:226)
    at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1097)
    at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1131)
    at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:436)
    at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:431)
    at play.api.inject.ContextClassLoaderInjector.$anonfun$instanceOf$2(Injector.scala:119)
    at play.api.inject.ContextClassLoaderInjector.withContext(Injector.scala:128)
    at play.api.inject.ContextClassLoaderInjector.instanceOf(Injector.scala:119)
    at play.api.libs.concurrent.ActorRefProvider.$anonfun$get$1(Akka.scala:268)
    at akka.actor.TypedCreatorFunctionConsumer.produce(IndirectActorProducer.scala:91)
    at akka.actor.Props.newActor(Props.scala:226)
    at akka.actor.ActorCell.newActor(ActorCell.scala:613)
    at akka.actor.ActorCell.create(ActorCell.scala:640)
    ... 9 common frames omitted
Caused by: java.lang.NoClassDefFoundError: Could not initialize class io.ebean.Ebean
    at module.EbeanServerReader.getReader(EbeanServerReader.java:8)

我做错了什么?我试过其他的东西,像@Inject,但是是一样的。
此致

mwngjboj

mwngjboj1#

通过更换以下组件解决了问题:

ebean.default = ["models.db_writer.*"]
ebean.reader = ["models.db_reader.*"]

用这个

ebean.default = ["models.*"]
ebean.writer = ["models.*"]

因为我的应用程序使用相同的class model,只是不同的db示例,一个用于读取,另一个用于写入。

相关问题