Announcement Announcement Module
No announcement yet.
multi db Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • multi db

    Hi . Im using meaven,jetty and sql. What do i have to if i want my application to be able to save the data in different databases in same time. I mean that a user would be able to choose one of the databases he/she wants to use?
    Thank you.

  • #2
    It pretty much depends on the persistence strategy you're using (JPA, another specific ORM like Hibernate or JDO, MyBatis, Spring JDBC...); in any case, it boils down to defining different data sources in your application server that use the different DBs, configuring their access in Spring, and using them. If distributed transactionality is required, you should also configure and use container-driven JTA.


    • #3
      Hi. Im using hibernate. Do u know any good tutorial for that? Thank you.


      • #4
        I don't know of a specific tutorial, but the official Spring reference documentation helps you a lot. Basically, for Hibernate you will have to configure one different session factory for each database you want to use (2 databases = 2 session factories, and so on).

        You'll need to create 2 or more different data sources inside your application server, each linked to one of the 2 or more dbs. Then, configure the DataSource beans using the jndi lookup in Spring:

        <jee:jndi-lookup id="myDataSource1" jndi-name="java:comp/env/jdbc/myds1"/>
        <jee:jndi-lookup id="myDataSource2" jndi-name="java:comp/env/jdbc/myds2"/>
        ...and so on
        then create 2 or more session factories the standard Spring way and inject one data source bean in each of them.

        You can then design your daos to accept the session factory interface as argument, so that you will be able to choose the implementation (and, by consequence, the underlying db) at runtime.

        As I already stated, if distributed transactionality is required (you have operations that are atomic and need to persist on more than one of your dbs) you should use @Transactional support configuring it to use the application server JTA implementation. This part is also very well covered in the official Spring reference documentation.


        • #5
          Thank you for help