Discussions

EJB programming & troubleshooting: multiple databases + one j2ee server

  1. multiple databases + one j2ee server (2 messages)

    Does anyone know if it's possible to have an app server dynamically configure itself to use different databases at runtime? I'm using CMP with JBOSS, and the databases I'd like to use for my application are identical in structure except they contain different data.

    thanks.
  2. Sure, I often use a fairly simple factory pattern to "let the system know" what database to use at run time.
    I think that the Pet Store from java.sun.com has a sample of this, but I don't remember, and I know they've changed it since the last time I looked at it.

    You might take a look at pet store though. Some of the stuff in their factory patterns is sometimes a little overcomplicated when your trying to do something relatively simple. It's kind of like swatting a fly with a buick, so you might want to simplify some of it.

    I've also done some of this with JBoss, although only with BMP, but it should work fine for you. You might consider combining the pattern with the minerva connection pool system.

    -Newt
  3. you need to register both data bases as 2 different data sources.. have an environment variable or bind the datasource name you wish to use to a wellknown registry entry... this would allow you to switch data bases.... but if you want to switch databases per request... use the factory pattern as suggested by Jason above