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()) ) {
emf = provider.createContainerEntityManagerFactory(persistenceUnitInfo, persistenceUnitInfo.getProperties()); // 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)
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());
emf = provider.createContainerEntityManagerFactory(persistenceUnitInfo, persistenceUnitInfo.getProperties()); // 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)
if (emf != null) {
log.info("Found compatible persistence provider");
return emf;
}
}