[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - Re: Dynamic Datasource invocation at runtime in JSF-EJB3-MyS

pragun@imcsystemsuk.com do-not-reply at jboss.com
Tue Mar 24 12:08:09 EDT 2009


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. 

         <xa-datasource>
                    <jndi-name>1TestDS</jndi-name>
                     ..
         </xa-datasource>
         <xa-datasource>
                    <jndi-name>2TestDS</jndi-name>
                     ..
         </xa-datasource>


(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">	
        <jta-data-source>java:/1TestDS</jta-data-source>
         ...
        </persistence-unit>
        <persistence-unit name="2TestPU" transaction-type="JTA">
        <jta-data-source>java:/2TestDS</jta-data-source>
         ...
        </persistence-unit>


(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
  @PersistenceContext
  private EntityManager em;

  @PersistenceUnit
  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();
	em.close();
	return x;
  }

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

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4220673#4220673

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4220673



More information about the jboss-user mailing list