使用Java中的jsch连接到远程postgressql数据库时出错

时间:2016-05-12 09:33:13

标签: java postgresql jsch

我有以下代码段:

jsch.addIdentity(configuration.getSSHPrivateKeyPath(), "...");
jsch.setKnownHosts("...");
session = jsch.getSession("...", configuration.getIp(), 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
config.put("Compression", "yes");
config.put("ConnectionAttempts","2");
session.setConfig(config);
Proxy proxy = new ProxyHTTP("...", ...);
session.setProxy(proxy);
session.connect();
session.setPortForwardingL(configuration.getLocalPort(), configuration.getIp(), 5432);

我能够连接并建立端口转发。但是,当我发出以下代码时:

nucConnection = DriverManager.getConnection("jdbc:postgresql://" + "localhost:" + configuration.getLocalPort() + "/" + configuration.getDbname(), configuration.getDbuser(), configuration.getDbpass());

我得到以下异常:

org.postgresql.util.PSQLException: The connection attempt failed.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:257)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:156)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:35)
    at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22)
    at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:47)
    at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:30)
    at org.postgresql.Driver.makeConnection(Driver.java:414)
    at org.postgresql.Driver.connect(Driver.java:282)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at se.foi.oculusdb.rest.server.datapipe.Pipe.run(Pipe.java:185)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:209)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:143)
    at org.postgresql.core.VisibleBufferedInputStream.ensureBytes(VisibleBufferedInputStream.java:112)
    at org.postgresql.core.VisibleBufferedInputStream.read(VisibleBufferedInputStream.java:71)
    at org.postgresql.core.PGStream.ReceiveChar(PGStream.java:282)
    at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:405)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:203)
    ... 12 more

在远程计算机上使用带有凭据和数据库的psql时,我没有任何问题。我必须做一个PostgreSQL配置吗?

PS 在使用Jsch路由之前,我使用了以下代码:

key = Runtime.getRuntime().exec("ssh-add " + configuration.getSSHPrivateKeyPath());
Thread.sleep(1000);
ssh = Runtime.getRuntime().exec("ssh -i " + configuration.getSSHPrivateKeyPath() +  " -L " + configuration.getLocalPort() + ":localhost:5432 gnu-radio-usr@" + configuration.getIp());

在获得上述异常之前会运行任意数小时。这就是我改用Jsch的原因。 DS

0 个答案:

没有答案