views:

50

answers:

4

Hi all,

I want to implement a sample in java that reads a configuration from some config file and, based on that, when user interacts with the page the application will store some data on either MySql or Oracle according to the configuration parameters. How can we implement this sample in most efficient and smart way?

A: 
  • Make an interface for data storage, and use that in your application
  • Make an abstract class which implements this interface and implements the functionality which MySQL and Oracle have in common.
  • Make two classes, one for MySQL and one for Oracle which implement the database-specific stuff.
  • In your configuration, specifiy which class to use (MySQL or Oracle database class).
Sjoerd
ok makes sense, any tools or open source libs that can be helpful?
lakhlaniprashant.blogspot.com
A: 

Maybe use something like Hibernate, which abstracts the database away from you.

keuleJ
nhibernate will be an OR/M, do I need something with Hibernate to make the things easier?
lakhlaniprashant.blogspot.com
In Java you use Hibernate, not NHibernate. What's your question exactly? You don't want to use the OR-Mapping features of Hibernate?
keuleJ
Yes I do want to use it, but using hibernate, is it possible to dynamically select the database (mysql, oracle, sqlLite)?
lakhlaniprashant.blogspot.com
I only know that you can change it in the config. But I don't think that you can change the db during runtime. Please take a look at the docs to be shure.
keuleJ
+3  A: 

Indeed using Hibernate or JPA allows you to abstract the database differences away.

With a dependency injection framework like Spring or Guice you can then create 2 service instances which differ only in the persistence manager which is injected.

In this case you can keep almost 100% of the code identical for the 2 databases which guarantees they will not get out of sync over time.

Peter Tillemans
thanks, can you point me to any good example implementation?
lakhlaniprashant.blogspot.com
I think this blog post might be helpful : http://xocoatl.blogspot.com/2008/02/multiple-persistence-units-with-spring.html
Peter Tillemans
A: 

Following up on @Sjoerd's answer:

Any tools or open source libs that can be helpful?

Hibernate or some other JPA implementation is your best bet.

Alternatively, Spring has some JDBC support classes that do some of the work ... if you can figure out which of the various alternatives is a good match to your requirements.

Unfortunately, implementing an application that works against multiple database backends is hard work, no matter how you do it. In my experience, you usually end up with a solution that doesn't perform as well as a solution that is tailored to one and only one database back end.

If I had my way, database vendors who refuse to implement the SQL standard would be first against the wall ... come the revolution.

Stephen C