JDBCAdapter & close() method ?  
Author Message
Dado





PostPosted: 2004-9-22 16:36:00 Top

java-programmer, JDBCAdapter & close() method ? Do you know JDBCAdapter from j2sdk TableExample:
JDBCAdapter extends AbstractTableModel {

....

public void close() throws SQLException {
System.out.println("Closing db connection");


resultSet.close();
statement.close();
connection.close();
}
...

}

Who start the close() method on exit of my application???




---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.725 / Virus Database: 480 - Release Date: 19.7.2004


 
IchBin





PostPosted: 2004-9-23 9:45:00 Top

java-programmer >> JDBCAdapter & close() method ? Dado wrote:
> Do you know JDBCAdapter from j2sdk TableExample:
> JDBCAdapter extends AbstractTableModel {
>
> ....
>
> public void close() throws SQLException {
> System.out.println("Closing db connection");
>
>
> resultSet.close();
> statement.close();
> connection.close();
> }
> ...
>
> }
>
> Who start the close() method on exit of my application???
>
>
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.725 / Virus Database: 480 - Release Date: 19.7.2004
>
>
Actually this call the close at clean up time;

protected void finalize() throws Throwable {
close();
super.finalize();
}

finalize() is called by the garbage collector on an object when garbage
collection determines that there are no more references to the object. A
subclass overrides the finalize method to dispose of system resources or
to perform other cleanup.
--


Thanks in Advance...
IchBin
__________________________________________________________________________

'Laughter is inner jogging'
- Norman Cousins, editor and author (1915-1990)
 
Jonck





PostPosted: 2004-9-23 12:13:00 Top

java-programmer >> JDBCAdapter & close() method ? > Actually this call the close at clean up time;
>
> protected void finalize() throws Throwable {
> close();
> super.finalize();
>} }
>
> finalize() is called by the garbage collector on an object when
> garbage collection determines that there are no more references to
> the object. A subclass overrides the finalize method to dispose of
> system resources or to perform other cleanup.

From the documentation on finalize(), which is deprecated btw:
"This method is inherently unsafe. It may result in finalizers being
called on live objects while other threads are concurrently manipulating
those objects, resulting in erratic behavior or deadlock".

Instead use a so-called shutdown hook. To implement a shutdown hook,
here's an example for your situation:

public class ShutDownObject implements Runnable {

public void run() {
try{
close();
} } catch (SQLException e) {
System.out.println(e.getMessage());
} }
} }
}

In the main method of your starting class, put the following code:

Runtime runTime = Runtime.getRuntime();
ShutDownObject hook = new ShutDownObject();
runTime.addShutdownHook(new Thread(hook));


This will ensure that your close() method is called when you shut down
your application.

Hope this helps, Jonck
 
 
IchBin





PostPosted: 2004-9-23 13:03:00 Top

java-programmer >> JDBCAdapter & close() method ? Jonck wrote:
>>Actually this call the close at clean up time;
>>
>> protected void finalize() throws Throwable {
>> close();
>> super.finalize();
>>} }
>>
>>finalize() is called by the garbage collector on an object when
>>garbage collection determines that there are no more references to
>>the object. A subclass overrides the finalize method to dispose of
>>system resources or to perform other cleanup.
>
>
> From the documentation on finalize(), which is deprecated btw:
> "This method is inherently unsafe. It may result in finalizers being
> called on live objects while other threads are concurrently manipulating
> those objects, resulting in erratic behavior or deadlock".
>
> Instead use a so-called shutdown hook. To implement a shutdown hook,
> here's an example for your situation:
>
> public class ShutDownObject implements Runnable {
>
> public void run() {
> try{
> close();
> } } catch (SQLException e) {
> System.out.println(e.getMessage());
> } }
> } }
> }
>
> In the main method of your starting class, put the following code:
>
> Runtime runTime = Runtime.getRuntime();
> ShutDownObject hook = new ShutDownObject();
> runTime.addShutdownHook(new Thread(hook));
>
>
> This will ensure that your close() method is called when you shut down
> your application.
>
> Hope this helps, Jonck

Thanks Jonck..

I was just explaining an answer to the question he had on how the
current Java 2 Platform, Standard Edition, v 1.5.0 Beta 2 JDK table
example worked. He must be new and did not know about the
java.lang.Object.finalize() method because the close was issued indirectly.

Did not know that finalize was deprecated thanks for the info.. Which
bothers me because Eclipse does not flag it as deprecated but does for
say .show(). I'm using 1.5.0.
--


Thanks in Advance...
IchBin
__________________________________________________________________________

'Laughter is inner jogging'
- Norman Cousins, editor and author (1915-1990)
 
 
Dado





PostPosted: 2004-9-23 18:01:00 Top

java-programmer >> JDBCAdapter & close() method ?
"IchBin" <email***@***.com> wrote in message
news:email***@***.com...
> Jonck wrote:
> >>Actually this call the close at clean up time;
> >>
> >> protected void finalize() throws Throwable {
> >> close();
> >> super.finalize();
> >>} }
> >>
> >>finalize() is called by the garbage collector on an object when
> >>garbage collection determines that there are no more references to
> >>the object. A subclass overrides the finalize method to dispose of
> >>system resources or to perform other cleanup.
> >
> >
> > From the documentation on finalize(), which is deprecated btw:
> > "This method is inherently unsafe. It may result in finalizers being
> > called on live objects while other threads are concurrently manipulating
> > those objects, resulting in erratic behavior or deadlock".
> >
> > Instead use a so-called shutdown hook. To implement a shutdown hook,
> > here's an example for your situation:
> >
> > public class ShutDownObject implements Runnable {
> >
> > public void run() {
> > try{
> > close();
> > } } catch (SQLException e) {
> > System.out.println(e.getMessage());
> > } }
> > } }
> > }
> >
> > In the main method of your starting class, put the following code:
> >
> > Runtime runTime = Runtime.getRuntime();
> > ShutDownObject hook = new ShutDownObject();
> > runTime.addShutdownHook(new Thread(hook));
> >
> >
> > This will ensure that your close() method is called when you shut down
> > your application.
> >
> > Hope this helps, Jonck
>
> Thanks Jonck..
>
> I was just explaining an answer to the question he had on how the
> current Java 2 Platform, Standard Edition, v 1.5.0 Beta 2 JDK table
> example worked. He must be new and did not know about the
> java.lang.Object.finalize() method because the close was issued
indirectly.
>
> Did not know that finalize was deprecated thanks for the info.. Which
> bothers me because Eclipse does not flag it as deprecated but does for
> say .show(). I'm using 1.5.0.
> --
>
>
> Thanks in Advance...
> IchBin
> __________________________________________________________________________
>
> 'Laughter is inner jogging'
> - Norman Cousins, editor and author (1915-1990)


I wrote wrong, I didn't saw that finalize() calls close(), but what I still
don't understand does shutdown hook calls automatically on an exit of my
application or I must call it?



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.725 / Virus Database: 480 - Release Date: 19.7.2004


 
 
Jonck





PostPosted: 2004-9-23 20:59:00 Top

java-programmer >> JDBCAdapter & close() method ? > I wrote wrong, I didn't saw that finalize() calls close(), but what I
> still don't understand does shutdown hook calls automatically on an
> exit of my application or I must call it?

When you attach the shutdown hook to your Runtime object, it will be
called automatically when your app shuts down.
 
 
Jonck





PostPosted: 2004-9-23 21:03:00 Top

java-programmer >> JDBCAdapter & close() method ? >
> Did not know that finalize was deprecated thanks for the info.. Which
> bothers me because Eclipse does not flag it as deprecated but does for
> say .show(). I'm using 1.5.0.

Sorry, my mistake, finalize() is not deprecated, System.
runFinalizersOnExit(boolean) is deprecated. And since this will be the
way to get your finalize() methods run, well, you get the point I'm sure.
I've been taught not to use finalize() if I can avoid it, but perhaps
there are situations when you want to use it.

Cheers, Jonck
 
 
IchBin





PostPosted: 2004-9-23 22:11:00 Top

java-programmer >> JDBCAdapter & close() method ? Jonck wrote:
>>Did not know that finalize was deprecated thanks for the info.. Which
>>bothers me because Eclipse does not flag it as deprecated but does for
>>say .show(). I'm using 1.5.0.
>
>
> Sorry, my mistake, finalize() is not deprecated, System.
> runFinalizersOnExit(boolean) is deprecated. And since this will be the
> way to get your finalize() methods run, well, you get the point I'm sure.
> I've been taught not to use finalize() if I can avoid it, but perhaps
> there are situations when you want to use it.
>
> Cheers, Jonck

Looks like this was a learning exercise for both of us... And the
initial poster. Did not know about this "hook"..

You fingers should have a seatbelt on! Take it off before you try to get
out of the car......LOL


--


Thanks in Advance...
IchBin
__________________________________________________________________________

'Laughter is inner jogging'
- Norman Cousins, editor and author (1915-1990)