Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Database loading/saving  RSS feed

 
Derek White
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried some things with databases and I was wondering if it is faster to use a HashMap with an XML file or to just serialize the HashMap.

Either way the system runs from constantly accessing this map, I just want to know the fastest way to load on start-up and save on exit.

Thanks much
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Derek,

unfortunately my last (long) post got lost because there was some problem with the connection to JavaRanch

So here once again in short:

I'm not sure if I got you right and I don't know how this is related to databases. But as I understand your problem you use a HashMap as in-memory database and now you want to save it to and load it from disk?!? Hope this is correct...

Java serialization will be more compact and therefore faster because it's a binary data format. To work correctly the whole object graph of all things you put into the map have to be serializable. If this is no problem and you only have Java applications using these data then serialization is an option.

XML on the other hand is more verbose because it's text data. So the disadvantage is clearly that it produces more I/O overhead and therefore it will be slower, in particular if you have lots of data. The advantages are that the data will be human readable and editable. You can use lots of tools and other programming languages to edit or read the data because it's highly interchangeable. To load and save the data there are many APIs and libraries you can use in Java depending on your needs. For this it's difficult to give a good advice without knowing your requirements and some more details about the data and application. Possible ways to use XML in Java that come to mind are the following APIs and libs: SAX, DOM, StAX, JAXB, xstream and others...

Marco
 
Derek White
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marco, your answer was definitely what I was looking for.
I am using an Object[] to store/load entries from a server, and I have little or no need for a human readable format.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Derek, you're welcome ;-)

Unfortunately it seems that things are getting more complicated... We have a database I don't know anything about. And now we even have a server I don't know anything about. And of course an array of Objects in contrast to the HashMap in XML you were talking about a few lines above

This is definitely a little bit confusing. Did my advices help you to find a solution? Or do you have problems or questions at the moment? If you still need some help it would probably be helpful to hear the whole story about your application

Marco
 
Tim Holloway
Bartender
Posts: 18705
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will probably get faster load/store time using serialization than a HashMap. However, you will almost certainly blow up the first time you upgrade Java and the serialized format has changed and become incompatible, as it often does. Serialization is better used for short-term persistence and over-the-wire transmittion, and not always even then.

Depending on the environment, you may actually find it faster and simpler to put that data in a database and use an ORM such as JPA, Hibernate, or even EJB to manage it. Frequently-accessed items will stay in cache transparently, infrequently-accessed items will be left in persistent store where they won't consume RAM.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!