如何在glassfish上部署play framework 2.4应用并阅读application.conf

How to deploy play framework 2.4 application on glassfish and read application.conf

我尝试在 glassfish 上部署 play framework 2.4 应用程序。 我使用了 maven play2 插件并使用以下指令制作了 war 文件。

https://play2-maven-plugin.github.io/play2-maven-plugin/1.0.0-beta4/war-packaging.html

之后,我尝试从 glassfish 控制台部署我的 war,但它返回了以下错误。

  Error occurred during deployment: Exception while loading the app : 
  CDI deployment failure:Exception List with 3 exceptions: 
  Exception 0 : org.jboss.weld.exceptions.DeploymentException: 
  WELD-001408: Unsatisfied dependencies for type Environment with qualifiers @Default
   at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject public play.api.db.BoneConnectionPool(Environment)
   at play.api.db.BoneConnectionPool.<init>(BoneConnectionPool.java:0)
   at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:370)
   at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:291)
   at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
   at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:165)
   at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:529)
   at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:515)
   at org.jboss.weld.bootst .... msg.seeServerLog


  Error occurred during deployment: Exception while loading the app : 
  CDI deployment failure:
  WELD-001408: Unsatisfied dependencies for type Database with qualifiers @Default 
   at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject public modules.MyBatisModule$PlayDataSourceProvider(Database) 
   at modules.MyBatisModule$PlayDataSourceProvider.<init>(PlayDataSourceProvider.java:0) . 
   Please see server.log for more details.

而MyBatisModule是这样的

public class MyBatisModule extends org.mybatis.guice.MyBatisModule {

    @Override
    protected void initialize() {
        environmentId("development");
        bindConstant().annotatedWith(Names.named("mybatis.configuration.failFast")).to(true);
        bindDataSourceProviderType(PlayDataSourceProvider.class);
        bindTransactionFactoryType(JdbcTransactionFactory.class);
        addMapperClasses();
    }

    @Singleton
    public static class PlayDataSourceProvider implements Provider<DataSource> {
        final Database db;

        @Inject
        public PlayDataSourceProvider(final Database db) {
            this.db = db;
        }

        public DataSource get() {
            return db.getDataSource();
        }
    }

我认为应用程序无法读取包含数据库设置的 application.conf。但我不确定。

你能给我一些建议吗?

更新 2015/01/13

我在部署应用程序时禁用了 glassfish 控制台上的 "Implicit CDI" 选项。 我现在没有收到第一个错误,但我收到另一个错误...

Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: 
ContainerBase.addChild: start: org.apache.catalina.LifecycleException: com.google.inject.CreationException: 
Unable to create injector, see the following errors: 
1) Error in custom provider, Configuration error: Configuration error
[Cannot connect to database [default]] while locating play.api.db.DBApiProvider while locating play.api.db.DBApi for parameter 0 
at play.db.DefaultDBApi.<init>(DefaultDBApi.java:28) 
at play.db.DefaultDBApi.class(DefaultDBApi.java:28) 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 QualifierInstanc .... msg.seeServerLog

我的 conf/application.conf 在这里:

db.default.driver=oracle.jdbc.driver.OracleDriver
db.default.url="jdbc:oracle:thin:@url..."
db.default.username=username
db.default.password=password

并且它适用于本地环境。 我认为 glassfish 无法读取 war 文件中的 application.conf。

对不起。你能再给我点建议吗?

更新 2015/01/14

以下为完整日志部分

Caused by: Configuration error: Configuration error[Exception during pool initialization]
    at play.api.Configuration$.configError(Configuration.scala:178)
    at play.api.PlayConfig.reportError(Configuration.scala:1048)
    at play.api.db.HikariCPConnectionPool.create(HikariCPModule.scala:69)
    at play.api.db.PooledDatabase.createDataSource(Databases.scala:199)
    at play.api.db.DefaultDatabase.dataSource$lzycompute(Databases.scala:124)
    at play.api.db.DefaultDatabase.dataSource(Databases.scala:122)
    at play.api.db.DefaultDatabase.getConnection(Databases.scala:143)
    at play.api.db.DefaultDatabase.getConnection(Databases.scala:139)
    at play.api.db.DefaultDBApi$$anonfun$connect.apply(DefaultDBApi.scala:44)
    ... 130 more
Caused by: com.zaxxer.hikari.pool.PoolInitializationException: Exception during pool initialization
    at com.zaxxer.hikari.pool.BaseHikariPool.initializeConnections(BaseHikariPool.java:542)
    at com.zaxxer.hikari.pool.BaseHikariPool.<init>(BaseHikariPool.java:171)
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:60)
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:48)
    at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:80)
    at play.api.db.HikariCPConnectionPool$$anonfun.apply(HikariCPModule.scala:58)
    at play.api.db.HikariCPConnectionPool$$anonfun.apply(HikariCPModule.scala:54)
    at scala.util.Try$.apply(Try.scala:191)
    at play.api.db.HikariCPConnectionPool.create(HikariCPModule.scala:54)
    ... 136 more
Caused by: java.sql.SQLException: JDBC4 Connection.isValid() method not supported, connection test query must be configured
    at com.zaxxer.hikari.pool.BaseHikariPool.addConnection(BaseHikariPool.java:441)
    at com.zaxxer.hikari.pool.BaseHikariPool.initializeConnections(BaseHikariPool.java:540)
    ... 144 more

3) Error in custom provider, java.lang.NullPointerException
  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@34850279)) (vi
a modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon)
  while locating play.db.Database annotated with @play.db.NamedDatabase(value=default)

一些想法(应该是评论,但我还不能写评论)。

  1. 如果您的 application.conf 文件在 WEB-INF/classes 目录中,应该可以找到它(更多信息 - https://www.playframework.com/documentation/2.4.x/ProductionConfiguration#Specifying-an-alternate-configuration-file)。

  2. war 文件是否包含 JDBC 驱动程序?

  3. 也许你应该配置 JNDI - https://www.playframework.com/documentation/2.4.x/ScalaDatabaseOthers#Exposing-the-datasource-through-JNDI

我的问题原因是:

Caused by: java.sql.SQLException: JDBC4 Connection.isValid() method not supported, connection test query must be configured

https://www.playframework.com/documentation/2.4.x/Migration24#JDBC-connection-pool

我在 application.conf 上添加以下设置后,它起作用了。 (此 sql 适用于 Oracle 数据库。)

db.default.hikaricp.connectionTestQuery="SELECT 1 FROM DUAL"

感谢您的帮助!