How to switch the underlying database for entity beans?


EJB design: How to switch the underlying database for entity beans?

  1. I have an application that allows a user to work with multiple databases. However, at one time the application uses only a single database. Each of these databases have exact same schema but the data is different. If I were to use entity beans, how would I accommodate switching of the database in my design, in a portable manner?

  2. Container Managed Persistence or Bean Managed Persistence?

  3. Thanks for pointing out what I missed to mention - yeah I am looking for a portable solution using CMP. BMP would be easier since there wouldn't be any transparent mapping involved anyway.
  4. If the mapping would really be transparent, there wouldn't be any problems in switching the database, right?

  5. Yes, it should be. But the question is how exactly would you do it within pure J2EE framework (i.e. without using any vendor proprietary solution)? As far as I know, the app server is supposed to manage the mapping to a database rather than to simply a logical schema. If I could programmaticaly ask the underlying mapping engine
    to use a datasource that I specify, at any point in time, only then I think I can manage this.

  6. <rant>
    You are hitting the very problem with CMP. CMP by it's very nature is non-portable, vendor-specific, and generally a pain in the ass when you need to do anything non-trivial.

    I am quite sure you cannot change the databse mapping at runtime, though what you might be able to do is map an entity to 2 different JNDI names, and each bean is mapped to different DataSources. This way at runtime whatever you had in miond to switch the datasource would just call lookup the appropriate bean.
  7. I agree about CMP. I have found very little use for it in most of the J2EE projects I worked on. Potential poblems are vendor lock in, not flexible enough for non trivial database queries.

    For BMP, you could take the following approach for database portability

    - Switch DataSource through your App Server configuration
    - Write SQL queries as java properties in text files and have different sets of such properties files for different databases

  8. People. People. Don't say anything bad on any technology that you haven't really used and explored. In Weblogic, you can achieve this without changing anything in your code and in your ejb descriptors.

    When you create your CMP descriptor (weblogic-cmp-rdbms-jar.xml), you specify there the JNDI name of your datasource (<data-source-name>jdbc.lif_Ora_DS</data-source-name>). This datasource can actually be pointed to any database connection pool. If you have multiple databases with the same schema, you can create different pools for each. In our case, we have something like DB_dev_pool, DB_qa_pool, and DB_prod_pool. All you need to do when you want to change your entity bean's datasource (database, that is) is go to the management console and change your datasource's poolname to the one that you want.

    Easy, isn't it? Of course, you'll have to restart your server.