Announcement Announcement Module
No announcement yet.
multiple DataSources without JTA Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • multiple DataSources without JTA


    We have a situation where the same kind of data is in three different databases. (One user's account data is in one database and another user's data in another.) We have to access this data in a webapp so that each request always involves only one database - no XA needed.

    Now I'm trying to figure out how to make use of Spring here. Basically I have three beans defined that are all of the same type but each gets their own data source. I'm also using Hibernate with lazy initialisation and would like to use it in my view so the transaction/connection should stay open until the view is rendered. Therefore I cannot use declarative TX mgmt around the beans. This means that I should probably wrap everything inside a TransactionTemplate.execute() call.

    What makes things more complicated is that the code that decides which bean (also then which DataSource) to use is inside the transactional block. Therefore I don't know actually when creating the TransactionTemplate which datasource will take part in the transaction. An easy solution here would be JtaTransactionManager but I would like to avoid JTA as I actually use only one data source in one transaction and I would like to run my app inside Jetty without any JTA implementation. DataSourceTransactionManager (and basically HibernateTransactionManager also) needs the DataSource to be set before the TransactionTemplate.execute() call.

    My question actually is now: could anyone advise is it somehow possible to achieve this kind of solution without JTA? I imagine I could write a TransactionManager that is kind-of lazy-init or something like JTA enlisting where the the DataSource (or Connection actually) used will be determined on the first actual call to that DataSource?

  • #2
    Interesting situation :? .

    I suppose that you could write a custom datasource that knows what type of user you have. Based on the user type, your custom data source delegates to the specific datasource 1, or datasource 2, or datasource 3. An example for the implementation: create a servlet filter (that should be the first one, before OpenSessionInView and others) that determines the user type and puts it in some threadlocal storage so that is easily accesible to your custom datasource.

    Regards, Croco


    • #3
      Just noticed this post:
      I think there is already a solution for your problem.

      Regards, Croco
      Last edited by robyn; May 19th, 2006, 05:04 AM.