Hi guys,
I had created a factory class that use Ejb3Configuration to add the dynamic persistent-units.
Code:
public class JPAUtil
{
private static Logger log = Logger.getLogger(JPAUtil.class);
private static Map<String, EntityManagerFactory> factories = new HashMap<String, EntityManagerFactory>();
private static final List<String> businessJars = Arrays.asList(
"entity-impl-0.0.1.jar",
"access-impl-0.0.1.jar");
private static final List<String> dataSourceNames = Arrays.asList(
"ums001DS", "ums002DS");
public static void createEMFs()
{
for ( String dsName : dataSourceNames )
{
if ( !factories.containsKey(dsName) )
{
log.debug("EntitiManagerFactory does not exists.");
createEMF(dsName);
}
}
}
private static void createEMF(String dataSourceName)
{
try
{
log.info("Creating new EntityManagerFactory to the DataSource "
+ dataSourceName);
Ejb3Configuration ejbConf = new Ejb3Configuration();
log.info("Setting the properties");
ejbConf.setProperty("hibernate.connection.datasource", "java:/"
+ dataSourceName);
ejbConf.setProperty("hibernate.dialect",
"org.hibernate.dialect.PostgreSQLDialect");
ejbConf.setProperty("hibernate.transaction.factory_class",
"org.hibernate.transaction.JTATransactionFactory");
/*
ejbConf.setProperty("hibernate.transaction.manager_lookup_class",
"org.hibernate.transaction.JBossTransactionManagerLookup");
*/
ejbConf.setProperty("hibernate.show_sql", "false");
ejbConf.setProperty("hibernate.format_sql", "true");
ejbConf = mapJarsEntities(ejbConf);
log.info("Building EntityManagerFactory");
EntityManagerFactory emf = ejbConf.buildEntityManagerFactory();
log.info("Adding EntityManagerFactory to the factories Map");
factories.put(dataSourceName, emf);
}
catch ( Exception e )
{
log.error(e);
}
}
private static Ejb3Configuration mapJarsEntities(Ejb3Configuration ejbConf)
{
for ( String nomeJar : businessJars )
{
mapEntityClasses(ejbConf, nomeJar);
}
return ejbConf;
}
@SuppressWarnings("unchecked")
private static Ejb3Configuration mapEntityClasses(Ejb3Configuration ejbConf,
String jarName)
{
try
{
Filter[] filters = new Filter[2];
filters[0] = new PackageFilter(false, null)
{
public boolean accept(String javaElementName)
{
return true;
}
};
filters[1] = new ClassFilter(false, new Class[] {
Entity.class, MappedSuperclass.class, Embeddable.class })
{
public boolean accept(String javaElementName)
{
return true;
}
};
log.info("processing jar " + jarName);
JarVisitor j = JarVisitorFactory.getVisitor(Thread.currentThread()
.getContextClassLoader().getResource(jarName), filters);
Set<Entry>[] entries = (Set<Entry>[])
j.getMatchingEntries();
for ( int i = 0; i < entries.length; i++ )
{
for ( Entry c : entries[i] )
{
log.info("Entity Found: " + c.getName());
ejbConf.addAnnotatedClass(Class.forName(c.getName()));
}
}
}
catch ( IOException e )
{
log.error(e);
}
catch ( ClassNotFoundException e )
{
log.error(e);
}
return ejbConf;
}
public static EntityManager getEntityManager(String dataSourceName)
{
EntityManager em = null;
try
{
log.info("getting EntityManager to the DataSource "
+ dataSourceName);
if ( !factories.containsKey(dataSourceName) )
{
log.debug("EntitiManagerFactory does not exists.");
createEMF(dataSourceName);
}
log.debug("creating EntityManager");
em = factories.get(dataSourceName).createEntityManager();
log.debug("returning EntityManager");
}
catch ( Exception e )
{
log.error(e);
}
return em;
}
}
I tried to test connecting to a DS db001ds and made a persist and a find operation successfully.
But when I try to create 2 EntityManagerFactory's, (DS db001ds and db002ds) i get the following warn:
Code:
...
14:09:45,953 INFO [DatasourceConnectionProvider] Using datasource: java:/db002ds
14:09:45,984 WARN [loggerI18N] [com.arjuna.ats.internal.jta.transaction.arjunacore.lastResource.dis
allow] [com.arjuna.ats.internal.jta.transaction.arjunacore.lastResource.disallo
w] Adding multiple last resources is disallowed. Current resource is org.jboss.resource.connectionma
nager.TxConnectionManager$LocalXAResource@1bc6b34
14:09:46,000 WARN [SettingsFactory] Could not obtain connection metadata
org.jboss.util.NestedSQLException: Could not enlist in transaction on entering meta-aware object!; -
nested throwable: (javax.transaction.SystemException: java.lang.Throwable: Una
bled to enlist resource, see the previous warnings. tx=TransactionImple < ac, BasicAction: -3f57ffbb:5b6:49492433:65 status: ActionStatus.ABORT_ONLY >); - nested throwable: (org.j
boss.resource.JBossResourceException: Could not enlist in transaction on entering meta-aware object!
; - nested throwable: (javax.transaction.SystemException: java.lang.Throwable:
Unabled to enlist resource, see the previous warnings. tx=TransactionImple < ac, BasicAction: -3f57ffbb:5b6:49492433:65 status: ActionStatus.ABORT_ONLY >))
at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:95
)
...
And any subsequent call to the EntityManager of db002ds throws the same message, but now like error and not warn.
Someone had used Ejb3Configuration without trouble to connect in multiple persistent units?
Thanks for your time,
Jorge