I'm trying to connect to a remote Vertica DB using Java and connecting to it involves an SSH tunnel with the addition of a PEM file as a Public Key.
However, after creating a port forwarding session using JSch AND THEN creating a connection to the DB using JDBC, I am faced with the following error:
java.sql.SQLNonTransientConnectionException: [Vertica]VJDBC Failed to connect to host 10.13.3.213 on port 5433. Reason: Failed to establish a connection to the primary server or any backup address due to network error
The code were I am trying to connect is bellow and after that is the PortForwarder.openConnection method:
int localBindingPort = PortForwarder.openConnection();
JDBCurl = String.format(JDBCClass + "%s:%s/%s", Host, localBindingPort, Schema);
return DriverManager.getConnection(JDBCurl,
username,password);
JDBCurl value ends up as "jdbc:vertica://10.13.3.213:5433/DATA_SERVICES"
public class PortForwarder extends DBConfig {
static String portsString = Port + ":" + Host + ":" + SSHPort; // port:host:hostport
static Session session = null;
public static int openConnection() {
int assigned_port = -1;
JSch jsch = new JSch();
try {
int lport;
String rhost;
int rport;
String[] portParams;
portParams = portsString.split(":");
lport = Integer.parseInt(portParams[0]);
rhost = portParams[1];
rport = Integer.parseInt(portParams[2]);
jsch.addIdentity(pemFile);
session = jsch.getSession(SSHUsername, SSHHost, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
assigned_port = session.setPortForwardingL(lport, rhost, rport);
} catch (Exception e) {
System.err.println(e);
System.exit(1);
}
return assigned_port;
}
public static void closeConnection() {
if (session != null) {
session.disconnect();
}
}
jdbcUrlin preference. afaik, SSH tunnelling normally operates on the basis that one connects to the localhost using the forwarding port. At least that's the way I do it. So by that reckoning, that url could be jdbc:vertica://localhost:5433/DATA_SERVICES but I'm not sure what JSch expects.