• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Server shutdown

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am nearly coming to the end, i hope. I have been searching some posts on ways to handle server shutdown.

The two main options i have a come up with are either to use the Runtime.addShutdownHook or use the finalize() method.

i don't have a server gui. after the initial start up it reverts back to the console.

Now to do a clean exit i think the only thing i need to go is close the database file,

What i am not sure is do i need to write the finalize in the data class or the server class, my main concern is i am not sure what i need to clean up, because the booking record is done in one atomic step, so i am not really sure what i need to clean up when the server is shutdown

server shutdown

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the link you had practically answers your question.

Where to close the database file? First for local mode did you close your file? If so, reuse that in your server.
 
jesal dosa
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi K Tsang,

I was'nt closing the file in the local mode. So I assume if I just have a finally() method in the data class which closes the file, like you mention in the previous post that will solve all my problems.
 
jesal dosa
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
server shutdown

the following link suggests i can get away without actually worrying about server shutdown but I think i will implement the database file shutdown in a finally() method but not worry about anything else
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jesal dosa wrote:So I assume if I just have a finally() method in the data class which closes the file



I guess you mean finalize-method instead of finally. And be aware of (from the thread mentioned in your first post):

Nicholas Cheung wrote:
In addition, you may override the protected function finalize() in order to release any resources held by the system.
But, whether the resource is released is NOT assured.



Kind regards,
Roel
 
jesal dosa
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all, I hope some one can help. I have added a shutdown hook like suggested in my Data class, what this does is close the RandomAccess file and make it point to null.

The things that i am still confused about is my update and delete methods in my database are synchronized. So what would happen if the database closes the file during an update option.

This may seem like a dumb question but if the update method is synchronized i assume it will not close the file. i am at a point were i am so close to finishing i don't want to make any mistakes.

Also since the booking is done in lock book unlock method even if it is unsuccessful it should be handled correctly from the client prospective.

Is what i have specified in the shutdown hook in enough or am i missing something important


 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using the shutdown hook in your server side right? Like in your server code calls Data class close method say.

I think if the server is about to shut down and if there are pending writes then the writes will finish because synchronized before doing your close method.

To really make sure it depends on how you implement your locking. For example, if you used a collection to track locked records, you can check this collection first before closing.

Just curious are you using a service layer in between your Data class and client?
 
jesal dosa
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes I am using a service layer its only got two method book and search.

The booking does
database.lock
database.update
database.unlock its in a finally block
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then how can you access the Data random access file variable say to close the file??

Because I'm thinking about this too. Doesn't the service layer also need to have a "close" method or something?
 
jesal dosa
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the Data class i have implemented the shutdown hook


when the data object is created in the constructor it creates a

thread object which creates inner class using the Runnable interface this has access to the data random access file. I create my clean up code in the run method and then i use



when ever the application is going to shutdown, this code will run
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have similar code as you:
- just used the empty Thread constructor
- i added this shutdownhook in the constructor of my business service (so i have an extra method in my dao-interface).
 
jesal dosa
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the moment in my locking code I just have recNo which are mapped to threads. i dont have any -1 which locks the whole database, which some people seem to have implemented. I am still not sure how to confirm there are no writes happening before the data file is closed. Do I even really have to worry about this or can i get away with it when I call the shutdownhook?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesal,

I guess when this method is called (thread is executed) it's one of the last things that happens before the JVM exits. So let's say you could determine if there are still any locked records, how are you going to prevent the JVM from exiting?
 
jesal dosa
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,

Thats the big money question.

I dont care about locked records, if something goes wrong the client will get a message with error connecting to server. All i am worried about is any writes that are talking place when this shutdownhook is called.

I could have something like a flag, Before a write method like update, delete set the flag to true and the change it back to false once its finished. When the hook is called check the flag if its false else waits for bit before and then shut down.

Is this design acceptable?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe i think this is a bit beyond the scope of this project. i would not do anything about it, but make an entry about it in your choices.txt (indicating you have thought about it).

in my approach i have a seperate method to do cleanup and each of my method's from data are synchronized. so the cleanup-method could only take place after or before write but not during write. and the shutdown is added when a business service layer is created, but even if i added it when the Data is constructed, it should not close during a write
 
jesal dosa
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,

I have seen a post where people have skipped this and you have cemented this idea to let be and let go of it. Thanks for the advice. Ill leave it has is.

Thanks
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can see this can go forever. In my design, I have a clientCount, I would simply check this variable during/before shutdown. If there are connected clients, server won't shutdown. Thats my idea because as long as there are connected clients, I assume they may be doing read/writes.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:In my design, I have a clientCount, I would simply check this variable during/before shutdown.



- how are you counting the number of clients?
- If you work with the addShutdownHook how are you preventing the VM from shutting down? because when you enter the thread that should close your file (and there you do the check to see if your clientCount > 0) your server will terminate nonetheless. i think there is no way to prevent your server from shutting down if you are using the shutdownHook.
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roel the client is pretty simple. In my remote interface I declared 2 methods connect and disconnect to print the client IP connected/disconnected on some date at some time when clients instantiate the Data class or service.

This is why I'm not sure how Jesal is able to directly close the RandomAccessFile in the Data class without some sort of method in the service layer.

Actually what you described - checking the client count > 0 - in the shutdown hook thing ... I have tried that once. That try looked to me it hanged. Of course I may have added the hook incorrectly.

Now since I got everything working like I wanted it I can figure this shutdown thing in depth. and write docs
 
jesal dosa
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi K.Tsang,

The client only ever get a copy of the Remote Service implementation. The data class only exists on the server.

I only expose the book and search methods. So when ever the VM is going to shut-down it calls the clean up code through the a thread which is run through the implementation of Runnable interface which is created as inner class in the Data class. So its got access to the Random Access file and therefore it can close it.

When the Ctrl -c or system.exit(0) is called then the shutdown hook code is called.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:the client is pretty simple. In my remote interface I declared 2 methods connect and disconnect to print the client IP connected/disconnected on some date at some time when clients instantiate the Data class or service.


With those 2 extra methods it's indeed very easy

K. Tsang wrote:This is why I'm not sure how Jesal is able to directly close the RandomAccessFile in the Data class without some sort of method in the service layer.


he adds the shutdown hook in the constructor of the data-class and not in the service according to this quote: "In the Data class i have implemented the shutdown hook. when the data object is created in the constructor it creates a thread object which creates inner class using the Runnable interface this has access to the data random access file."

i work with a record cache, so it's really important that shutdown hook works, because else all modification on the records are gone . but i have not to worry about clients maybe accessing the file when server is shut down because that's impossible.
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. I confirmed that if I do the client count check in my shutdown hook thread the server will shut down anyway. So I did the count check in my start server method and if count=0 add the hook. This behavior will ignore Ctrl+C so long the count != 0. But now I want to pop up error if count != 0 base on Ctrl+C. Can I do that .. like check key input for Ctrl+C and not shutdown hook.

After some testing and googling, Ctrl+C combo can't be detected programmatically - it's reserved - shutdown hook works.

Now if I new a window inside the hook i get a

Exception in thread "AWT-Windows" java.lang.IllegalStateException: Shutdown in progress
at java.lang.ApplicationShutdownHooks.add(Unknown Source)
at java.lang.Runtime.addShutdownHook(Unknown Source)
at sun.awt.windows.WToolkit.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)



If I just System.out or System.err it shuts down anyway.

Ok after more playing, I forget using the pop up window idea. Since i have a client count thing, I use that idea and pop up window a status about server is ABOUT to shutdown. Yes I sleep it. This should be enough because I can't prevent the server from shutting down one Ctrl+C is triggered.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I see people say they implement the finalize() method to close down. This is wrong. The finalize method is not guaranteed to be executed, so you can´t rely on it for neat shut-down. The finalize method is executed when an object is garbage collected.

The other thing, Roel asks the question a number of times: How do you prevent the server from shutting down if there are still clients connected? (Especially clients halfway writing a record would be dangerous.) I do this by making the server obtain the lock on the database. Clients need this lock to be able to write or update a record, so if the server has it, no client can be writing.

I'm surprised to see people saying this is "somewhat beyond" the requirements, but there are experts out there who agree (Andrew Monkhouse). So... Well, I've done it already.

Cheers-
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

You are the first one giving a solid solution for the problem of server shutdown and how prevent a client from writing at the same moment to the file. I'll think this answer will be referenced a lot from other threads asking a similar question.

I used a similar approach like you (although mine is a nice benefit of the approach/design i chose)

Regards,
Roel
 
Paul Balm
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!

Note by the way, that the functionality I suggested (server obtains DB lock, etc.) can not be in a JVM shutdown hook. When you enter the server shutdown hook, all threads have already received an InterruptedException (I think -- might also be a ThreadDeath or something). If one of the threads was halfway writing a record, your data will be corrupted.

Therefore, what I think is needed (but again, experts say you can also consider this beyond the requirements), is to have a shutdown mechanism that does not rely on Ctrl-C. If someon Ctrl-Cs your server, your threads will quit, whatever they're doing, so this can corrupt your data always. You can only shut down cleanly from regular code (not JVM shutdown hook), because what you need to do is to allow the writing threads to finish normally. You need some cue from the user telling you that the server must shutdown other than Ctrl-C: I don't have a server GUI, so my server prints: "Hit enter to exit." If you have a GUI, the exit button (or similar) can terminate neatly.

Cheers-

CORRECTION Paul Balm: This is incorrect, as I have now found out. Shutdown hooks run concurrently with still ongoing threads, so if they try to obtain a lock that is held by one of the threads, the JVM will not exit until the thread has released it. See my post later in this thread on 04/05/2009 04:09:14 PM.
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Balm wrote:Note by the way, that the functionality I suggested (server obtains DB lock, etc.) can not be in a JVM shutdown hook. When you enter the server shutdown hook, all threads have already received an InterruptedException (I think -- might also be a ThreadDeath or something). If one of the threads was halfway writing a record, your data will be corrupted.



I agree with Paul on the lock bit. Yet I doubt the InterruptedException part. Because the write operation should take ms to complete. The possible chance of getting IE is if there are many many clients connected all doing write operations. And during this very instance the server shutdown.

Given Paul's idea, I orginally prolonged the uptime using Thread.sleep(). Now I added a flag to so-called lock or prevent write once the shutdown is activated.
 
Paul Balm
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi K,

The part about threads being interrupted halfway a write-operation is the point of doing a neat shutdown though. If you don't think that is likely to happen, why do you need to be careful with the shutdown of your server?

Cheers-
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I finished my RMI implementation this weekend, so now i can also do some tests with the CTRL+C for shutting down the server and how the program reacts on this.

This is what i did to test:
- in update-method i write a blank for each field, except for customer i write customer-id. this approach should allow me to see if write was abruptly ended or not
- in update-method i added a sleep (to allow me to CTRL+C my server) between each write of a field to the record (in my record cache). The interrupted exception is catched and i just print message to sysout.
- start my server
- start a bunch of threads (each representing a client)
- when my server enters my update-method i hit at a given moment CTRL+C
- in the server console i can't see anything indicating an interrupted exception occured and it takes more time for my server to enter the shutdownhook (when i do this at the end of program, the shutdownhook is invoked a lot faster)

i tried this 2-3 times, always the same result. so it seems when CTRL+C is invoked the server finishes first the update-method he was executing before invoking shutdownhook). to execute shutdownhook thread has to obtain lock on the data-instance (when CTRL+C is invoked the lock on data-instance isn't available (because it's needed to execute update-method). when update-method then finishes, lock is available and shutdownhook is invoked.
when i look at my db-file all records are as expected and no half records are encountered (each record has blank fields except field customer or populated fields with empty field customer)

Maybe i was just lucky those times i tested, but it seems to be handled ok (with my design: data = singleton with all methods synchronized)

Kind regards,
Roel
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Balm wrote:Hi K,

The part about threads being interrupted halfway a write-operation is the point of doing a neat shutdown though. If you don't think that is likely to happen, why do you need to be careful with the shutdown of your server?

Cheers-



Nice question. In fact I'm not going to answer it. I'll let you know if I get a similar question in my essay exam then I will answer your question. ... Of course I do have an answer/explanation.
 
Paul Balm
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K: I hope you get that question then. :-)

Roel: That's an interesting test. Your results contradict what I was saying of course... Unless your implementation is different from what I understood in some important way. I'll do the same test with my application and I'll post the results here.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Balm wrote:Your results contradict what I was saying of course...


That's why i posted my test results

Paul Balm wrote:Unless your implementation is different from what I understood in some important way.


I chose the most simple approach that's possible (in my opinion): 1 data instance (singleton) where every method is synchronized and in the shutdownhook i'm calling one of these synchronized methods. so i don't think you can misunderstand something important.

Paul Balm wrote:I'll do the same test with my application and I'll post the results here.


Curious to see your test results.
 
Paul Balm
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,

I've found the time to do some tests, basically the same as what you were doing:
- I run the server
- I run an application that connects to it using RMI, which is continuously creating new records
- After a few records (the server prints a line every time a new record is created) I Ctrl-C the server

I find that probably 3 out of 4 times (I've tried often), there is an incomplete record at the end of the file. The most commonly occurring corruption is that the last field of the record is missing, for some strange reason, or maybe just coincidence. But I also see cases where just the first part of the name (the first field) of a record was written.

If I have an incomplete record at the end of my file, and I restart the server and add some more records, then the incomplete record is overwritten. Experimental fact, I didn´t know that that was what it was doing. Guess I´m going to have to study for the exam.

To look at what´s happening I just do a "tail" of my data-file, as if it were a text-file. It works well enough to be able to see what´s going on. But I guess you have to be on Linux or find a Windows implementation of tail (I used to have one, back in the days, so there is a version out there).

I don't understand why we seem to be getting different results, but my results are consistent with my understanding of how things work... (See my earlier posts on this thread.)

I would appreciate your opinion.
Cheers-

Paul
 
Paul Balm
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,

Here's the answer to the contradiction: My understanding is wrong.
I thought that when you type Ctrl-C, all threads are killed, therefore if you do that halfway writing a record, the writing will be aborted and your data corrupted.

However, it appears that "the JVM makes no guarantees on the order in which shutdown hooks are started. If any application threads (daemon or nondaemon) are still running at shutdown time, they continue to run concurrently with the shutdown process." So if your shutdown hook acquires the lock before finishing, the JVM will not exit until it has it. That is, until write operations are finished.

The conclusion is then that, totally opposite to what I was saying before, it is actually better to have your shutdown code in a shutdown hook. I don't (yet!) have it in a shutdown hook, and the result is data corruption when the server is killed abruptly.

This is of course also why I see data corruption and you don't: The difference between our implementations is that you have shut-down code in the shutdown hook. Well done!

Cheers-
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

I'm glad you sorted out your misunderstanding.

In my theory the shutdownhook just was the only option i could go for (and using a cache the shutdown hook has to be invoked of all work will be gone if the server is exited), so quiet important. But because you made me doubt about that approach i started testing how it would behave when shutting it down in the middle of an operation. And luckily for me it behaved like i planned so no need to change that approach and i can continue with the last part of the assignment: the gui.

But it was certainly a nice discussion about the shutdown of the server
Good luck with your assignment,
Kind regards,
Roel
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic