In your resources/WEB-INF/components.xml configuration file you find the following line:
<core:managed-persistence-context name="entityManager" auto-create="true" persistence-unit-jndi-name="java:/yourDBnameEntityManagerFactory" />
Where "yourDBname" is normaly the name of your db reference in /META-INF/persistence.xml
<persistence-unit name="yourDBname"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:/yourDBnameDatasource</jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="hibernate.cache.use_query_cache" value="true"/> <property name="hibernate.show_sql" value="true"/> <property name="jboss.entity.manager.factory.jndi.name" value="java:/yourDBnameEntityManagerFactory"/> </properties> </persistence-unit>
which is actually a reference to the jboss *-ds.xml configuration.
You may configure a second database connection (an addition *-ds.xml, a new enty in your persitence.xml and components.xml).
<core:managed-persistence-context name="entityManager2" auto-create="true" persistence-unit-jndi-name="java:/yourOtherDBnameEntityManagerFactory" />
On code level you may reference the new entityManager with
entityManager= (EntityManager) Component.getInstance( "entityManager2" );
The old one with
entityManager= (EntityManager) Component.getInstance( "entityManager" ); Or simply with annotation @In EntityManager entityManager;
Can not garantee this works but i would try it.
But the problem is, that the Database Connection Information(url, password, username..) are retrievied at runtime of the application. so i can not configure a second, third.. database in a configuration file.
at Application start i have the following databasase defined in JBoss-Beans.xml(i'm using tomcat 6.0 server) :
<?xml version="1.0" encoding="UTF-8"?>
This datasource will be queried to gain the Datasource Connection Information to another Database. At this point i must programatically establish the Connection to that second Database.
We had the same problem and never really found a solution for it.
The (business/legal) requirement was to store each clients financial data in a separate DB, but have a single web application where a use can switch between different clients (depending on authorizations).
We never really found a way to solve the problem. As a workaround we
did what Ralph proposed and individually configured lots of datasources (one for each client, plus a couple spare ones) with generic names and access the entity manager programmatically.
Besides being an ugly hack and wasting resources it does not really solve the problem the original poster is having:
What do you do if the database connection details are only delivered dynamically after login?
This would be dead-easy to do with JDBC, but Hibernate/JPA/Seam makes this hard to do.
@bravefencer (?): Put code into code tags!