I have built a simple SOAP
java application(server side) and I am using Glassfish4,JPA/EclipseLink,EJB
. I have set the db connections(resources/poo
Another solution is creating the persistent context programmatically.
Define an persistent.xml without the connection. Similar to:
persistent.xml
mx.saaskun.model.UserInfo
Create a factory for the custom connection:
The method receives two parameters, the custom unit name and the JNDI for the connection.
DynamicResource.java
@Stateless
@LocalBean
public class DynamicResource implements Serializable{
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public EntityManagerFactory getEntityManager(unitName, jndiConnection){
Map properties = new HashMap();
properties.put("javax.persistence.jtaDataSource", jndiConnection);
return Persistence.createEntityManagerFactory(unitName, properties);
}
}
then you use as:
public class UserService{
@EJB
DynamicResource radResources;
public List listAll(){
List();
String[] databases = new String[]{"jndi/simfin","jndi/simfin2"};
for(String db:databases){
List results = listServerUsers("simfin", db);
allUsers.addAll(results);
}
return allUsers;
}
protected List listServerUsers(String unitName, String jndi){
EntityManager em= radResources.getEntityManager(unitName,jndi);
try {
Query q = em.createNamedQuery("UserInfo.findAll");
return (List) q.getResultList();
} finally {
em.close();
}
}
}