java - 对于 SqlServer,请通过 DatabaseConfig.setDatabasePlatformName 明确选择 sqlserver16 或 sqlserver17 作为平台

我正在使用 sbt 1.6.2 和 Scala 2.13.8 开发 Java Play Framework 模板 2.8.13,我来自从 2.1.5 升级到上述版本的游戏。问题是当我尝试连接到 SQL Server (SQL Server Management Studio v18.4,服务器版本为 14.0.1000.169)

我的应用程序.conf:

play.db {

  config = "db"
  default = "default"
  
  prototype {
    # Sets a fixed JDBC connection pool size of 50
    hikaricp.minimumIdle = 5
    hikaricp.maximumPoolSize = 5
  }
}


db {
    default.hikaricp.connectionTestQuery="SELECT 1" 
    default.driver=net.sourceforge.jtds.jdbc.Driver
    default.url="jdbc:jtds:sqlserver://127.0.0.1:1433/dbName" 
    default.username="sa"
    default.password="test12"
    default.databasePlatformName="sqlserver17"
    default.logstatements=true
}

ebean.default = ["models.*"]

我的插件。sbt

addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.15")
addSbtPlugin("org.foundweekends.giter8" % "sbt-giter8-scaffold" % "0.13.1")
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.2.4")
addSbtPlugin("com.typesafe.play" %% "sbt-play-ebean" % "6.2.0-RC4")

我的构建。sbt

name := """PRECIEMSA"""
organization := "com.ciemsa"

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayJava, PlayEbean)

scalaVersion := "2.13.8"

libraryDependencies +=  guice
libraryDependencies +=  ehcache
libraryDependencies +=  javaJdbc
libraryDependencies +=  jdbc


libraryDependencies +=  "com.google.inject" % "guice" % "5.1.0"
libraryDependencies += "com.typesafe.play" %% "play-ebean" % "6.2.0-RC4"
libraryDependencies +=  "net.sourceforge.jtds" % "jtds" % "1.3.1"

我的全球。java

@Singleton
public class Global{

    @Inject
    public Global(Database pDb) {
        this.db = pDb;
    }

}

我已经试过了

  • 验证与 SQL > 的连接 OK
  • 新驱动程序如:
db.default.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
db.default.url="jdbc:sqlserver://localhost\\MSSQLSERVER:1433;databaseName=dbName"
db.default.username=sa
db.default.password="test12" 
db.default.logSql=true
db.default.hikaricp.connectionTestQuery="SELECT 1"

和变体。最后一个运行在一个带有 play 2.1.5 的旧版本项目上

不知道现在如何连接。

那么,也许有人有 SQL Server 的示例 application.conf?

堆栈跟踪错误。

2022-05-23 13:55:52 [1;31mERROR[0;39m [36mp.api.http.DefaultHttpErrorHandler[0;39m [35m[0;39m 

! @7nm0g1542 - Internal server error, for (GET) [/] ->
 
play.api.UnexpectedException: Unexpected exception[CreationException: Unable to create injector, see the following errors:

1) Error injecting constructor, javax.persistence.PersistenceException: java.lang.IllegalArgumentException: For SqlServer please explicitly choose either sqlserver16 or sqlserver17 as the platform via DatabaseConfig.setDatabasePlatformName. Refer to issue #1340 for more details
  at play.db.ebean.EbeanDynamicEvolutions.<init>(EbeanDynamicEvolutions.java:36)
  at play.db.ebean.EbeanDynamicEvolutions.class(EbeanDynamicEvolutions.java:33)
  while locating play.db.ebean.EbeanDynamicEvolutions
  at play.db.ebean.EbeanModule.bindings(EbeanModule.java:21):
Binding(class play.api.db.evolutions.DynamicEvolutions to ConstructionTarget(class play.db.ebean.EbeanDynamicEvolutions) eagerly) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$4)
  while locating play.api.db.evolutions.DynamicEvolutions
Caused by: javax.persistence.PersistenceException: java.lang.IllegalArgumentException: For SqlServer please explicitly choose either sqlserver16 or sqlserver17 as the platform via DatabaseConfig.setDatabasePlatformName. Refer to issue #1340 for more details
    at io.ebeaninternal.server.core.DatabasePlatformFactory.create(DatabasePlatformFactory.java:70)
    at io.ebeaninternal.server.core.DefaultContainer.setDatabasePlatform(DefaultContainer.java:215)
    at io.ebeaninternal.server.core.DefaultContainer.createServer(DefaultContainer.java:99)
    at io.ebeaninternal.server.core.DefaultContainer.createServer(DefaultContainer.java:35)
    at io.ebean.DatabaseFactory.createInternal(DatabaseFactory.java:130)
    at io.ebean.DatabaseFactory.create(DatabaseFactory.java:78)
    at io.ebean.EbeanServerFactory.create(EbeanServerFactory.java:50)
    at play.db.ebean.EbeanDynamicEvolutions.lambda$start$2(EbeanDynamicEvolutions.java:50)
    at java.util.HashMap.forEach(HashMap.java:1289)
    at play.db.ebean.EbeanDynamicEvolutions.start(EbeanDynamicEvolutions.java:50)
    at play.db.ebean.EbeanDynamicEvolutions.<init>(EbeanDynamicEvolutions.java:39)
    at play.db.ebean.EbeanDynamicEvolutions$$FastClassByGuice$$52c94231.newInstance(<generated>)
    at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance(DefaultConstructionProxyFactory.java:89)
    at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:114)
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:91)
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:306)
    at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
    at com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:168)
    at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:39)
    at com.google.inject.internal.FactoryProxy.get(FactoryProxy.java:62)
    at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
    at com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:168)
    at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:39)
    at com.google.inject.internal.InternalInjectorCreator.loadEagerSingletons(InternalInjectorCreator.java:213)
    at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:184)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:111)
    at com.google.inject.Guice.createInjector(Guice.java:87)
    at com.google.inject.Guice.createInjector(Guice.java:78)
    at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:200)
    at play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:155)
    at play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)
    at play.core.server.DevServerStart$$anon$1.$anonfun$reload$2(DevServerStart.scala:193)
    at play.utils.Threads$.withContextClassLoader(Threads.scala:22)
    at play.core.server.DevServerStart$$anon$1.reload(DevServerStart.scala:185)
    at play.core.server.DevServerStart$$anon$1.get(DevServerStart.scala:148)
    at play.core.server.AkkaHttpServer.handleRequest(AkkaHttpServer.scala:302)
    at play.core.server.AkkaHttpServer.$anonfun$createServerBinding$1(AkkaHttpServer.scala:224)
    at akka.stream.impl.fusing.MapAsync$$anon$30.onPush(Ops.scala:1307)
    at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:542)
    at akka.stream.impl.fusing.GraphInterpreter.processEvent(GraphInterpreter.scala:496)
    at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:390)
    at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:650)
    at akka.stream.impl.fusing.GraphInterpreterShell$AsyncInput.execute(ActorGraphInterpreter.scala:521)
    at akka.stream.impl.fusing.GraphInterpreterShell.processEvent(ActorGraphInterpreter.scala:625)
    at akka.stream.impl.fusing.ActorGraphInterpreter.akka$stream$impl$fusing$ActorGraphInterpreter$$processEvent(ActorGraphInterpreter.scala:800)
    at akka.stream.impl.fusing.ActorGraphInterpreter$$anonfun$receive$1.applyOrElse(ActorGraphInterpreter.scala:818)
    at akka.actor.Actor.aroundReceive(Actor.scala:537)
    at akka.actor.Actor.aroundReceive$(Actor.scala:535)
    at akka.stream.impl.fusing.ActorGraphInterpreter.aroundReceive(ActorGraphInterpreter.scala:716)
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:580)
    at akka.actor.ActorCell.invoke(ActorCell.scala:548)
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:270)
    at akka.dispatch.Mailbox.run(Mailbox.scala:231)
    at akka.dispatch.Mailbox.exec(Mailbox.scala:243)
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
    at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
    at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:172)
Caused by: java.lang.IllegalArgumentException: For SqlServer please explicitly choose either sqlserver16 or sqlserver17 as the platform via DatabaseConfig.setDatabasePlatformName. Refer to issue #1340 for more details
    at io.ebeaninternal.server.core.DatabasePlatformFactory.byDatabaseMeta(DatabasePlatformFactory.java:153)
    at io.ebeaninternal.server.core.DatabasePlatformFactory.byDataSource(DatabasePlatformFactory.java:135)
    at io.ebeaninternal.server.core.DatabasePlatformFactory.create(DatabasePlatformFactory.java:67)

回答1

经过几天的代码、网络和论坛(实际上是 10 天或更长时间)的斗争,并且 application.conf 路径没有解决方案。我创建一个类 Conexion

public class Conexion {

public static Database ObtenerConexion() {
    
    DataSourceConfig dataSourceConfig = new DataSourceConfig();
    dataSourceConfig.setUsername("sa");
    dataSourceConfig.setPassword("sa");
    dataSourceConfig.setUrl("jdbc:sqlserver://localhost:1433;databaseName=dbName;encrypt=true;trustServerCertificate=true");
    dataSourceConfig.setDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver");

    ServerConfig config= new ServerConfig();
    config.setDatabasePlatformName("sqlserver17");
    config.setDataSourceConfig(dataSourceConfig);
    
    config.addClass(CategoriaRubro.class);
    config.addClass(CentroComun.class);
    config.addClass(Cotizacion.class);
    config.addClass(Departamento.class);
    //all your models 
    
    Database database = EbeanServerFactory.create(config);
    
    return database;
}

}

并从

public class SomeController extends Controller{
static Database db ;

public SomeController () {
    this.db = Conexion.ObtenerConexion();
}

}

它并不优雅,但它有效。可能可以帮助某人。

相似文章

r - `dplyr` 结果在过滤和分离行时发生变化

我有关于对话问题期间学生反应的数据。瞳孔数据在A*、B*和C*列中串在一起,以*intpl_new结尾的列显示插值的瞳孔区域values,以*dur结尾的列给出每个瞳孔的持续时间观察。数据框df包含两...

最新文章