DTodt
DTodt

Reputation: 380

How to Use a dynamic @PersistenceContext with Hibernate and JBoss AS 7 Connection Pool

Good morning, I'm trying to make a database script executor.

When the web application starts, will show to the User, a listing of available datasources (pre-registered in JBoss AS7).

After choosing the desired datasource, the application will use a @PersistenceContext based on the choice that the User did.

The answer that says how to list the datasources I found here: James R. Perkins - Answer

And the answer that tells how to create EntityManagers from a JNDI I found here: Nayan Wadekar - Answer

My question is. It is possible to define a @PersistenceContext from a JNDI? How would my "persistence.xml" file, if I do not use a JTA DataSource ("jta-data-source") declared?

Currently it looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="database" transaction-type="JTA">
        <jta-data-source>java:jboss/datasources/DefaultDS</jta-data-source>
        <properties>
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>

Is there any tutorial or source code example that explains a little how this operation work?

Note: I want to control the JBoss AS Connection Pools, I would not schedule it, I believe that JBoss will better manage Pools than any code I create.

Upvotes: 3

Views: 3249

Answers (1)

Kirill Mikhailov
Kirill Mikhailov

Reputation: 1069

There is a solution if all of your datasources connects to one database using different database schemas. In this case you can create one data source, define it in persistence.xml and dynamically create EntityManagerFactory based on this datasource but for another database scheme:

private Map<String, EntityManagerFactory> entityManagers = Collections.synchronizedMap(new HashMap<String, EntityManagerFactory>());

public EntityManager getEntityManager(String databaseSchema) {
    EntityManagerFactory emf = entityManagers.get(databaseSchema.toUpperCase());
    if (emf == null) {
        emf = Persistence.createEntityManagerFactory("your_persistent_unit_name", createMap(databaseSchema));
        entityManagers.put(databaseSchema, emf);
    }
    return emf.createEntityManager();
}

public static java.util.Map createMap(String databaseSchema) {
    java.util.Map map = new HashMap();
    map.put("provider", "org.hibernate.ejb.HibernatePersistence");
    map.put("hibernate.cache.provider_class", "org.hibernate.cache.HashtableCacheProvider");
    map.put("hibernate.default_schema", databaseSchema.toUpperCase());
    map.put("hibernate.show_sql", "false");
    map.put("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
    map.put("hibernate.transaction.jta.platform", "org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform");
    map.put("hibernate.ejb.entitymanager_factory_name", databaseSchema+ "_entity_manager");
    return map;
}

Upvotes: 1

Related Questions