Thread: Calling Connection.close() in Connection.finalize()

Calling Connection.close() in Connection.finalize()

From
Matthias Brunner
Date:
Hello,

IMHO, it would be useful if Connection.close() was called from
Connection.finalize(), if the connection is still open. Thus the connection
would always be closed properly.
The mysql JDBC driver, for instance, does this.

Or is this already implemented in the current development codebase?


Best regards!
--
Matthias Brunner <mb@blumenstrasse.vol.at>
PGP FP 7862 32B3 3B75 292A F76F  5042 8587 21AB 5B89 D501
Check out http://blumenstrasse.vol.at/~mb/gpgkey.asc


Re: Calling Connection.close() in Connection.finalize()

From
Felipe Schnack
Date:
  I downloaded the development version today and got a compilation error
about "${minor}" not found. What's this?
--

Felipe Schnack
Analista de Sistemas
felipes@ritterdosreis.br
Cel.: (51)91287530
Linux Counter #281893

Faculdade Ritter dos Reis
www.ritterdosreis.br
felipes@ritterdosreis.br
Fone/Fax.: (51)32303328


Re: Calling Connection.close() in Connection.finalize()

From
Barry Lind
Date:
Matthias,

This is implemented and has been since 7.1.

--Barry

Matthias Brunner wrote:

>Hello,
>
>IMHO, it would be useful if Connection.close() was called from
>Connection.finalize(), if the connection is still open. Thus the connection
>would always be closed properly.
>The mysql JDBC driver, for instance, does this.
>
>Or is this already implemented in the current development codebase?
>
>
>Best regards!
>
>



Re: Calling Connection.close() in Connection.finalize()

From
Dave Cramer
Date:
Do a make clean, and then do a make, and it will go away

Dave
On Thu, 2002-08-15 at 12:39, Felipe Schnack wrote:
>   I downloaded the development version today and got a compilation error
> about "${minor}" not found. What's this?
> --
>
> Felipe Schnack
> Analista de Sistemas
> felipes@ritterdosreis.br
> Cel.: (51)91287530
> Linux Counter #281893
>
> Faculdade Ritter dos Reis
> www.ritterdosreis.br
> felipes@ritterdosreis.br
> Fone/Fax.: (51)32303328
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>
>




Re: Calling Connection.close() in Connection.finalize()

From
Matthias Brunner
Date:
On Thursday 15 August 2002 18:50, Barry Lind wrote:
>
> This is implemented and has been since 7.1.
>

I'm sorry to be such a pain in the neck :-) Created a test class now (file
attached).

$md5sum pgjdbc2.jar
f4ed2deaa88e16e79ccfa1c5b1f5ca22  pgjdbc2.jar
$export CLASSPATH=pgjdbc2.jar:.
$javac PgTest.java
$java PgTest

Message in /var/log/postgresql:
DEBUG:  connection: host=127.0.0.1 user=mb database=mb
DEBUG:  pq_recvbuf: unexpected EOF on client connection

When I call Connection.close() explicitly the second message does not appear.

Versions:
postgresql 7.2,
JDBC driver for 7.2
Sun JDK 1.4 (build 1.4.0_01-b03)
Linux 2.4.18
--
Matthias Brunner <mb@blumenstrasse.vol.at>
PGP FP 7862 32B3 3B75 292A F76F  5042 8587 21AB 5B89 D501
Check out http://blumenstrasse.vol.at/~mb/gpgkey.asc

Attachment

Re: Calling Connection.close() in Connection.finalize()

From
Dave Cramer
Date:
Just tried your code with the latest dev driver and I didn't get an
exception

Dave
On Thu, 2002-08-15 at 13:16, Matthias Brunner wrote:
> On Thursday 15 August 2002 18:50, Barry Lind wrote:
> >
> > This is implemented and has been since 7.1.
> >
>
> I'm sorry to be such a pain in the neck :-) Created a test class now (file
> attached).
>
> $md5sum pgjdbc2.jar
> f4ed2deaa88e16e79ccfa1c5b1f5ca22  pgjdbc2.jar
> $export CLASSPATH=pgjdbc2.jar:.
> $javac PgTest.java
> $java PgTest
>
> Message in /var/log/postgresql:
> DEBUG:  connection: host=127.0.0.1 user=mb database=mb
> DEBUG:  pq_recvbuf: unexpected EOF on client connection
>
> When I call Connection.close() explicitly the second message does not appear.
>
> Versions:
> postgresql 7.2,
> JDBC driver for 7.2
> Sun JDK 1.4 (build 1.4.0_01-b03)
> Linux 2.4.18
> --
> Matthias Brunner <mb@blumenstrasse.vol.at>
> PGP FP 7862 32B3 3B75 292A F76F  5042 8587 21AB 5B89 D501
> Check out http://blumenstrasse.vol.at/~mb/gpgkey.asc
> ----
>

> import java.sql.Connection;
> import java.sql.DriverManager;
>
> public class PgTest {
>
>
> public static void main(String[] args) {
>
>     try {
>         Class.forName("org.postgresql.Driver").newInstance();
>         Connection con = DriverManager.getConnection(
>             "jdbc:postgresql:mb?user=mb");
>
>         //con.close();
>
>     }
>     catch (Exception e) {
>     e.printStackTrace();
>     }
> }
>
> }
> ----
>

>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html




Re: Calling Connection.close() in Connection.finalize()

From
Matthias Brunner
Date:
On Thursday 15 August 2002 19:25, Dave Cramer wrote:
> Just tried your code with the latest dev driver and I didn't get an
> exception

Hello Dave,

I was using the latest stable driver:
http://jdbc.postgresql.org/download/pgjdbc2.jar
md5sum: f4ed2deaa88e16e79ccfa1c5b1f5ca22

Actually, I don't feel comfortable with having to upgrade to the latest dev
version. It would be nice if you (or someone else) tried to reproduce it with
the stable driver, so that I know for sure whether it is really a bug.

Best regards!
--
Matthias Brunner <mb@blumenstrasse.vol.at>
PGP FP 7862 32B3 3B75 292A F76F  5042 8587 21AB 5B89 D501
Check out http://blumenstrasse.vol.at/~mb/gpgkey.asc


Re: Calling Connection.close() in Connection.finalize()

From
Barry Lind
Date:
Matthias,

That is the behavior I would expect.  If you don't close the connection
you will see this.  The reason here is the JVM doesn't run finalization
when it is exiting.  So you didn't close the connection and the finalize
method is never called because you are just exiting before garbage
collection runs.

--Barry



Matthias Brunner wrote:

>On Thursday 15 August 2002 18:50, Barry Lind wrote:
>
>
>>This is implemented and has been since 7.1.
>>
>>
>>
>
>I'm sorry to be such a pain in the neck :-) Created a test class now (file
>attached).
>
>$md5sum pgjdbc2.jar
>f4ed2deaa88e16e79ccfa1c5b1f5ca22  pgjdbc2.jar
>$export CLASSPATH=pgjdbc2.jar:.
>$javac PgTest.java
>$java PgTest
>
>Message in /var/log/postgresql:
>DEBUG:  connection: host=127.0.0.1 user=mb database=mb
>DEBUG:  pq_recvbuf: unexpected EOF on client connection
>
>When I call Connection.close() explicitly the second message does not appear.
>
>Versions:
>postgresql 7.2,
>JDBC driver for 7.2
>Sun JDK 1.4 (build 1.4.0_01-b03)
>Linux 2.4.18
>
>
>------------------------------------------------------------------------
>
>import java.sql.Connection;
>import java.sql.DriverManager;
>
>public class PgTest {
>
>
>public static void main(String[] args) {
>
>    try {
>        Class.forName("org.postgresql.Driver").newInstance();
>        Connection con = DriverManager.getConnection(
>            "jdbc:postgresql:mb?user=mb");
>
>        //con.close();
>
>    }
>    catch (Exception e) {
>    e.printStackTrace();
>    }
>}
>
>}
>
>



Re: Calling Connection.close() in Connection.finalize()

From
Matthias Brunner
Date:
On Thursday 15 August 2002 19:43, Barry Lind wrote:
> Matthias,
>
> That is the behavior I would expect.  If you don't close the connection
> you will see this.  The reason here is the JVM doesn't run finalization
> when it is exiting.  So you didn't close the connection and the finalize
> method is never called because you are just exiting before garbage
> collection runs.
>

Thanks, you're right.
After executing "con=null;System.gc();" the database connection is closed
properly.
Dumb I was thinking of the finalize method as a C++-like destructor which must
always be called.
Again, thanks for your efforts.

Cheers!
--
Matthias Brunner <mb@blumenstrasse.vol.at>
PGP FP 7862 32B3 3B75 292A F76F  5042 8587 21AB 5B89 D501
Check out http://blumenstrasse.vol.at/~mb/gpgkey.asc