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

For SqlServer please explicitly choose either sqlserver16 or sqlserver17 as the platform via DatabaseConfig.setDatabasePlatformName

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

我的application.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.*"]

我的plugin.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")

我的build.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"

我的Global.java

@Singleton
public class Global{

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

}

我已经试过了

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 服务器的示例 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)
  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(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$c94231.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.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.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.$anonfun$reload(DevServerStart.scala:193)
    at play.utils.Threads$.withContextClassLoader(Threads.scala:22)
    at play.core.server.DevServerStart$$anon.reload(DevServerStart.scala:185)
    at play.core.server.DevServerStart$$anon.get(DevServerStart.scala:148)
    at play.core.server.AkkaHttpServer.handleRequest(AkkaHttpServer.scala:302)
    at play.core.server.AkkaHttpServer.$anonfun$createServerBinding(AkkaHttpServer.scala:224)
    at akka.stream.impl.fusing.MapAsync$$anon.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.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)

经过几天在代码、网络和论坛上的奋斗(实际上 10 天或更长时间),application.conf 路径没有解决方案。我创建了一个 class 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();
}

}

虽然不优雅,但很管用。可能可以帮助某人。