• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Handling shutdown scenarios for writing to XML  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have my java application running in Windows machine. During login, a specific xml is read and it is updated with certain information for the next login details.

When this is happening, if somebody else performs a hard shut down like mechanically pressing the power off button, xml becomes blank on next login and could not able to perform the previously said updating of information

What solution can be performed for this ?

PS: Cannot use shutdown hooks as this scenario will not call the registered thread.

Please help.
 
Marshal
Posts: 63454
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think a shutdown hook might take too long to execute. Why is the file blank if the program doesn't shut down normally? Why aren't you reverting to the previous login details? Why are you writing the ile at shutdown time rather than immediately after starting?
 
Usha Viswanathan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

I think a shutdown hook might take too long to execute. Why is the file blank if the program doesn't shut down normally? Why aren't you reverting to the previous login details? Why are you writing the ile at shutdown time rather than immediately after starting?



Hi,

There might be cases like when user performs a hard shutdown , example directly pressing the power off button. In that case jvm also shuts down abruptly right ? During the write process of xml file, when this shutdown happens, the xml file becomes blank
 
Usha Viswanathan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi,

XML is read initially to capture the last login details.

Even I have run a sample program dealing with File. That also gets blanked out if I start my program and terminate it immediately.

Please help me with some solution on how to deal with this
 
Marshal
Posts: 6594
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Usha Viswanathan wrote:During the write process of xml file, when this shutdown happens, the xml file becomes blank


Cambell was asking, but you didn't explain yet, why and when file gets blank in the first place if the shutdown went abruptly?

Please explain, when file gets written with content and when the content gets deleted in normal conditions?
 
Rancher
Posts: 1930
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be better if you could please show us the code i.e. how you are dealing with the file.
 
Usha Viswanathan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Usha Viswanathan wrote:During the write process of xml file, when this shutdown happens, the xml file becomes blank


Cambell was asking, but you didn't explain yet, why and when file gets blank in the first place if the shutdown went abruptly?

Please explain, when file gets written with content and when the content gets deleted in normal conditions?




During the login of the application, xml file is written with some information like time in which the user is logged in. This information is used in the next login, where I show a prompt of last login time.

Similar to updating time, there are other writes performed on the XML.

Sometimes, if the Windows gets hung, user is performing hard shutdown and at this time, the data gets erased away.
 
Usha Viswanathan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only during abrupt shutdown, contents gets erased.

During a normal shutdown, this doesnt happen.
 
Liutauras Vilda
Marshal
Posts: 6594
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Usha Viswanathan wrote:Sometimes, if the Windows gets hung, user is performing hard shutdown and at this time, the data gets erased away.


Perhaps you are sort of writing file for an entire app-use lifetime and close file if ever just upon logout.

Why you don't use database to persist such information?
 
Usha Viswanathan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Usha Viswanathan wrote:Sometimes, if the Windows gets hung, user is performing hard shutdown and at this time, the data gets erased away.


Perhaps you are sort of writing file for an entire app-use lifetime and close file if ever just upon logout.

Why you don't use database to persist such information?



Will try doing that. Is there any other way like writing to tmp files will work out for this issue ?
 
Liutauras Vilda
Marshal
Posts: 6594
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As mentioned, if you open file upon login and only close (if at all) upon logout, the problem arise when program quits abruptly, that means you don't close file at all or you don't write at all depending how you programmed. Well, it has been mentioned earlier, we don't know what you are doing since we never saw the code.

You still can try such way:
1. User logs in -> open file -> write login info -> close file
2. User does something else -> open file -> append some extra info -> close file
3. ...
4. IF program quits abruptly, you still have the file with some content. Now whether it is enough for your next login or no - I don't know.
 
Usha Viswanathan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:As mentioned, if you open file upon login and only close (if at all) upon logout, the problem arise when program quits abruptly, that means you don't close file at all or you don't write at all depending how you programmed. Well, it has been mentioned earlier, we don't know what you are doing since we never saw the code.

You still can try such way:
1. User logs in -> open file -> write login info -> close file
2. User does something else -> open file -> append some extra info -> close file
3. ...
4. IF program quits abruptly, you still have the file with some content. Now whether it is enough for your next login or no - I don't know.




In the 4th point, if jvm gets crashed, even if the xml is half written, the contents gets erased fully and I am unable to get the content for the next login. Thats the problem I face.

Will try saving the contents in DB.
 
Swastik Dey
Rancher
Posts: 1930
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DB is always a better option, and preferably with proper transaction management.
 
Usha Viswanathan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:DB is always a better option, and preferably with proper transaction management.




Ya, will try that. Let me know if there is any way to handle shutdown gracefully while writing to XML
 
Swastik Dey
Rancher
Posts: 1930
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us the code
 
Saloon Keeper
Posts: 20510
115
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XML is not a good file format for situations where writing the file can be interrupted, such as a sudden system halt. I didn't use the term "shutdown", because shutdown is an orderly process and in such cases you get at least a little advance warning - although that warning may not get propagated to all applications. Even with a command-initiated shutdown, some processes may simply get halted in mid-stream. You don't get even that when the power goes out. Things just stop. Buffers don't get pushed out to disk. Even the disk drive's onboard cache may not write before losing data.

Since XML is a form that has paired element/end-element tags, an interruption while writing an XML file can result in the end tag(s) not being written, resulting in an invalid XML file. Many XML parsers validate the entire XML file before processing, which can cause the whole thing to get rejected instead of processed.

A simple text log would actually be better. Preferably one that flushes log messages to disk after each write. Or, as others have mentioned, logging to a database. Databases were explicitly designed to deal with possible interruptions not only in the process of writing to them, but in their own internal files. You might lose a login event or two, but at least the overall data would be intact.

 
I want my playground back. Here, I'll give you this tiny ad for it:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!