> Hi Friends,
> I have a doubt , if and Exception
> occurs in the java programming while accessing the
> Database ,say something like sql Exception...what
> happens to the connection ? will it be close
> or still open ? unless we close the connection
> explicitly ....
>
> Regards,
>
> ruban
It depends on description of SQLException such as
Connection reset by peer
Connection is closed
..
etc
> occurs in the java programming while accessing the
> Database ,say something like sql Exception...what
> happens to the connection ?
Mostly nothing. If your car won't start, what happens to the road?
> will it be close
> or still open ? unless we close the connection
> explicitly ....
Still open. Unless the exception happens when connecting, or it makes the DB close the connection.
Of course code could be carefully structured:
Connection con = getThatConnection();
try {
use connection ...
} finally {
con.close();
}
And for long-lived connections, like in a connection pool, a shutdown hook
could attempt to close them.
On the other side, DBMSs will close idle connections.
Why do you ask?
None of these answers is really satisfactory.
The connection may cease to exist for a number of reasons beyond the control of your application, but you still have to close the object that represents your end of the connection. Always.
This is why TCP/IP originally used the word 'socket' for your end of the connection, and why java.sql.Connection is really a poor name for the object representing your end of a database session.
You still have to close it yourself. It is consuming O/S resources.
OK, let me restate. It's the endpoint object of a connection which has two endpoints. If the other endpoint gets closed, the connection is lost but you still have to close your endpoint.
... where 'endpoint' may be a java.net.Socket, java.sql.Connection, java.nio.channels.SocketChannel, ...