Abe
Abe

Reputation: 9061

Opensource library to save tree of java objects to db or file

I am looking for an opensource library which can be used to transparently save java objects(and their dependencies!) to file or db, a kind of snapshot.

Sample use case: I have the state of a game in a bunch of objects, player, score, location etc. User clicks "save" and now I need to save these objects(all of the objects to be saved can be annotated) to a file or db. If system crashes or user log's in later, I should be able to recover from this point on.

I am looking for a library that can do it as transparently and efficiently as possible. Transaction etc is not necessary. My priorities would be

  1. OpenSource, free, Good documentation
  2. Ease of use. User should not have to manually save the object after something is modified. Of if (s)he has to, it should be trivial activity
  3. Stable
  4. Should be easy to read from snapshot and recover.

Does a library exist that covers all 4 or at least a good portion of the above req's? I am opent to aspect oriented approaches

Am I asking the wrong q for my usecase? Is it done very differently in the industry?

Upvotes: 0

Views: 1488

Answers (3)

Grim
Grim

Reputation: 2040

There is an build-in-functionality called ObjectInput/ObjectOutput-Stream.

http://docs.oracle.com/javase/6/docs/api/java/io/ObjectInputStream.html

If you like to have human-readable file's you may like to use XStream.

http://x-stream.github.io/faq.html

Upvotes: 0

Roger Lindsjö
Roger Lindsjö

Reputation: 11553

You should be able to use the built in serialization of java to solve this. If you want it to save automatically, wrap your model in a layer which saves it for you. This could even be done "automatically" using proxies (java.lang.reflect.Proxy).

However, I usually do not recommend automatically saving state as values often are related. Imagine a progress class which keeps track of which level you are on and how far within that level you have gotten (say 0%, 25%, 50% and 75%).

Now, you are just completing level 2 so the progress has to be updated to "Level 3, 0%". If you first set the level the progress will (for a short period) have the state "Level 3, 75%" which is incorrect, and if you set how far first you will get "Level 2, 0%". Should the program stop at either of those points, then the user will have an incorrect state.

Upvotes: 1

AlexR
AlexR

Reputation: 115398

You can use standard java serialization mechanism. If all your classes that you want to save implement tag interface Serializable you can transparently save as big as you want graph of objects. It is not DB, it is file, but you can always read these objects back to your application and probably it is the best (or the first) option for you.

The disadvantage of this method is that the data is binary, i.e. you cannot read it using Notepad. And format is very sensitive to changes done in classes, so support of backwards/forward compatibility is problematic.

Other possibility is to serialize objects as XML or JSON. For XML try JAXB or XStream. For JSON take a look here.

Upvotes: 1

Related Questions