jpa - How can I create a JTA-aware EntityManager in WebLogic 12c with a data-source not known at compile time? -


i working on application, deployed in weblogic 12c, needs able obtain jpa entitymanager (eclipselink 2.5.2) connected arbitrary data source @ runtime. not know jndi name of data source be; there several many of them, connected different databases identical schemas. data source name cannot specified in persistence.xml inside application; must come outside (configuration file likely).

i don't think can have entitymanagerfactory or entitymanager injected; pretty tightly coupled configuration in persistence.xml , not seem able override jta data source name. this, example, not work:

@persistenceunit(unitname="mypu") private entitymanagerfactory emf; // ...     map<string, object> emprops = new hashmap<string, object>();     emprops.put(entitymanagerproperties.jta_datasource, "jdbc/foobar");     entitymanager em = emf.createentitymanager(emprops); 

my entitymanager here still connected jta datasource specified in persistence.xml.

so started looking @ creating entitymangerfactory through non-injected means, persistence.createentitymanagerfactory(puname, propmap) here, seems, no matter persistence.xml or property map says, resource_local entitymanagerfactory!

how can entitymanager or entitymanagerfactory jta-enabled and associated arbitrary datasource name not known @ compile-time?

this trick, @ least in eclipselink 2.5.2:

    map<string, object> properties = new hashmap<string, object>();     properties.put(persistenceunitproperties.transaction_type, "jta");     properties.put(persistenceunitproperties.jta_datasource, "jdbc/foobar");     emf = persistence.createentitymanagerfactory("mypu", properties);     jpaentitymanagerfactory jemf = (jpaentitymanagerfactory)emf;      weblogictransactioncontroller wlstx = new weblogictransactioncontroller();     if (jemf.getdatabasesession() != null && jemf.getdatabasesession().getexternaltransactioncontroller() == null) {         jemf.getdatabasesession().setexternaltransactioncontroller(wlstx);     }     if (jemf.getserversession() != null && jemf.getserversession().getexternaltransactioncontroller() == null) {         jemf.getserversession().setexternaltransactioncontroller(wlstx);     } 

by adding transaction controller emf, once again enlisted jta , respect jta transactions. persistence.xml provides dummy value jta datasource; override in code , away go!

nb: getdatabasesession() , getserversession() in fact return exact same object. away setting 1 of these, undocumented , you're better off safely setting both of them, sure.


Comments