'JAVA/MSSQL: com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed

I have got a problem with database queries, when I try to run them from my groovy script. Sometimes, but very often, I've got an error:

com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.checkClosed(SQLServerConnection.java:388)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.createStatement(SQLServerConnection.java:2156)

The module which I used for connection to database is:

import com.microsoft.sqlserver.jdbc.*;
def dbUser = "username"
def dbPassword = 'password'
def dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
def dbUrl = "jdbc:sqlserver://mssql-server"
def sql = Sql.newInstance(dbUrl, dbUser, dbPassword, dbDriver)

The script started by windows scheduler on Windows Server 2012 every 2 minutes. And I use developer machine where I'm making code and running the script from this size also. There was no problem problem, when I used MySQL database, but after migrating database on MSSQL, I see this error.



Solution 1:[1]

That can be caused by several reasons:

  1. A bug in your script. The connection is closed somewhere (maybe because of an exception which you don't handle properly) and then you try to use it again. Check exception and resource handling.

  2. You're keeping a connection open for too long. After a certain timeout, the database closes the connection on their side.

  3. Someone restarted the database server.

It's most likely not a firewall issue; firewalls just drop packets when the connection times out, so the application appears to "hang" instead of giving a good error message.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aaron Digulla