Connection timeout over VPN - java

I have a Java program that connects to SQL Server with mssql-jdbc driver. But some users use this program over VPN and the last days we are having issues with that VPN that is causing slowness. When I ping the machine that have the SQL instance returns a 1800ms ping. Last week this ping was 400~600ms.
When the ping is above 800ms Java returns a timeout to me, so the users using VPN can not connect.
I have tried to set up a higher login timeout in Java connection string to 10 minutes for testing, but even with that the connection always return a timeout. This only happens when the ping is above 800ms so I do not think the problem can be ports, firewall or something like that.
//my connection code
DriverManager.setLoginTimeout(60); //setting up my timeout to 60 seconds
private static final String CONNSTRING = "jdbc:sqlserver://server-name\\SQLEXPRESS;databaseName=db_name;"; //my connection string
conn = DriverManager.getConnection(CONNSTRING, USER, PASS);
Maybe there is another property that I can add to my connection string to solve that problem? Or something that I can set up in SQL Server to allow this connection when the VPN is slow?
I appreciate the help.

Related

CommunicationsException: The last packet sent successfully to the server xxx milliseconds ago

I have a question about MySQL/JDBC connections in Java.
I wrote an application that successfully communicates with a database, but the issue that I recently found out was that my DB connection was dropping, and I need the application to have a connection to the DB at all times.
This is a small snipplet of the error I was getting:
com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 89,225,584 milliseconds ago. The last packet sent successfully to the server was 89,225,584 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1003)
This is also a snipplet of the constructor for my DBConnections class:
private final String url = "jdbc:mysql://localhost:3306/", database.....;
private Connection connection;
public DBConnector(){
try {
// Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url+database, username, password);
} catch (Exception ex) {
System.out.println("Error: " + ex);
}
}
In the errors section, I noticed it's telling me to add autoReconnect=true, I wondered; will my connection still stay up for longer if I structured the connection class like this:
connection = DriverManager.getConnection(url+database+"?autoReconnect=true", username, password);
If not, what else could I do to make sure my connection doesn't drop?
What I would suggest is to use a connection pool (Apache DBCP or HikariCP - the last one is currently having the best performance out of all solutions on the market) with configuration of testing connection before borrowing it from the pool. Depending on the library there should be an option like setTestOnBorrow(true).
In real applications you should always use connection pool instead of manually handling connections.

Can't connect to SQL Server using JDBC

I'm having issues connecting to a Database server with the JDBC driver in Netbeans. I've tried everything, enabling TCP/IP, opening the ports, I've followed tutorials online. It just won't work.
This is the error message I get in the console:
Information: Error: The TCP / IP connection could not be made to the MANUEL-PC host, port 1433. Error: "Connection refused: connect Verify the connection properties, check that there is an instance of SQL Server running on the host and accepting TCP / IP connections on the port and verify that there is no firewall blocking TCP connections on the port. "
Start of the class we're using...
public class DBPosteo
{
private final String URL ="jdbc:sqlserver://MANUEL-PC\\SQLEXPRESS:1433;databaseName=DLC_MotorDeBusqueda;integratedSecurity=true";
private Connection con;
String query = "";
PreparedStatement pstmt;
ResultSet rs;
public void init()
throws ClassNotFoundException, SQLException
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(URL);
}
DB Server name in SQL Management Studio
Thanks in advance for the help... I've never struggled so much with Databases in my life :)
Remove the port number.
Only specify either instance name (SQLEXPRESS) or port number (1433), never both.
Since port 1433 is reserved for the unnamed instance, the SQLEXPRESS named instance would be on a different port, and unless you specifically configured it (unlikely), that port is dynamic and can change on reboots, so you need the named lookup.

What causes ch.ethz.ssh2.Connection to have a hang time?

I am currently using ch.ethz.ssh2.Connection to connect to my servers in java. sometimes it hangs on one server.(maybe like 10-15 seconds). I wanted to know what causes this hang time and how to avoid it.
Connection sample
conn = new ch.ethz.ssh2.Connection(serverName);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(user, pass);
logger.info("Connecting to " + server);
if (isAuthenticated == false) {
logger.info(server + " Please check credentials");
}
sess = conn.openSession();
// I am connecting to over 200 servers and closing them. What would be the best practice to loop thru all these servers in the minimal time.
//some servers quickly connects, while some takes some time.
why does this happen?
The main question is: Is it a code problem, a network problem or a server problem.
A code problem can be debugged - unfortunately ch.ethz.ssh2.Connection does not have any logging possibility to detect what is going inside.
May be you should thing about switching the ssh library (or use it for some tests with the problematic servers). From my experience sshj is very useful.
If it is a network problem or a server problem you can check what is going on via Wireshark. If network packets are sent but the response is delayed the problem is not the used client-side code.
My psychic debugging powers tell me that the server is doing a DNS lookup on the IP address of each client which connects. These DNS lookups are either taking a long time to complete, or they're failing entirely. The DNS lookup will block the authentication process until it finishes, successfully or not.
If the server is the OpenSSH server, this behavior is controlled by the sshd config "UseDNS" option.

Not being able to connect to MySql database

I've had more problems with my database in the past: It could not always get connection. The database runs on a website (webhosting), and I try to access it from my own PC. Here things go wrong, if I access it from localhost to localhost then it works okay.
Error that I get in Java: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
I got no clue why, I'm using similar structure as ever, namely the following:
public class SQL {
private final static String USERNAME = "";
private final static String PASSWORD = "";
private final static String URL = "jdbc:mysql://www.fvheeswijk.nl:3306/p28004_bf4";
private static Connection cachedConnection;
private static void createConnection() {
cachedConnection = null;
Properties connectionProperties = new Properties();
connectionProperties.put("user", USERNAME);
connectionProperties.put("password", PASSWORD);
try {
cachedConnection = (Connection) DriverManager.getConnection(URL, connectionProperties);
} catch (SQLException ex) {
Logger.getLogger(SQL.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static Connection getConnection() {
if (cachedConnection == null) {
createConnection();
}
return cachedConnection;
}
}
The data is blanked out of course.
I then tried to ping my website, all fine.
Later I tried to ping www.fvheeswijk.nl:3306, the database, but Windows cmd cannot find it. Then I tried visiting it via the browser (does this even make sense?), but I got some message along the lines of "packets received out of order". And I have already (way before) added my PC's (Router/Network's) host name to the allowed host list of the database.
Any clue or suggestions what is going wrong?
EDIT: Now I am getting this, might explain something... java.sql.SQLException: null, message from server: "Host '541DB0AA.cm-5-6c.dynamic.ziggo.nl' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'"
The main problem is that you are opening too much connections and probably never closing them or they are being closed by the application server (or wherever you run this application). This can be known from two facts in your post:
private static Connection cachedConnection. The database connection must not be cached manually, instead it should be retrieved only when needed, and closed after being used.
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed. This error is very explicit, you're trying to use a connection that is closed.
Also, you're naively opening connections manually, this is noted here:
cachedConnection = (Connection) DriverManager.getConnection(URL, connectionProperties);
To solve all these problems, you should move to a database connection pool. In short, the connection pool will open a bunch of physical database connections and keep them alive in sleeping status, and will wake up a connection on demand, then when closing the connection, instead of closing the physical connection, it will be back to the sleeping status, thus saving time for opening a new connection.
More info:
Is it a good idea to put jdbc connection code in servlet class?
How to properly keep a DB connection from a Connection Pool opened in JBoss
Java Mysql query database with connection
Should a database connection stay open all the time or only be opened when needed?
About your last edit, seems that you need to close some connections to your database. You should kill some of them and try to connect again using the database pool instead.

java.net.SocketException Connection timed out error

I am getting below error when I am trying to connect to a TCP server. My programs tries to open around 300-400 connections using diffferent threads and this is happening during 250th thread. Each thread uses its own connection to send and receive data.
java.net.SocketException: Connection timed out:could be due to invalid address
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:372)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:233)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:220)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:385)
Here is the code I have that a thread uses to get socket:
socket = new Socket(my_hostName, my_port);
Is there any default limit on number of connections that a TCP server can have at one time? If not how to solve this type of problems?
You could be getting a connection timeout if the server has a ServerSocket bound to the port you are connecting to, but is not accepting the connection.
If it always happens with the 250th connection, maybe the server is set up to only accept 250 connections. Someone has to disconnect so you can connect. Or you can increase the timeout; instead of creating the socket like that, create the socket with the empty constructor and then use the connect() method:
Socket s = new Socket();
s.connect(new InetSocketAddress(my_hostName, my_port), 90000);
Default connection timeout is 30 seconds; the code above waits 90 seconds to connect, then throws the exception if the connection cannot be established.
You could also set a lower connection timeout and do something else when you catch that exception...
Why all the connections? Is this a test program? In which case be aware that opening large numbers of connections from a single client stresses the client in ways that aren't exercised by real systems with large numbers of different client hosts, so test results from that kind of client aren't all that valid. You could be running out of client ports, or some other client resource.
If it isn't a test program, same question. Why all the connections? You'd be better off running a connection pool and reusing a much smaller number of connections serially. The network only has so much bandwidth after all; dividing it by 400 isn't very useful.

Categories

Resources