use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.
the class PersistenceProvider method createContainerEntityManagerFactoryImpl.
protected EntityManagerFactory createContainerEntityManagerFactoryImpl(PersistenceUnitInfo info, Map properties, boolean requiresConnection) {
// Record that we are inside a JEE container to allow weaving for non managed persistence units.
JavaSECMPInitializer.setIsInContainer(true);
Map nonNullProperties = (properties == null) ? new HashMap<>() : properties;
String forceTargetServer = EntityManagerFactoryProvider.getConfigPropertyAsString(SystemProperties.ENFORCE_TARGET_SERVER, null);
if ("true".equalsIgnoreCase(forceTargetServer)) {
nonNullProperties.remove(PersistenceUnitProperties.TARGET_SERVER);
}
EntityManagerSetupImpl emSetupImpl = null;
if (EntityManagerSetupImpl.mustBeCompositeMember(info)) {
// persistence unit cannot be used standalone (only as a composite member).
// still the factory will be created but attempt to createEntityManager would cause an exception.
emSetupImpl = new EntityManagerSetupImpl(info.getPersistenceUnitName(), info.getPersistenceUnitName());
// predeploy assigns puInfo and does not do anything else.
// the session is not created, no need to add emSetupImpl to the global map.
emSetupImpl.predeploy(info, nonNullProperties);
} else {
boolean isNew = false;
ClassTransformer transformer = null;
String uniqueName = PersistenceUnitProcessor.buildPersistenceUnitName(info.getPersistenceUnitRootUrl(), info.getPersistenceUnitName());
String sessionName = EntityManagerSetupImpl.getOrBuildSessionName(nonNullProperties, info, uniqueName);
synchronized (EntityManagerFactoryProvider.emSetupImpls) {
emSetupImpl = EntityManagerFactoryProvider.getEntityManagerSetupImpl(sessionName);
if (emSetupImpl == null) {
emSetupImpl = new EntityManagerSetupImpl(uniqueName, sessionName);
isNew = true;
emSetupImpl.setIsInContainerMode(true);
// if predeploy fails then emSetupImpl shouldn't be added to FactoryProvider
transformer = emSetupImpl.predeploy(info, nonNullProperties);
EntityManagerFactoryProvider.addEntityManagerSetupImpl(sessionName, emSetupImpl);
}
}
if (!isNew) {
if (!uniqueName.equals(emSetupImpl.getPersistenceUnitUniqueName())) {
throw PersistenceUnitLoadingException.sessionNameAlreadyInUse(sessionName, uniqueName, emSetupImpl.getPersistenceUnitUniqueName());
}
// synchronized to prevent undeploying by other threads.
boolean undeployed = false;
synchronized (emSetupImpl) {
if (emSetupImpl.isUndeployed()) {
undeployed = true;
} else {
// emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
transformer = emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), nonNullProperties);
}
}
if (undeployed) {
// it has been undeployed by factory.close() in another thread - start all over again.
return createContainerEntityManagerFactory(info, properties);
}
}
if (transformer != null) {
info.addTransformer(transformer);
}
}
EntityManagerFactoryImpl factory = null;
try {
factory = new EntityManagerFactoryImpl(emSetupImpl, nonNullProperties);
emSetupImpl.setRequiresConnection(requiresConnection);
emSetupImpl.preInitializeCanonicalMetamodel(factory);
// This code has been added to allow validation to occur without actually calling createEntityManager
if (emSetupImpl.shouldGetSessionOnCreateFactory(nonNullProperties)) {
factory.getDatabaseSession();
}
return factory;
} catch (RuntimeException ex) {
if (factory != null) {
factory.close();
} else {
emSetupImpl.undeploy();
}
throw ex;
}
}
use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.
the class PersistenceProvider method createEntityManagerFactoryImpl.
/**
* Internal method to return the entity manager factory.
*/
protected EntityManagerFactoryImpl createEntityManagerFactoryImpl(PersistenceUnitInfo puInfo, Map properties, boolean requiresConnection) {
if (puInfo != null) {
boolean isNew = false;
// the name the uniquely defines the pu
String uniqueName = null;
String sessionName = null;
EntityManagerSetupImpl emSetupImpl = null;
String puName = puInfo.getPersistenceUnitName();
JPAInitializer initializer = getInitializer(puInfo.getPersistenceUnitName(), properties);
try {
if (EntityManagerSetupImpl.mustBeCompositeMember(puInfo)) {
// Persistence unit cannot be used standalone (only as a composite member).
// Still the factory will be created but attempt to createEntityManager would cause an exception.
emSetupImpl = new EntityManagerSetupImpl(puName, puName);
// Predeploy assigns puInfo and does not do anything else.
// The session is not created, no need to add emSetupImpl to the global map.
emSetupImpl.predeploy(puInfo, properties);
isNew = true;
} else {
if (initializer.isPersistenceUnitUniquelyDefinedByName()) {
uniqueName = puName;
} else {
uniqueName = initializer.createUniquePersistenceUnitName(puInfo);
}
sessionName = EntityManagerSetupImpl.getOrBuildSessionName(properties, puInfo, uniqueName);
synchronized (EntityManagerFactoryProvider.emSetupImpls) {
emSetupImpl = EntityManagerFactoryProvider.getEntityManagerSetupImpl(sessionName);
if (emSetupImpl == null) {
// there may be initial emSetupImpl cached in Initializer - remove it and use.
emSetupImpl = initializer.extractInitialEmSetupImpl(puName);
if (emSetupImpl != null) {
// change the name
emSetupImpl.changeSessionName(sessionName);
} else {
// create and predeploy a new emSetupImpl
emSetupImpl = initializer.callPredeploy((SEPersistenceUnitInfo) puInfo, properties, uniqueName, sessionName);
}
// emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), properties);
EntityManagerFactoryProvider.addEntityManagerSetupImpl(sessionName, emSetupImpl);
isNew = true;
}
}
}
} catch (Exception e) {
throw PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(initializer.getInitializationClassLoader(), e);
}
if (!isNew) {
if (!uniqueName.equals(emSetupImpl.getPersistenceUnitUniqueName())) {
throw PersistenceUnitLoadingException.sessionNameAlreadyInUse(sessionName, uniqueName, emSetupImpl.getPersistenceUnitUniqueName());
}
// synchronized to prevent undeploying by other threads.
boolean undeployed = false;
synchronized (emSetupImpl) {
if (emSetupImpl.isUndeployed()) {
undeployed = true;
} else {
// emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), properties);
}
}
if (undeployed) {
// it has been undeployed by factory.close() in another thread - start all over again.
return (EntityManagerFactoryImpl) createEntityManagerFactory(puName, properties);
}
}
EntityManagerFactoryImpl factory = null;
try {
factory = new EntityManagerFactoryImpl(emSetupImpl, properties);
emSetupImpl.setRequiresConnection(requiresConnection);
emSetupImpl.preInitializeCanonicalMetamodel(factory);
if (emSetupImpl.shouldGetSessionOnCreateFactory(properties)) {
factory.getDatabaseSession();
}
return factory;
} catch (RuntimeException ex) {
if (factory != null) {
factory.close();
} else {
emSetupImpl.undeploy();
}
throw ex;
}
}
return null;
}
use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.
the class PersistenceFactoryBase method get.
/**
* Gets existing persistence context or create new based on given parameters if it doesn't exist.
*/
@Override
public PersistenceContext get(String persistenceUnitName, URI defaultURI, String version, Map<String, Object> initializationProperties) {
PersistenceContext persistenceContext = getDynamicPersistenceContext(persistenceUnitName, version);
if (persistenceContext == null) {
try {
DynamicClassLoader dcl = new DynamicRestClassLoader(Thread.currentThread().getContextClassLoader());
Map<String, Object> properties = new HashMap<>();
properties.put(PersistenceUnitProperties.CLASSLOADER, dcl);
if (initializationProperties != null) {
properties.putAll(initializationProperties);
}
properties.putIfAbsent(PersistenceUnitProperties.WEAVING_REST, "true");
EntityManagerFactoryImpl factory = (EntityManagerFactoryImpl) Persistence.createEntityManagerFactory(persistenceUnitName, properties);
ClassLoader sessionLoader = factory.getServerSession().getLoader();
if (!DynamicClassLoader.class.isAssignableFrom(sessionLoader.getClass())) {
properties = new HashMap<>();
dcl = new DynamicRestClassLoader(sessionLoader);
properties.put(PersistenceUnitProperties.CLASSLOADER, dcl);
if (initializationProperties != null) {
properties.putAll(initializationProperties);
}
properties.putIfAbsent(PersistenceUnitProperties.WEAVING_REST, "true");
factory.refreshMetadata(properties);
}
persistenceContext = bootstrapPersistenceContext(persistenceUnitName, factory, defaultURI, version, true);
} catch (Exception e) {
JPARSLogger.exception("exception_creating_persistence_context", new Object[] { persistenceUnitName, e.toString() }, e);
}
}
if ((persistenceContext != null) && (!persistenceContext.isWeavingEnabled())) {
JPARSLogger.error(persistenceContext.getServerSession().getSessionLog(), "weaving_required_for_relationships", new Object[] {});
throw JPARSException.invalidConfiguration();
}
return persistenceContext;
}
use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.
the class RuntimeFactory method createEntityManagerFactory.
public EntityManagerFactory createEntityManagerFactory(PersistenceUnit pu) {
EntityManagerSetupImpl emSetupImpl = null;
boolean isNew = false;
// the name that uniquely defines persistence unit
String name = pu.getName();
pu.setProperty(PersistenceUnitProperties.METADATA_SOURCE, new MetadataSource(pu));
SEPersistenceUnitInfo puInfo = (SEPersistenceUnitInfo) pu.getPersistenceUnitInfo();
JPAInitializer initializer = new PersistenceProvider().getInitializer(name, null);
Map<String, Object> props = new HashMap<String, Object>();
String uniqueName = initializer.createUniquePersistenceUnitName(puInfo);
String sessionName = EntityManagerSetupImpl.getOrBuildSessionName(props, puInfo, uniqueName);
try {
synchronized (EntityManagerFactoryProvider.emSetupImpls) {
emSetupImpl = EntityManagerFactoryProvider.getEntityManagerSetupImpl(sessionName);
if (emSetupImpl != null) {
if (puInfo.getClassLoader() != emSetupImpl.getPersistenceUnitInfo().getClassLoader()) {
emSetupImpl.undeploy();
EntityManagerFactoryProvider.emSetupImpls.remove(sessionName);
SessionManager manager = SessionManager.getManager();
if (manager.getSessions().containsKey(sessionName)) {
manager.destroySession(sessionName);
}
manager.destroy();
emSetupImpl = null;
}
}
if (emSetupImpl == null) {
// there may be initial emSetupImpl cached in Initializer - remove it and use.
emSetupImpl = initializer.extractInitialEmSetupImpl(name);
if (emSetupImpl == null) {
// create and predeploy a new emSetupImpl
emSetupImpl = initializer.callPredeploy(puInfo, props, uniqueName, sessionName);
} else {
// change the name
emSetupImpl.changeSessionName(sessionName);
}
// emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), props);
EntityManagerFactoryProvider.addEntityManagerSetupImpl(sessionName, emSetupImpl);
isNew = true;
}
}
} catch (Exception e) {
throw PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(initializer.getInitializationClassLoader(), e);
}
if (!isNew) {
if (!uniqueName.equals(emSetupImpl.getPersistenceUnitUniqueName())) {
throw PersistenceUnitLoadingException.sessionNameAlreadyInUse(sessionName, uniqueName, emSetupImpl.getPersistenceUnitUniqueName());
}
// synchronized to prevent undeploying by other threads.
boolean undeployed = false;
synchronized (emSetupImpl) {
if (emSetupImpl.isUndeployed()) {
undeployed = true;
}
// emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), props);
}
if (undeployed) {
// start all over again.
return createEntityManagerFactory(pu);
}
}
EntityManagerFactoryImpl factory = null;
try {
factory = new EntityManagerFactoryImpl(emSetupImpl, props);
// actually calling createEntityManager
if (emSetupImpl.shouldGetSessionOnCreateFactory(props)) {
factory.getDatabaseSession();
}
} catch (RuntimeException ex) {
if (factory != null) {
factory.close();
} else {
emSetupImpl.undeploy();
}
throw ex;
}
return factory;
}
use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.
the class TestJNDIConnector method testLibertTargetServerLookupType.
@Test
public void testLibertTargetServerLookupType() {
ServerSession session = ((EntityManagerFactoryImpl) libertyEmf).getServerSession();
_connector.connect(new Properties(), session);
Assert.assertEquals(String.class, _handler.getParamType());
}
Aggregations