use of javax.persistence.spi.PersistenceProviderResolver in project OpenAttestation by OpenAttestation.
the class PersistenceManager method createEntityManagerFactory.
/**
* The new form of this method. The old form simply wraps this one now.
* Loads persistence.xml from the classpath and creates an entity manager
* with those settings, which can now include Apache Commons Pool and JDBC.
*
* @param persistenceUnitName
* @param properties
* @return
*/
public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Properties jpaProperties) {
log.debug("Loading database driver {} for persistence unit {}", new String[] { jpaProperties.getProperty("javax.persistence.jdbc.driver"), persistenceUnitName });
try {
Class.forName(jpaProperties.getProperty("javax.persistence.jdbc.driver"));
} catch (ClassNotFoundException ex) {
log.error("Cannot load JDBC Driver for persistence unit", ex);
}
PersistenceUnitInfo persistenceUnitInfo = null;
try {
persistenceUnitInfo = getPersistenceUnitInfo(persistenceUnitName, jpaProperties);
} catch (IOException e) {
throw new PersistenceException("Cannot load PersistenceUnit named " + persistenceUnitName, e);
}
if (persistenceUnitInfo == null) {
throw new PersistenceException("Cannot find PersistenceUnit named " + persistenceUnitName);
}
EntityManagerFactory emf = null;
PersistenceProviderResolver resolver = PersistenceProviderResolverHolder.getPersistenceProviderResolver();
List<PersistenceProvider> providers = resolver.getPersistenceProviders();
// check if we have the requested provider
if (persistenceUnitInfo.getPersistenceProviderClassName() != null) {
log.info("Looking for specific JPA provider: {}", persistenceUnitInfo.getPersistenceProviderClassName());
for (PersistenceProvider provider : providers) {
log.info("Looking at provider: {}", provider.getClass().getName());
if (provider.getClass().getName().equals(persistenceUnitInfo.getPersistenceProviderClassName())) {
// important: must use the properties as returned by the persistenceUnitInfo because it may have altered them... specifically: remove user and password entries after creating datasource to force eclipselink to call getConnection() instead of getConnection(user,password)
emf = provider.createContainerEntityManagerFactory(persistenceUnitInfo, persistenceUnitInfo.getProperties());
if (emf != null) {
log.info("Found requested persistence provider");
return emf;
}
}
}
}
// check if any other provider can accomodate the persistence unit
log.info("Looking for any compatible JPA provider");
for (PersistenceProvider provider : providers) {
log.info("Looking at provider: {}", provider.getClass().getName());
// important: must use the properties as returned by the persistenceUnitInfo because it may have altered them... specifically: remove user and password entries after creating datasource to force eclipselink to call getConnection() instead of getConnection(user,password)
emf = provider.createContainerEntityManagerFactory(persistenceUnitInfo, persistenceUnitInfo.getProperties());
if (emf != null) {
log.info("Found compatible persistence provider");
return emf;
}
}
throw new PersistenceException("No Persistence provider for EntityManager named " + persistenceUnitName);
}
Aggregations