Results 1 to 7 of 7

Thread: Switching between multiple jndi data source on-the-fly

  1. #1
    Join Date
    Jun 2005
    Location
    Saint Louis, Missouri
    Posts
    45

    Default 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/, http:blah.com/app/test/) 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. #2
    Join Date
    Aug 2004
    Location
    Carlisle, UK
    Posts
    184

    Default

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

    HTH
    Chris Harris
    Carlisle, UK

  3. #3
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    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. #4
    Join Date
    Jun 2005
    Location
    Saint Louis, Missouri
    Posts
    45

    Default

    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. #5
    Join Date
    Aug 2004
    Location
    San Francisco
    Posts
    423

    Default

    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. #6
    Join Date
    Jun 2008
    Posts
    1

    Default 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. #7
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    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.
    Bill

Similar Threads

  1. JBoss 3.2.6 & Spring Deployment issues
    By difranr in forum Container
    Replies: 2
    Last Post: Sep 18th, 2005, 10:08 PM
  2. Help: the boolean property exception!
    By linwei in forum Swing
    Replies: 2
    Last Post: Nov 17th, 2004, 07:11 PM
  3. listener tag exception
    By hyuan in forum Web
    Replies: 3
    Last Post: Nov 17th, 2004, 03:53 PM
  4. Replies: 6
    Last Post: Nov 8th, 2004, 06:43 AM
  5. NameNotFoundException: ejb not bound
    By alesj in forum EJB
    Replies: 2
    Last Post: Oct 19th, 2004, 11:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •