P
Peter Duniho
I am trying to save some data during user shutdown. To this end, I've
added a ShutdownHook to run when the application shuts down
(Runtime.addShutdownHook()), and in the run() method for the Thread
instance added, I use the Preferences class to save data.
What I found is that unless I call Preferences.flush(), the data wasn't
actually saved. On the one hand, this doesn't seem like an unreasonable
requirement. On the other hand, the documentation _explicitly_ states:
Normal termination of the Java Virtual Machine
will not result in the loss of pending updates
-- an explicit flush invocation is not required
upon termination to ensure that pending updates
are made persistent.
Which to me seems to mean that as long as I don't care when the data is
saved, that it _will_ be saved.
Since it's definitely not being saved, this makes me wonder if it's kosher
to save data to the user preferences at all. On the one hand, I did get
it to work by explicitly flushing the preferences node, but on the other
hand the fact that this was necessary at all suggests that maybe I
shouldn't be trying to access the preferences from my shutdown hook in the
first place.
The documentation for Runtime.addShutdownHook() says, with respect to the
shutdown hook itself:
... They should also not rely blindly upon services
that may have registered their own shutdown hooks
and therefore may themselves in the process of
shutting down.
Since the Preferences class is documented as having at least some degree
of asynchronous behavior, it seems possibly (likely?) that it has its own
shutdown hooks, and thus is relevant to that quote.
At the very least, that would explain why in spite of the Preferences
class saying that you don't need to call flush(), that you actually do in
this case. After all, the Preferences class might already believe it's
done writing all pending preferences out by the time my own shutdown hook
tries to write a new one.
But more importantly, I'm concerned that I shouldn't be trying to write
new preferences data at all in my shutdown hook.
Can I safely do so? Is it simply a matter of needing to call flush(),
since I can't rely on the Preferences own shutdown behavior to write the
data? Or am I tempting fate here? And if so, is there somewhere in the
Java API documentation that covers this? I did look, but couldn't find
anything specific to this exact scenario.
Thanks!
Pete
added a ShutdownHook to run when the application shuts down
(Runtime.addShutdownHook()), and in the run() method for the Thread
instance added, I use the Preferences class to save data.
What I found is that unless I call Preferences.flush(), the data wasn't
actually saved. On the one hand, this doesn't seem like an unreasonable
requirement. On the other hand, the documentation _explicitly_ states:
Normal termination of the Java Virtual Machine
will not result in the loss of pending updates
-- an explicit flush invocation is not required
upon termination to ensure that pending updates
are made persistent.
Which to me seems to mean that as long as I don't care when the data is
saved, that it _will_ be saved.
Since it's definitely not being saved, this makes me wonder if it's kosher
to save data to the user preferences at all. On the one hand, I did get
it to work by explicitly flushing the preferences node, but on the other
hand the fact that this was necessary at all suggests that maybe I
shouldn't be trying to access the preferences from my shutdown hook in the
first place.
The documentation for Runtime.addShutdownHook() says, with respect to the
shutdown hook itself:
... They should also not rely blindly upon services
that may have registered their own shutdown hooks
and therefore may themselves in the process of
shutting down.
Since the Preferences class is documented as having at least some degree
of asynchronous behavior, it seems possibly (likely?) that it has its own
shutdown hooks, and thus is relevant to that quote.
At the very least, that would explain why in spite of the Preferences
class saying that you don't need to call flush(), that you actually do in
this case. After all, the Preferences class might already believe it's
done writing all pending preferences out by the time my own shutdown hook
tries to write a new one.
But more importantly, I'm concerned that I shouldn't be trying to write
new preferences data at all in my shutdown hook.
Can I safely do so? Is it simply a matter of needing to call flush(),
since I can't rely on the Preferences own shutdown behavior to write the
data? Or am I tempting fate here? And if so, is there somewhere in the
Java API documentation that covers this? I did look, but couldn't find
anything specific to this exact scenario.
Thanks!
Pete