已更新
无法连接到Postgres数据库。 postgres的依赖关系是通过maven添加的。
Maven依赖
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
Postgres Pool
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)。上次获取尝试异常:org.postgresql.util.PSQLException:错误提示。在org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:152)在org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)在org.postgresql.jdbc2.AbstractJdbc2Connection。(AbstractJdbc2Connection.java: 125)在org.postgresql.jdbc3.AbstractJdbc3Connection。(AbstractJdbc3Connection.java:30)在org.postgresql.jdbc3g.AbstractJdbc3gConnection。(AbstractJdbc3gConnection.java:22)在org.postgresql.jdbc4.AbstractJdbc4Connection。(Abstract:Jdbc4Connection。在org.postgresql.Driver.make(Driver.java:393)在org.postgresql.Driver.connect(Driver.java:267)在org.postgresql.jdbc4.Jdbc4Connection。(Jdbc4Connection.java:24)在com.mchange .v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:119)位于com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:143)位于com.mchange.v2.c3p0.WrapperConnectionPooledSource(getr ource.java:132),位于com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool $ 1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(Basic) .mchange.v2.resourcepool.BasicResourcePool.access $ 800(BasicResourcePool.java:32)位于com.mchange.v2.resourcepool.BasicResourcePool $ AcquireTask.run(BasicResourcePool.java:1810)位于com.mchange.v2.async.ThreadPoolAsynchronousRunner PoolThread.run(ThreadPoolAsynchronousRunner.java:547)原因:javax.net.ssl.SSLHandshakeException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法在java.base /上找到到所请求目标的有效证书路径。 java.base / sun.security.ssl.TransportContext.fatal(TransportContext.java:321)处的sun.security.ssl.Alert.createSSLException(Alert.java:128)在java.base / sun.security.ssl.TransportContext中。 fatal(TransportContext.java:264)在java.base / sun.security.ssl.TransportCo ntext.fatal(TransportContext.java:259)在java.base / sun.security.ssl.CertificateMessage $ T12CertificateConsumer.checkServerCerts(CertificateMessage.java:642)在java.base / sun.security.ssl.CertificateMessage $ T12CertificateConsumer.onCertificate(在java.base / sun.security.ssl.CertificateMessage $ T12CertificateConsumer.consume(CertificateMessage.java:361)处的CertificateMessage.java:461)在java.base / sun.security.ssl.SSLHandshake.consume(SSLHandshake.java:392)中在java.base / sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:444)在java.base / sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:421)在java.base / sun.security .ssl.TransportContext.dispatch(TransportContext.java:178)(位于java.base / sun.security.ssl.SSLTransport.decode(SSLTransport.java:164)位于java.base / sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl .java:1152),位于java.base / sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1063),位于java.base / sun.security.ssl.SSLSocketImpl.startHandshake (SSLSocketImpl.java:402)在java.base / sun.security.ssl.SSLSocketImpl.ensureNegotiated(SSLSocketImpl.java:716)在java.base / sun.security.ssl.SSLSocketImpl $ AppOutputStream.write(SSLSocketImpl.java:970 )的org.postgresql.core.PGStream.flush()的java.base / java.io.BufferedOutputStream.flush(BufferedOutputStream.java:142)的java.base / java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:81) org.postgresql.core.v3上的PGStream.java:523).ConnectionFactoryImpl.sendStartupPacket(ConnectionFactoryImpl.java:259)在org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:105) ...另外16个 引起原因:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法在java.base / sun.security.validator.PKIXValidator.doBuild上找到到所请求目标的有效证书路径。 java.base / sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:290)的java.base / sun.security.validator.Validator.validate(Validator.java:264)的PKIXValidator.java:385) java.base / sun.security.ssl的.base / sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:321)java.base / sun.security.ssl的.base / sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:321) .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 /上的java.base / sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)处找到到请求目标的有效证书路径。位于java.base / java.security.cert.CertPathBuilder.build(CertPathBuilder.java:297)的sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)在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);
答案 0 :(得分:0)
代替props.setProperty("ssl", "false");
试试:
props.setProperty("sslmode", "disable");
有关更多信息,请参见Connecting to Relational Databases on Heroku with Java上的Heroku文档。