openshift postgres:tomcat上没有合适的驱动程序

时间:2015-10-29 10:26:59

标签: java postgresql tomcat

我已将我的应用程序从localhost移至OpenShift,并发现PostgreSQL连接存在问题。 我已配置 pom.xml

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.4-1204-jdbc41</version>
</dependency>

persistence.xml

    <persistence-unit name="expenseSystem">
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://$OPENSHIFT_POSTGRESQL_DB_HOST:$OPENSHIFT_POSTGRESQL_DB_PORT/new" />
            <property name="javax.persistence.jdbc.user" value="$OPENSHIFT_POSTGRESQL_DB_USERNAME" />
            <property name="javax.persistence.jdbc.password" value="$OPENSHIFT_POSTGRESQL_DB_PASSWORD" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
            <property name="hibernate.c3p0.testConnectionOnCheckout" value="true" />
            <property name="hibernate.c3p0.timeout" value="300" />
            <property name="hibernate.c3p0.idle_test_period" value="3000" />
<!--            <property name="hibernate.hbm2ddl.auto" value="create"/> -->
        </properties>
    </persistence-unit>

但在日志中我仍然发现:

Oct 29, 2015 6:11:08 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.10.Final}
Oct 29, 2015 6:11:08 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Oct 29, 2015 6:11:08 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Oct 29, 2015 6:11:08 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Oct 29, 2015 6:11:08 AM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator instantiateExplicitConnectionProvider
INFO: HHH000130: Instantiating explicit connection provider: org.hibernate.connection.C3P0ConnectionProvider
Oct 29, 2015 6:11:08 AM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH010002: C3P0 using driver: org.postgresql.Driver at URL: jdbc:postgresql://$OPENSHIFT_POSTGRESQL_DB_HOST:$OPENSHIFT_POSTGRESQL_DB_PORT/new
Oct 29, 2015 6:11:08 AM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH000046: Connection properties: {user=$OPENSHIFT_POSTGRESQL_DB_USERNAME, password=****}
Oct 29, 2015 6:11:08 AM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH000006: Autocommit mode: false
Oct 29, 2015 6:11:08 AM com.mchange.v2.log.MLog <clinit>
INFO: MLog clients using java 1.4+ standard logging.
Oct 29, 2015 6:11:08 AM com.mchange.v2.c3p0.C3P0Registry banner
INFO: Initializing c3p0-0.9.2.1 [built 20-March-2013 10:47:27 +0000; debug? true; trace: 10]
Oct 29, 2015 6:11:09 AM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@c4a86e25 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@e71f40d [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1bqonyz9c1dory2qrezkfd|5edf64, idleConnectionTestPeriod -> 3000, initialPoolSize -> 3, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 300, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@f0b8d1c4 [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> 1bqonyz9c1dory2qrezkfd|1019b35, jdbcUrl -> jdbc:postgresql://$OPENSHIFT_POSTGRESQL_DB_HOST:$OPENSHIFT_POSTGRESQL_DB_PORT/new, properties -> {user=******, password=******} ], preferredTestQuery -> null, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> true, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> 1bqonyz9c1dory2qrezkfd|b0612a, numHelperThreads -> 3 ]
Oct 29, 2015 6:11:39 AM com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask run
WARNING: com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@2ef080 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception:
java.sql.SQLException: No suitable driver

当我更改 persistence.xml 以连接到我的localhost数据库时,一切正常。 所以我认为这是凭证问题,但我找不到它。

1 个答案:

答案 0 :(得分:0)

我将应用程序移动到OpenShift托管时也发现了一些特定问题。我认为OpenShift更喜欢使用数据源而不是建立直接连接的一个重要时刻。 您是否尝试过OpenShift blog的下一步方法? 接下来是快速指南:

  1. 在Tomcat server.xml中定义正确的数据源:

    <Resource name="jdbc/postgresqldb" auth="Container" type="javax.sql.DataSource" 
      username="${env.OPENSHIFT_POSTGRESQL_DB_USERNAME}"
      password="${env.OPENSHIFT_POSTGRESQL_DB_PASSWORD}"
      url="jdbc:postgresql://${env.OPENSHIFT_POSTGRESQL_DB_HOST}:${env.OPENSHIFT_POSTGRESQL_DB_PORT}/${env.OPENSHIFT_APP_NAME}"
      driverClassName="org.postgresql.Driver" initialSize="5" maxWait="5000"
      maxActive="120" maxIdle="5" validationQuery="select 1"
      poolPreparedStatements="true">
    </Resource>
    
  2. 您必须进行的下一项更改是context.xml。您必须定义数据源的资源链接,如下所示

    <ResourceLink name="jdbc/postgresqldb" global="jdbc/postgresqldb" type="javax.sql.DataSource"/>
    
  3. 下载PostgreSQL JDBC驱动程序的正确版本(目前OpenShift使用的是9.2版本)

    cd $OPENSHIFT_DATA_DIR
    wget http://repo1.maven.org/maven2/postgresql/postgresql/9.2-1003-jdbc4/postgresql-9.2-1003-jdbc4.jar
    
  4. 接下来更新应用catalina.properties文件夹中的.openshift/config以扫描OPENSHIFT_DATA_DIR,如下所示。我们使用catalina.home系统属性来获取OPENSHIFT_DATA_DIR路径。

    common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar,${catalina.home}/../data/*.jar
    
  5. 现在您可以将更改推送到OpenShift,如下所示。

    git commit -am "made changes for tomcat jndi datasource"
    git push
    
  6. 按如下所示更改persistence.xml

    <persistence>
       <persistence-unit name="REQUIRED_PERSISTENCE_UNIT_NAME_GOES_HERE">
          <non-jta-data-source>jdbc/myDS</non-jta-data-source>
          <!--OTHER YOUR HIBERNATE PROPERTIES-->
       </persistence-unit>
    </persistence>
    
  7. 我希望本手册对您有所帮助。 在另一篇博文here中也描述了几乎相同的步骤。