Announcement Announcement Module
No announcement yet.
Switching between multiple jndi data source on-the-fly Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Switching between multiple jndi data source on-the-fly

    Ok, I'm sure this can be done but I don't know how about to do this. I would immagine Spring has some nifty way to do this already that I don't know about which is why I'm posting before I try to code a solution.

    Ok, I have two jndi data sources that "point" to two different databases in this web app. Depending on the context (http://blah.comapp/, I need the app to use a different database.

    Basically our requirements are to have one codebase that switches between the training and production database as well as some UI changes. Based on the URL we know if the user is a production or training user and we need to act accordingly.

    Any ideas on an elegant solution? Worst case is that we deploy two apps that are configured slightly differently (one for prod one for training) but use the same code base -- technically fulfilling our requirements -- in order to get the all important speed to market requirement done.

    We're using Struts MVC -> Spring -> Hibernate

    I'd appreciate any pointers on how to do this. I'm farily new to Spring but it has been a joy to use thus far. TIA.

  • #2
    One way would be to write a HandlerInterceptor to check the URL and configure a request atttribute appropriately.



    • #3
      If you are running under Tomcat - it's possible to bind application-wide JNDI entries to different global JNDI entries, under the same name.


      • #4
        The HandlerInterceptor looks like what I want -- thanks!

        I'm curious about how to implement this though. I know in my applicationContext-hibernate.xml file I'll have to have two entries for my two jndi data sources. Do I then for my session factory (org.springframework.orm.hibernate3.LocalSessionFa ctoryBean) have a reference there for each of my jndi data sources?

        Or would I in my session factory define one jndi datasource and then in my code grab that session factory bean and change the datasource?


        • #5
          I did this on a project a year or so back using JMX. Hiberbate comes with a JMX bean that inits the session factory and places in JNDI. A change datasource method of the JMX bean allowed us to change datasources on the fly. Spring config allowed us to change from a local to jndi datasource for testing vs deployment easily.

          Springs JMX support has been introduced since, and it wouldn't suprise me if Hibernate's support hasn't improved. One problem I do remember was with jar isolation - I had to put hibernate jars into a central place on the app server, which can cause problems if there's multiple applications using different versions of hibernate.


          • #6
            Access multiple datasources and return the result

            I have a similar situation where I use Struts, Spring and Jpa. From the Initial Parameters screen, based on the button clicked, the application has to go to the production data base or archive database or both and bring back the results. I was not sure of how to use the HandlerInterceptor. I have tried to use two different Application Context for these two data sources and made the application work. But I have a feeling that this is not the right way of doing it. The way how it works right now I have the ContextLoaderPlugin load the application context which hands the control over the struts actions to spring framework and it uses the production data base. There is a specific action which is called when the buttons are selected has a static block which loads the other application context with the archive database. Please let me know how we can handle this scenario with one application context and change the datasources on the fly.Thanks for your help in advance.


            • #7
              You can accomplish this entirely in Spring. Take a look at this thread. It basically entails creating multiple session factories. Its similar to concepts from the blog post Dynamic DataSource Routing by Mark Fisher. But its applied to session factories instead of DataSources.