0

我在通过 Hibernate JPA 配置的 C3P0 连接池上有一个奇怪的案例persistence.xml

这是我的persistence.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="public_db" transaction-type="RESOURCE_LOCAL">

        <description>MySQL Persistence Unit</description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://AAA.BBB.CCC.DDD:3306/public_db" />
            <property name="javax.persistence.jdbc.user" value="USERNAME" />
            <property name="javax.persistence.jdbc.password" value="SECRET" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />

            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.transaction.flush_before_completion" value="false" />

            <property name="hibernate.connection.provider_class"
                      value="org.hibernate.connection.C3P0ConnectionProvider" />
            <property name="hibernate.c3p0.min_size" value="5" />
            <property name="hibernate.c3p0.max_size" value="50" />
            <property name="hibernate.c3p0.timeout" value="500" />
            <property name="hibernate.c3p0.max_statements" value="50" />
            <property name="hibernate.c3p0.idle_test_period" value="2000" />
        </properties>
    </persistence-unit>

<persistence>

构建应用 JAR 包后,如果我在本地机器上运行,它可以工作,它可以连接到位于AAA.BBB.CCC.DDD.

但是,如果我在远程服务器上执行 JAR 应用程序,则会收到错误消息:

    [2017-05-19 11:11:02,509 +0700] [DEBUG] [c.m.v.r.BasicResourcePool] An exception occurred while acquiring a poolable resource. Will retry.
    java.sql.SQLException: Access denied for user 'USRENAME'@'AAA.BBB.CCC.EEE' (using password: YES)
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
            at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)
            at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)
            at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:873)
            at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1710)
            at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226)
            at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
            at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
            at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
            at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
            at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
            at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
            at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
            at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:175)
            at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:220)
            at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:206)
            at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:203)
            at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1138)
            at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1125)
            at com.mchange.v2.resourcepool.BasicResourcePool.access$700(BasicResourcePool.java:44)
            at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1870)
            at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)

奇怪的是,我在其中配置了服务器,AAA.BBB.CCC.DDD但 C3P0 数据源正在尝试连接到远程主机上的 MySQL 服务器,即AAA.BBB.CCC.EEE.

我没有在代码中使用localhostor设置任何位置AAA.BBB.CCC.EEE

有没有人经历过这个?

4

1 回答 1

0

经过一段时间的挖掘,这看起来像是网络和数据库配置的问题。在远程主机上运行的数据库帐户没有访问数据库服务器的权限。因此,只需修复权限即可!

这个问题的奇怪之处在于,如果 C3P0 上的 DB 连接超时,它会退回到当前机器上的池,因此 DB IP 地址上的错误消息很难剖析问题。

于 2017-05-23T10:08:11.517 回答