Skip to content
Advertisement

The connection attempt failed when try connect PostgreSQL via SSH tunnel. I using Java with jsch lib

I’m facing an issue when I try to connect to PostgreSQL Database after connected SSH successfully. I’m using Java + JSCH library to help connect to DB PostgreSQL via SSH tunnel. Please help to take a look on my code:

JavaScript

An exception is thrown at following step:

JavaScript

Here is result:

JavaScript

I used value above to connect PostgreSQL manually, using pgAdmin4, and connect successfully: See configuration image here.

Please someone could give some ideal for fixing my code.

Advertisement

Answer

int assinged_port = session.setPortForwardingL(port, tunnelHost, tunnelPort);

I think the problem is that you have no idea what any of these values are supposed to mean. You’re passing the wrong values in the wrong places. The assigned_port value makes no sense at all here, for example.

The idea behind an L-type port forward is this:

  • connections to the local port port go across the SSH connection to the server you connected to…
  • and then pop out the other side. The other side will then connect to host tunnelHost, on port tunnelPort, and thus the forward will proceed.

Given that’s how it works, the names and values are all wrong.

  • tunnelPort is surely the ‘port of the tunnel’. In other words, that’s the local port – it should be the first argument passed to setPortForwardingL, not the last.
  • the second and third parameter tell the remote SSH server what to connect to, to finish the ports. Given that you’re SSHing to the server, you don’t want the server to then connect to the server. That sometimes works, but often it doesn’t, depends on network sessions. You instead want localhost there! Remember, that server/port combo is connected to server-side. ‘localhost’ here as value would be referring to what resolved as localhost on the server. Which is what you want.
  • Finally, the point of SSH tunnels is that you have a local port on your local box – that is where the tunnel ‘starts’. Thus, you want your JDBC to connect to localhost. You’re connecting to ipAddress, whatever that might be.

So, the fixed code is:

JavaScript

There are also multiple code style errors in this. For example, if you ever write code that catches an exception, e.printStackTrace() it, and then keeps going, stop what you’re doing. That is horrible, horrible error handling. Just make these methods throw the checked exception. A method that connects to a DB should be throwing SQLException and probably IOException. If you really can’t do that or don’t want to, then either handle the exception, and for the 90%+ of exceptions where there is nothing you can think of (that’s normal), it shuold always end in throw. So not:

JavaScript

Instead, do:

JavaScript

or if you can’t, do:

JavaScript

for example. Preferably, write up how exceptions are handled; throwing the type RuntimeException is a last resort. But still miles better than e.printStackTrace(); and continuing to run!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement