【问题标题】:Play Framework fails to read db config and to inject JPAApiPlay Framework 无法读取数据库配置并注入 JPAApi
【发布时间】:2016-06-24 10:16:15
【问题描述】:

我正在尝试使用 play 2.5,但失败了。

我的控制器:

public class HomeController extends Controller {

    private JPAApi db;

    @Inject
    public HomeController(JPAApi api) {
        this.db = api;
    }

    public Result index(String name) {
        return ok(views.html.index.render("hello", name, Collections.singletonList("world")));
    }
}

我的 conf\META-INF\persistence.xml

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
  <persistence-unit name="readwritePU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <non-jta-data-source>ReadWriteDS</non-jta-data-source>

    <properties>
      <property name="hibernate.connection.isolation" value="1" />
      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL94Dialect" />
      <property name="hibernate.cache.use_second_level_cache" value="false" />
      <property name="hibernate.cache.use_query_cache" value="false" />
    </properties>
  </persistence-unit>
</persistence>

我的 conf\application.conf

jpa.default=readwritePU

db {
  #pool = "hikaricp"

  default.driver = org.postgresql.Driver
  default.url = "jdbc:postgresql://127.0.0.1:5432/mydbname"
  default.username = "postgres"
  default.password = "123456"
  #default = "default"

  default.jndiName=ReadWriteDS
}

在上面的 application.conf 中启用和启用池配置键的错误:

    No configuration setting found for key 'pool'
Error in custom provider, com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'pool'
  while locating play.api.db.DBApiProvider
  while locating play.api.db.DBApi
    for field at play.api.db.NamedDatabaseProvider.dbApi(DBModule.scala:80)
  while locating play.api.db.NamedDatabaseProvider
  at com.google.inject.util.Providers$GuicifiedProviderWithDependencies.initialize(Providers.java:149)
  at play.api.db.DBModule$$anonfun$namedDatabaseBindings$1.apply(DBModule.scala:34):
Binding(interface play.api.db.Database qualified with QualifierInstance(@play.db.NamedDatabase(value=default)) to ProviderTarget(play.api.db.NamedDatabaseProvider@7af4d3bc)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
Caused by: com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'pool'

    pool has type STRING rather than OBJECT
Error in custom provider, com.typesafe.config.ConfigException$WrongType: application.conf @ file:/E:/workspace2/coup/target/scala-2.11/classes/application.conf: 344: pool has type STRING rather than OBJECT
  while locating play.api.db.DBApiProvider
  while locating play.api.db.DBApi
    for parameter 0 at play.db.DefaultDBApi.<init>(DefaultDBApi.java:27)
  at play.db.DefaultDBApi.class(DefaultDBApi.java:27)
  while locating play.db.DefaultDBApi
  while locating play.db.DBApi
    for field at play.db.DBModule$NamedDatabaseProvider.dbApi(DBModule.java:61)
  while locating play.db.DBModule$NamedDatabaseProvider
  at com.google.inject.util.Providers$GuicifiedProviderWithDependencies.initialize(Providers.java:149)
  at play.db.DBModule.bindings(DBModule.java:40):
Binding(interface play.db.Database qualified with QualifierInstance(@play.db.NamedDatabase(value=default)) to ProviderTarget(play.db.DBModule$NamedDatabaseProvider@3ec40700)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
Caused by: com.typesafe.config.ConfigException$WrongType: application.conf @ file:/E:/workspace2/coup/target/scala-2.11/classes/application.conf: 344: pool has type STRING rather than OBJECT

我尝试了很多不同的选项和组合,但我没有想法。谷歌甚至不知道这些错误。播放停止工作,我必须重新启动它。

【问题讨论】:

    标签: java hibernate jpa playframework playframework-2.0


    【解决方案1】:

    试试

    jpa.default=readwritePU
    
    db {
        default {
            hikaricp {
                dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
                dataSource {
                    serverName = localhost
                    databaseName = "mydbname"
                    user = "postgres"
                    password = "123456"
                }
            }
            jndiName=ReadWriteDS
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 2015-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多