Any application shutdown hooks?

2011-05-25 Thread Matthew Ong

Hi,

If I am writing a database library such as DSL, Data Service Layer, 
there is a need for me to do a good clean up for the connections/results 
sets/transactions that this library is currently handling.


How ever, if another library/package somewhere else calls for a


java.lang.Runtime#public void addShutdownHook(Thread hook)


How do code or what library to use?

can someone please provide a simple working D example for try out.

Of cause, in Even in Java there is event if some cleaning lady were to 
accidentally unplug the main socket of your server. :)



--
Matthew Ong
email: on...@yahoo.com



Re: Any application shutdown hooks?

2011-05-25 Thread Steven Schveighoffer

http://www.digitalmars.com/d/2.0/class.html#StaticDestructor

-Steve


Re: Any application shutdown hooks?

2011-05-25 Thread Matthew Ong

On 5/25/2011 11:13 PM, Steven Schveighoffer wrote:

http://www.digitalmars.com/d/2.0/class.html#StaticDestructor

-Steve


Static Destructor maybe executed when the class is being unloaded GC?
Because this is a class level. It is more like the application level 
exit and such.


That is not really the same as:
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29


This is triggered when:
# The program exits normally, when the last non-daemon thread exits or 
when the exit (equivalently, System.exit) method is invoked, or


# The virtual machine is terminated in response to a user interrupt, 
such as typing ^C, or a system-wide event, such as user logoff or system 
shutdown.




--
Matthew Ong
email: on...@yahoo.com



Re: Any application shutdown hooks?

2011-05-25 Thread Ali Çehreli

On 05/25/2011 08:35 AM, Matthew Ong wrote:

On 5/25/2011 11:13 PM, Steven Schveighoffer wrote:

http://www.digitalmars.com/d/2.0/class.html#StaticDestructor

-Steve


Static Destructor maybe executed when the class is being unloaded GC?
Because this is a class level. It is more like the application level
exit and such.


There is a problem at Digital Mars's site. Steve must have meant the 
module static destructor. Search for Static Construction and 
Destruction on the module page:


  http://www.digitalmars.com/d/2.0/module.html

(Do not click StaticDestructor at the top of the page. That is linked to 
the class page.)


Ali


Re: Any application shutdown hooks?

2011-05-25 Thread Steven Schveighoffer

On Wed, 25 May 2011 11:35:51 -0400, Matthew Ong on...@yahoo.com wrote:


On 5/25/2011 11:13 PM, Steven Schveighoffer wrote:

http://www.digitalmars.com/d/2.0/class.html#StaticDestructor

-Steve


Static Destructor maybe executed when the class is being unloaded GC?
Because this is a class level. It is more like the application level  
exit and such.


This is *exactly* what static destructors do.

A static destructor gets called on program termination

Actually, that description is inaccurate for non-shared static dtors,  
someone should fix that.



That is not really the same as:
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29


If you want a static dtor that runs on every thread shutdown, use a normal  
static dtor.  If you want one that runs on the entire application  
shutdown, use a shared static dtor.


-Steve


Re: Any application shutdown hooks?

2011-05-25 Thread Steven Schveighoffer

On Wed, 25 May 2011 11:50:16 -0400, Ali Çehreli acehr...@yahoo.com wrote:


On 05/25/2011 08:35 AM, Matthew Ong wrote:

On 5/25/2011 11:13 PM, Steven Schveighoffer wrote:

http://www.digitalmars.com/d/2.0/class.html#StaticDestructor

-Steve


Static Destructor maybe executed when the class is being unloaded GC?
Because this is a class level. It is more like the application level
exit and such.


There is a problem at Digital Mars's site. Steve must have meant the  
module static destructor. Search for Static Construction and  
Destruction on the module page:


   http://www.digitalmars.com/d/2.0/module.html

(Do not click StaticDestructor at the top of the page. That is linked to  
the class page.)


Oh, yeah, that's exactly what I did!  hm...

Need to file a bug. (filed as  
http://d.puremagic.com/issues/show_bug.cgi?id=6055 )


Note that class static dtors are exactly the same as module static dtors,  
they just are inside the class namespace.


-Steve


Re: Any application shutdown hooks?

2011-05-25 Thread Matthew Ong

On 5/25/2011 11:52 PM, Steven Schveighoffer wrote:


If you want a static dtor that runs on every thread shutdown, use a
normal static dtor. If you want one that runs on the entire application
shutdown, use a shared static dtor.

-Steve
Module Destructor: Cool feature that I can consider to use for other 
purpose.


If you want one that runs on the entire application shutdown, use a 
shared static dtor.
It is more like an entire application shutdown event. So I will be 
placing that at the same module dtor and file as the main(string[] args)

using template mixin.

That might work. I will test that out.

--
Matthew Ong
email: on...@yahoo.com



Re: Any application shutdown hooks?

2011-05-25 Thread Steven Schveighoffer
On Wed, 25 May 2011 11:52:14 -0400, Steven Schveighoffer  
schvei...@yahoo.com wrote:



On Wed, 25 May 2011 11:35:51 -0400, Matthew Ong on...@yahoo.com wrote:


On 5/25/2011 11:13 PM, Steven Schveighoffer wrote:

http://www.digitalmars.com/d/2.0/class.html#StaticDestructor

-Steve


Static Destructor maybe executed when the class is being unloaded GC?
Because this is a class level. It is more like the application level  
exit and such.


This is *exactly* what static destructors do.

A static destructor gets called on program termination

Actually, that description is inaccurate for non-shared static dtors,  
someone should fix that.



That is not really the same as:
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29


If you want a static dtor that runs on every thread shutdown, use a  
normal static dtor.  If you want one that runs on the entire application  
shutdown, use a shared static dtor.


I should clarify that when I wrote this, I thought the description was  
from the module.html file, not the class.html file.  Ali pointed out the  
bug in the web site.  So I didn't get why you were talking about classes,  
sorry :)


Class and module static ctor/dtors should be exactly what you are looking  
for.


-Steve


Re: Any application shutdown hooks?

2011-05-25 Thread Andrej Mitrovic
On 5/25/11, Steven Schveighoffer schvei...@yahoo.com wrote:
 If you want a static dtor that runs on every thread shutdown, use a normal
 static dtor.  If you want one that runs on the entire application
 shutdown, use a shared static dtor.

I saw some commits change a static dtor to a shared static dtor and
wondered what was that all about..


Re: Any application shutdown hooks?

2011-05-25 Thread Matthew Ong

On 5/26/2011 12:23 AM, Andrej Mitrovic wrote:

On 5/25/11, Steven Schveighofferschvei...@yahoo.com  wrote:

If you want a static dtor that runs on every thread shutdown, use a normal
static dtor.  If you want one that runs on the entire application
shutdown, use a shared static dtor.


I saw some commits change a static dtor to a shared static dtor and
wondered what was that all about..

Thanks a lot for the all the comments and instructions shown here.

--
Matthew Ong
email: on...@yahoo.com