c3p0 and Heroku postgres, issues with SSL



>更新

无法连接到 postgres 数据库。postgres 的依赖性是通过 maven 添加的。

Maven dependency

<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>

波斯特格雷斯池

public class PostgresDB implements DBPool {
public PostgresDB()  {
}
private static ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
static {
try {
Properties props = new Properties();
props.setProperty("ssl", "false");
comboPooledDataSource.setDriverClass( "org.postgresql.Driver" );
comboPooledDataSource.setJdbcUrl("jdbc:postgresql://ec2-79-125-4-72.eu-west-1.compute.amazonaws.com:5432/d35hi5u7rnue7f");
comboPooledDataSource.setUser("user");
comboPooledDataSource.setPassword("xxxxxxxx");
comboPooledDataSource.setProperties(props);
} catch (Exception e) {
System.out.println("Error");
// handle the exception
}
}
public Connection getConnection() throws SQLException {
return comboPooledDataSource.getConnection();
}
}

用于获取连接的代码

Connection connection = PostgresDB.getConnection();

错误

警告:com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@2326357e -- 获取尝试失败!!清算待处理的收购。在尝试获取所需的新资源时,我们未能成功超过允许的最大获取尝试次数 (30)。Last acquisition try exception: org.postgresql.util.PSQLException: Der Verbindungsversuch schlug fehl.at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:152) at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66) at org.postgresql.jdbc2.AbstractJdbc2Connection.(AbstractJdbc2Connection.java:125) at org.postgresql.jdbc3.AbstractJdbc3Connection.(AbstractJdbc3Connection.java:30) at org.postgresql.jdbc3g.AbstractJdbc3gConnection.(AbstractJdbc3gConnection.java:22) at org.postgresql.jdbc4.AbstractJdbc4Connection.(AbstractJdbc4Connection.java:32) at org.postgresql.jdbc4.Jdbc4Connection.(Jdbc4Connection.java:24) at org.postgresql.Driver.makeConnection(Driver.java:393) at org.postgresql.Driver.connect(Driver.java:267) at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:119) at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:143) at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:132) at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137) at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014) at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32) at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810) atcom.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsyncRunner.java:547) 由: javax.net.ssl.SSLHandshakeException: PKIX 路径构建失败: sun.security.provider.certpath.SunCertPathBuilderException: 无法在java.base/sun.security.ssl.Alert.createSSLException(Alert.java:128) at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:321) at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:264) at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:259) at java.base/sun.security.ssl.CertificateMessage$T12CertificateConsumer.checkServerCerts(CertificateMessage.java:642) at java.base/sun.security.ssl.CertificateMessage$T12CertificateConsumer.onCertificate(CertificateMessage.java:461) at java.base/sun.security.ssl.CertificateMessage$T12CertificateConsumer.consumption(CertificateMessage.java:361) at java.base/sun.security.ssl.SSLHandshake.consumption(SSLHandshake.java:392) at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:444) at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:421) at java.base/sun.security.ssl.TransportContext.dispatch(TransportContext.java:178) at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:164) atjava.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1152) at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1063) at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:402) at java.base/sun.security.ssl.SSLSocketImpl.ensureNegotiated(SSLSocketImpl.java:716) at java.base/sun.security.ssl.SSLSocketImpl$AppOutputStream.write(SSLSocketImpl.java:970) at java.base/java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:81) at java.base/java.io.BufferedOutputStream.flush(BufferedOutputStream.java:142) at org.postgresql.core.PGStream.flush(PGStream.java:523) at org.postgresql.core.v3.ConnectionFactoryImpl.sendStartupPacket(ConnectionFactoryImpl.java:259) at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:105) ...还有 16 个由:sun.security.validator.Validator异常:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilder异常:无法在java.base/sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)at java.base/sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:290)at java.base/sun.security.validator.Validator(Validator.java:264) at java.base/sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:321) at java.base/sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:221) at java.base/sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:129) at java.base/sun.security.ssl.CertificateMessage$T12CertificateConsumer.checkServerCerts(CertificateMessage.java:626) ...33 更多 由: sun.security.provider.certpath.SunCertPathBuilderException: 无法在java.base/sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141) at java.base/sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126) at java.base/java.security.cert.CertPathBuilder.build(CertPathBuilder.java:297) at java.base/sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380) ...39 更多

修复

如果您有同样的问题,请将代码更改为:

comboPooledDataSource.setJdbcUrl("jdbc:postgresql://url:port/database");
Properties props = new Properties();
props.setProperty("sslmode", "require");
props.setProperty("ssl", "true");
props.setProperty("sslfactory", "org.postgresql.ssl.NonValidatingFactory");
props.setProperty("user", "username");
props.setProperty("password", "xxxxxxx");
comboPooledDataSource.setProperties(props);

与其尝试props.setProperty("ssl", "false");

props.setProperty("sslmode", "disable");

有关更多信息,请参阅 Heroku 关于使用 Java 连接到 Heroku 上的关系数据库的文档。

最新更新