4 Replies Latest reply on Jan 6, 2011 3:50 AM by Matej Lazar

    Dynamic Datasource invocation at runtime in JSF-EJB3-MySql a

    Pragun Mehta Newbie

      This issue is regarding Dynamic Datasource invocation at runtime in JSF-EJB3-MySql application on Eclipse3.4.1-JBoss5.0.0 plateform.

      I have created a sample JSF-EJB3-MySql application on JBoss5.0.0 AS. Application works fine which performs CRUD operation in database schema "schema1" on table "table1".

      my JBoss5.0.0 server datasource file C:\jboss5.0.0\server\default\deploy\mysql-ds.xml looks like


      my sample application has persistence.xml which looks like
      <persistence-unit name="pmtest">


      Primary requirement for my new application is as below,
      (1) Use JBoss Seam Framework to develop JSF-EJB3-MySql application on Eclipse3.4.1-JBoss5.0.0
      (2) There are multiple databases (having exactly same table design structure)
      running on single database server "host1" placed in a central "Head Office" location i.e.

      All the schemas listed above have has different JNDI name defined in mysql-ds.xml (multiple datasource configuration in one mysql-ds.xm file). As "table1" design structure is same in all the schema, the table1.java entity class of the application can be common.

      (3) The application will be deployed on one application server machine placed at the same central "Head Office" location where the database server "host1" is placed. This single application will access multiple databases running on database server "host1". This application will be called by various "Branch Office" from any other location say "Branch1", "Branch2" etc.

      So, is there any way in JBoss Seam framework using which, the application can identify which datasource to be invoked on the basis of "Branch User" login information ? i.e.
      Branch1 user --> invoke Schema1 datasource
      Branch2 user --> invoke Schema2 datasource

      I came across an article by Spring that provides the features as explained in http://blog.springsource.com/2007/01/23/dynamic-datasource-routing/

      So, can we implement the Dynamic Datasource invocation logic in JBoss Seam using JSF-EJB3-MySql application on Eclipse3.4.1-JBoss5.0.0 plateform ?

      Thanks for your help in advance.

        • 1. Re: Dynamic Datasource invocation at runtime in JSF-EJB3-MyS
          Matej Lazar Newbie

          I'm also looking for solution, how to use multiple databases (having exactly same table design structure).

          Have you found any solution for this ?

          • 2. Re: Dynamic Datasource invocation at runtime in JSF-EJB3-MyS
            Pragun Mehta Newbie

            I manage to solve it but need to fine-tune as per the Seam/Hibernate-JPA standards before using it in the application.

            (1) Create multiple <xa-datasource> envelopes with different JNDI names, in the single mysql-ds.xml datasource file. e.g.


            (2) Create multiple <persistence-unit> envelopes with different persistence unit name, in a single persistence.xml file of your application e.g.

            <persistence-unit name="1TestPU" transaction-type="JTA">
            <persistence-unit name="2TestPU" transaction-type="JTA">

            (3) In my case, the persistence unit name is the same as User branch office name. So it will be available in User entity which has Seam SESSION scope after user logged in.

            (4) Here is the Stalteless bean which uses different persistence unit as follows
            private EntityManager em;

            private EntityManagerFactory emf;

            // get the persistence unit name from User Entity and invoke below method

            public List findOms(String puName){
            emf = Persistence.createEntityManagerFactory(puName);
            em = emf.createEntityManager();
            List x = (List ) em.createQuery("select t from Om t").getResultList();
            return x;

            I hope this helps you. LEt me also know if you have found some better approch.

            • 3. Re: Dynamic Datasource invocation at runtime in JSF-EJB3-MySql a
              Ahsanfile Project Newbie

              I have same problem...

              but i have a quick win solution : BACK TO JDBC !...

              • 4. Re: Dynamic Datasource invocation at runtime in JSF-EJB3-MySql a
                Matej Lazar Newbie

                I solved the problem with seam. I created component which is returning different entity managers for different host names.

                public class SchemaSelection {

                    public EntityManager getEntityManager() {

                        String emName = getEmName(HttpUtil.getRequest().getServerName());    

                        return (EntityManager)Component.getInstance(emName);




                All different entity managers are defined in components.xml.