Connection Status

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
[294 byte] By [Rubana] at [2007-11-26 19:16:01]
# 1

> 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

p_epia at 2007-7-9 21:28:35 > top of Java-index,Java Essentials,Java Programming...
# 2

> 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.

CeciNEstPasUnProgrammeura at 2007-7-9 21:28:35 > top of Java-index,Java Essentials,Java Programming...
# 3

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?

DrLaszloJamfa at 2007-7-9 21:28:35 > top of Java-index,Java Essentials,Java Programming...
# 4
Hi Friends,Thanks for ur reply..it really satisfes my qstn
Rubana at 2007-7-9 21:28:35 > top of Java-index,Java Essentials,Java Programming...
# 5

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.

ejpa at 2007-7-9 21:28:36 > top of Java-index,Java Essentials,Java Programming...
# 6
well, it IS a connection. If it makes use of a socket (or more than one most likely) under the hood that's not something you should have to concern yourself with (especially as there are drivers that don't use socket based communications, like many type1 and 2 drivers).
jwentinga at 2007-7-9 21:28:36 > top of Java-index,Java Essentials,Java Programming...
# 7

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, ...

ejpa at 2007-7-9 21:28:36 > top of Java-index,Java Essentials,Java Programming...