[jboss-user] [EJB 3.0] - Re: How to connect multiple databases at same time?

kmagnant do-not-reply at jboss.com
Thu Oct 11 18:24:22 EDT 2007


This is a question frequently asked, but not answered.  I am having a similar issue: I need to determine the schema (PersistenceUnit) to use at runtime - actually on a per request basis.

Has this nut been cracked?  Ideally I would like to still be able to use JTA and let the container take care of the transaction details.  Below I try to summarize the approach I have been trying, but it doesn't feel right.  If you know of any examples or references that address this issue I would be grateful. 

I am using jBoss 4.2.1 as it was downloaded with the exception of defining the data sources, which are all defined in one file, all are local-tx-datasources.

Based on user entered criteria, I need to determine which schema to use.  The structure of each schema is identical so the Entity beans are properly defined for the each possible schema.

I have been able to get things working for read requests by using a static EntityFactoryLocator that returns the proper EntityManager given some decision criteria.  Apparently when you use a helper class to get the EntityManager, your transaction type must be set to RESOURCE-LOCAL.  So, the session facade that wraps the entity calls EntityFactoryLocator(decisionCriteria).getEntityManager().  

When a session facade method needs to do an update, I run into an issue where the update works in the correct schema, but I am unable to close the entityManager so the corresponding connection is never released back to the pool.  This is likely related to the fact that I have an EXTENDED persistence context - that I don't know how to change.  My code, in the session facade, looks like:

// update one entity
EntityTransaction transaction = entityManager.getTransaction();               
transaction.begin();
if (query.executeUpdate() == 1) {
    entityManager.commit();  //  Blows up if not commented out. java.sql.SQLException: You cannot commit during a managed transaction!
    entityManager.close();
}
else rollback, clean up, and throw some exception

I know I have issues with the above.  When in debug mode I see that the persistenceContext is EXTENDED.

Thanks for looking.
               


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

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



More information about the jboss-user mailing list