[Help needed] SqlException occurs: Connecting to a remote DB with SSH
Posted by: Çağlar Gülçehre
Date: November 06, 2007 07:53AM

Hello to all. I am getting this exception while trying to connect to a remote MySQL server within SSH. I am using JSCH for SSH tunneling. Here is the my exception:

Connecting to xyz@remoteip...
Connected to xyz@remoteip
SQLException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
BUILD SUCCESSFUL (total time: 27 seconds)

And here is what my code looks like:

static Connection conn = null;
static String userid="xyz", password = "1234";
static String url = "jdbc:mysql://localip:4000//tkdeneme";


public static Connection initMySQLJDBCConnection() throws InstantiationException, IllegalAccessException{

try {
Class.forName ("com.mysql.jdbc.Driver").newInstance ();

} catch(java.lang.ClassNotFoundException e) {}

try {
conn= DriverManager.getConnection(url,userid, password);
} catch(SQLException ex) {
System.err.println("SQLException: " + ex.toString());
}

return conn;
}
//Some other functions...
private static JSch jsch=null;
private static Session session=null;
private static String last_uh="";
private static Session getSession(String user, String host, String password) throws JSchException{

if(jsch==null){
jsch=new JSch();

String foo=user+"@"+host;
if(last_uh.equals(foo) && session!=null){
return session;
}
if(session!=null){
try{session.disconnect();}catch(Exception e){}
session=null;
}
Session _session=jsch.getSession(user, host,22);
_session.setUserInfo(new MyUserInfo(password));

_session.connect();
_session.setPortForwardingL(4000, host,3306);
last_uh=foo;
session=_session;
return session;
}
private static void connect(){
String user="xyz";
String host="remoteip";
String password="1234";
System.out.println("Connecting to "+user+"@"+host+"...");
try{
Session session=null;
try{
session=getSession(user,host,password);
}
catch(JSchException ee){
System.out.println(ee.toString());
return;
}

System.out.println("Connected to "+user+"@"+host);
}
catch(Exception e){
//System.err.println(e);
}
}
//Some other functions...
public static void main(String[] args) {
try{
connect();
initMySQLJDBCConnection();
}
catch(Exception ex){
System.err.println(ex.toString());
}
//...Other stuffs
}
}

I cut off some parts and put the only necessary parts of my code. I am doing port forwadding for tunneling in the below part of the code:

_session.setPortForwardingL(4000, host,3306);

and the 4000 port is closed in the server. Can this be the reason?

All kinds of helps wellcomed. Please help me I am gone mad. I have been tangling with this exception for 1 week. My brain neurons refuse to work.

Thanx in advance.

Options: ReplyQuote


Subject
Written By
Posted
[Help needed] SqlException occurs: Connecting to a remote DB with SSH
November 06, 2007 07:53AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.