Announcement Announcement Module
No announcement yet.
Help required on database Transaction Management Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help required on database Transaction Management

    hi Guys,

    This is 1st time i am handling transaction management and i am in SOUP.
    My project consists of spring 3 and hibernate 3. We create Sessionfactory dynamically depending on request attribute(We use multiple databases).
    We then perform various db operations on it.

    I want to do transaction management for this operations. can you guys tell me how to do it.
    There is no linking of hibernate and spring because we create sessionfactory on fly.

    I tried looking for options but it seems we require database source or transactionmanager. I cannot supply db source since we can only determine them onfly. I donno about transactionManager and it seems to me even he needs datasource.

    Any help will be highly appreciated cos its now 3 days since i m working over it

    Thanks in Advance
    Patil Abhijeet
    Last edited by abhi patil; Jun 7th, 2011, 11:44 PM.

  • #2
    If you want spring to handle transactions you will have to have a link between hibernate & spring. Spring will have to create hibernate session factory. In case you have transactions spawning multiple database you will need to configure spring to use JTA. I suggest going through the section on transaction managment provided in Spring documentation.


    • #3
      I did go and tried to implement JTA. But i dont know how to specify transactionManager for it. Since I cannot use hibernate in assiciation with spring
      Just to be clear from my side. My hibernate is running independent of spring i.e i create session factory using configuration object
      any thing that i can do


      • #4
        Just one question why make it so complex...

        You don't have to construct sessionfactories on the fly (neither is that something you want to do from a performance point of view because it is slow). I assume that you only need to switch the database they talk to based on the request parameter, assuming here that the database is the same and the same entities are used.

        Basically this is a description of what we did for an application which served 50 different databases, still works in production today (with probably even more databases being supported). The code (although it evolved since the writing of the blog) can be found here.

        Basically what it does is it uses a single sessionfactory and dynamically selects the datasource to use based on a parameter.


        • #5
          Or you can use something like this :


          • #6
            The drawback with that approach is that it only works for pre-registered datasources, the approach I pointed out can also work with dynamicly created (or recently added jndi) datasources.


            • #7
              But you always can extends the source to your own need...


              • #8
                Which again brings the drawback that the AbstractRoutingDataSource wasn't really designed for that and if you want to have to dynamic behavior you need to almost rewrite the whole class (as a lot of things are private which you would need to override).