gpt4 book ai didi

java - 开放式 postgres : No suitable driver on tomcat

转载 作者:行者123 更新时间:2023-11-28 23:28:54 25 4
gpt4 key购买 nike

我已将我的应用程序从本地主机移至 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 以连接到我的本地主机数据库时,一切正常。所以我认为这是凭据问题,但我找不到它。

最佳答案

在将我的应用程序移至 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. 接下来更新应用程序 .openshift/config 文件夹中的 catalina.properties 以扫描 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>

希望本手册对您有所帮助。另一篇博文中也描述了几乎相同的步骤 here .

关于java - 开放式 postgres : No suitable driver on tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33411686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com