Hi,
I have a Web application, using Tomcat5 and Microsoft SQL Server. It has been working fine for a couple of years, but recently it has sporadically started giving the following error:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
....
Once this error occurs, the website is down until Tomcat is restarted.
I am not sure whether it has been caused by a relatively recent piece of bad code, or by more users.
My hunch is that it is caused by all the connections to the database being used up. Probably, there are lots of idle connections that are getting lost.
Maybe, I am thinking, I have code that is crashing out and not closing the database connection.
Now, I know that I should do the following:
Connection conn = null;
try {
conn =ds.getConnection ();
}
catch (SQLException e {}
finally {
if (conn! = null)
try {
conn.close ();
}
catch (SQLException e) {}
}
However, I don't yet do this everywhere. I will, but I am not sure it will solve the problem.
My main question is what happens if I am using an object which contains a database connection:
MyObj o = new MyObj(); // contains db connection
AnotherObj a = AnotherObj();
a.doSomethingBadWhichIsFatal(); // causes the code to exception
��
o.doSomething() //never reached, and connection never closed
Presumably, this could also result in the database connection not being closed?
Do I need to wrap anything which contains anything that contains a database connection in the try/catch/finally model above?
Do you think that this is the cause of the original error I quoted above?
I am assuming that database connection pooling (which I don't currently implement) will not actually help me with the original error I quoted, and only helps in reducing the overhead of creating new connections?
Thanks in advance,
Jonny Cavell