The road so far….

December 6, 2010

Embedded DB + AbstractRoutingDataSource + Hibernate = Data Router Application

Filed under: java — Tags: , — Rahul Sharma @ 9:40 pm

Spring 3 contains a new feature of embedded databases. You could efficiently use this feature for an application at the start of a developement or for tests concerning db access or for smaller use cases of data storage. Basically using this feature you could start any of three viz. HSQL, Derby, H2. These all create  in memory database instances and you could create schema and import data in them. The EmbededDatabaseBuilder API offered by Spring can be used to create instances of  EmbededDatabase. The EmbededDatabase is of type Datasource and can  be plugged into the SessionFactory. This makes it easier to use these APIs for tests and smaller use cases where you will be required to create the database, perform actions, verify/show results and  just shutdown.

While this is an interesting API offered by Spring 3, I tried to use this API with one of the interesting ones introduced in Spring 2, the AbstractRoutingDatasource API. Using this API we could have two different data bases and then depending on some criteria shuffle our data in different( Homogeneous/Heterogeneous ) databases. This is an abstract class and you have to implement it for the business criteria for shuffling data . Also the offered API is of type Datasource and is pluggable in the SessionFactory. But by doing this we need standards that will remain same across different databases i.e. SQL,  as the goal is to have an  unaware access to all the databases. So using SpringJdbc looked quite promising and thus accomplished the task. (more…)