use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testEMFClose.
public void testEMFClose() {
// This test tests the bug fix for 260511
// The NPE would be thrown if the EnityManager
// was created through the constructor
String errorMsg = "";
EntityManagerFactory em = new EntityManagerFactoryImpl(getSessionBroker());
try {
em.close();
} catch (RuntimeException ex) {
errorMsg = "EMFClose: " + ex.getMessage() + ";";
}
if (errorMsg.length() > 0) {
fail(errorMsg);
}
}
use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.
the class TestBasicPersistence method testNonpooledConnectionWithErrorOnAcquireConnection.
@Test
public void testNonpooledConnectionWithErrorOnAcquireConnection() throws Exception {
System.out.println("********************");
System.out.println("*BEGIN testNonpooledConnectionWithErrorOnAcquireConnection()");
if (emf == null)
return;
final EntityManagerFactoryImpl emfi = emf.unwrap(EntityManagerFactoryImpl.class);
Assert.assertNotNull(emfi);
// Create an em with a unpooled connection policy, idea taken from EntityManagerJUnitTestSuite.testNonPooledConnection()
final ServerSession ss = emfi.getServerSession();
// cache the original driver name and connection string.
try {
setupDriverWrapper(ss);
// Clone the connection policy and set the pool name to null to emulate using non-pooled connections
final ConnectionPolicy connectionPolicy = (ConnectionPolicy) ss.getDefaultConnectionPolicy().clone();
connectionPolicy.setLogin(ss.getLogin());
connectionPolicy.setPoolName(null);
final Map<String, Object> properties = new HashMap<>();
properties.put(EntityManagerProperties.CONNECTION_POLICY, connectionPolicy);
// Validate against initial non-pooled connection count
final int initialNonPooledConnections = ss.getNumberOfNonPooledConnectionsUsed();
final int maxNonPooledConnections = ss.getMaxNumberOfNonPooledConnections();
System.out.println("initialNonPooledConnections = " + initialNonPooledConnections);
System.out.println("maxNonPooledConnections = " + maxNonPooledConnections);
if (maxNonPooledConnections != -1 && initialNonPooledConnections >= maxNonPooledConnections) {
Assert.fail("initialNonPooledConnections >= maxNonPooledConnections, other tests may be leaking.");
}
EntityManager em = emf.createEntityManager(properties);
EntityTransaction et = em.getTransaction();
try {
Person p = new Person();
Dog d = new Dog("Bingo");
p.setDog(d);
d.setOwner(p);
et.begin();
final int nonPooledConnectionsBeforePersist = ss.getNumberOfNonPooledConnectionsUsed();
System.out.println("nonPooledConnectionsBeforePersist = " + nonPooledConnectionsBeforePersist);
em.persist(p);
em.persist(d);
em.persist(new XmlFish());
final int nonPooledConnectionsBeforeFlush = ss.getNumberOfNonPooledConnectionsUsed();
System.out.println("nonPooledConnectionsBeforeFlush = " + nonPooledConnectionsBeforeFlush);
try {
// .breakDriver(); // would be ideal, but results in bug #515961
DriverWrapper.breakNewConnections();
em.flush();
Assert.fail("No PersistenceException was thrown.");
} catch (PersistenceException pe) {
// Expected
} finally {
DriverWrapper.repairAll();
}
final int nonPooledConnectionsAfterFlush = ss.getNumberOfNonPooledConnectionsUsed();
System.out.println("nonPooledConnectionsAfterFlush = " + nonPooledConnectionsAfterFlush);
Assert.assertEquals("nonPooledConnectionsBeforeFlush == nonPooledConnectionsAfterFlush", nonPooledConnectionsBeforeFlush, nonPooledConnectionsAfterFlush);
try {
et.rollback();
} catch (Throwable t) {
// Some databases such as mysql may see the Connection as still set to autocommit=true, and
// the DriverWrapper was set to make new connections broken, but the real Connection is
// still established, but being "Broken" prevents the Connection from being set autocommit=false.
}
final int nonPooledConnectionsAfterRollback = ss.getNumberOfNonPooledConnectionsUsed();
System.out.println("nonPooledConnectionsAfterRollback = " + nonPooledConnectionsAfterRollback);
Assert.assertTrue("initialNonPooledConnections >= nonPooledConnectionsAfterRollback", initialNonPooledConnections >= nonPooledConnectionsAfterRollback);
} finally {
if (et.isActive()) {
et.rollback();
}
em.close();
}
} finally {
DriverWrapper.repairAll();
System.out.println("*END testNonpooledConnectionWithErrorOnAcquireConnection()");
}
}
use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.
the class TestBasicPersistence method testNonpooledConnectionWithErrorOnReleaseConnection.
@Test
public void testNonpooledConnectionWithErrorOnReleaseConnection() throws Exception {
System.out.println("********************");
System.out.println("*BEGIN testNonpooledConnectionWithErrorOnReleaseConnection()");
if (emf == null)
return;
final EntityManagerFactoryImpl emfi = emf.unwrap(EntityManagerFactoryImpl.class);
Assert.assertNotNull(emfi);
// Create an em with a unpooled connection policy, idea taken from EntityManagerJUnitTestSuite.testNonPooledConnection()
final ServerSession ss = emfi.getServerSession();
// cache the original driver name and connection string.
try {
setupDriverWrapper(ss);
DriverWrapper.repairAll();
// Clone the connection policy and set the pool name to null to emulate using non-pooled connections
final ConnectionPolicy connectionPolicy = (ConnectionPolicy) ss.getDefaultConnectionPolicy().clone();
connectionPolicy.setLogin(ss.getLogin());
connectionPolicy.setPoolName(null);
final Map<String, Object> properties = new HashMap<>();
properties.put(EntityManagerProperties.CONNECTION_POLICY, connectionPolicy);
// Validate against initial non-pooled connection count
final int initialNonPooledConnections = ss.getNumberOfNonPooledConnectionsUsed();
final int maxNonPooledConnections = ss.getMaxNumberOfNonPooledConnections();
System.out.println("initialNonPooledConnections = " + initialNonPooledConnections);
System.out.println("maxNonPooledConnections = " + maxNonPooledConnections);
if (maxNonPooledConnections != -1 && initialNonPooledConnections >= maxNonPooledConnections) {
Assert.fail("initialNonPooledConnections >= maxNonPooledConnections, other tests may be leaking.");
}
EntityManager em = emf.createEntityManager(properties);
EntityTransaction et = em.getTransaction();
try {
et.begin();
Person p = new Person();
Dog d = new Dog("Bingo");
p.setDog(d);
d.setOwner(p);
em.persist(p);
em.persist(d);
em.persist(new XmlFish());
final int nonPooledConnectionsBeforeFlush = ss.getNumberOfNonPooledConnectionsUsed();
System.out.println("nonPooledConnectionsBeforeFlush = " + nonPooledConnectionsBeforeFlush);
em.flush();
final int nonPooledConnectionsAfterFlush = ss.getNumberOfNonPooledConnectionsUsed();
System.out.println("nonPooledConnectionsAfterFlush = " + nonPooledConnectionsAfterFlush);
Assert.assertEquals("nonPooledConnectionsBeforeFlush + 1 == nonPooledConnectionsAfterFlush", nonPooledConnectionsBeforeFlush + 1, nonPooledConnectionsAfterFlush);
// The non-pooled Connection would be released after the commit.
DriverWrapper.breakOldConnections();
try {
et.commit();
Assert.fail("No RollbackException was thrown.");
} catch (RollbackException re) {
// Expected
} finally {
DriverWrapper.repairAll();
}
int nonPooledConnectionsAfterCommit = ss.getNumberOfNonPooledConnectionsUsed();
System.out.println("nonPooledConnectionsAfterCommit = " + nonPooledConnectionsAfterCommit);
Assert.assertTrue("initialNonPooledConnections >= nonPooledConnectionsAfterCommit", initialNonPooledConnections >= nonPooledConnectionsAfterCommit);
} finally {
DriverWrapper.repairAll();
if (et.isActive()) {
et.rollback();
}
em.close();
}
} finally {
DriverWrapper.repairAll();
System.out.println("*END testNonpooledConnectionWithErrorOnReleaseConnection()");
}
}
use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.
the class TestBasicPersistence method testNonpooledConnection.
/*
* Verify that the number of non pooled connections used is accounted for accurately in regular non-error scenario usage.
*/
@Test
public void testNonpooledConnection() throws Exception {
System.out.println("********************");
System.out.println("*BEGIN testNonpooledConnection()");
if (emf == null)
return;
final EntityManagerFactoryImpl emfi = emf.unwrap(EntityManagerFactoryImpl.class);
Assert.assertNotNull(emfi);
// Create an em with a unpooled connection policy, idea taken from EntityManagerJUnitTestSuite.testNonPooledConnection()
final ServerSession ss = emfi.getServerSession();
// Clone the connection policy and set the pool name to null to emulate using non-pooled connections
final ConnectionPolicy connectionPolicy = (ConnectionPolicy) ss.getDefaultConnectionPolicy().clone();
connectionPolicy.setLogin(ss.getLogin());
connectionPolicy.setPoolName(null);
final Map<String, Object> properties = new HashMap<>();
properties.put(EntityManagerProperties.CONNECTION_POLICY, connectionPolicy);
final int initialNonPooledConnections = ss.getNumberOfNonPooledConnectionsUsed();
final int maxNonPooledConnections = ss.getMaxNumberOfNonPooledConnections();
System.out.println("initialNonPooledConnections = " + initialNonPooledConnections);
System.out.println("maxNonPooledConnections = " + maxNonPooledConnections);
if (maxNonPooledConnections != -1 && initialNonPooledConnections >= maxNonPooledConnections) {
Assert.fail("initialNonPooledConnections >= maxNonPooledConnections, other tests may be leaking.");
}
EntityManager em = emf.createEntityManager(properties);
EntityTransaction et = em.getTransaction();
try {
et.begin();
Person p = new Person();
Dog d = new Dog("Bingo");
p.setDog(d);
d.setOwner(p);
em.persist(p);
em.persist(d);
em.persist(new XmlFish());
em.flush();
int nonPooledConnectionsAfterFlush = ss.getNumberOfNonPooledConnectionsUsed();
System.out.println("nonPooledConnectionsAfterFlush = " + nonPooledConnectionsAfterFlush);
Assert.assertTrue("Test problem: connection should be not pooled", em.unwrap(UnitOfWork.class).getParent().getAccessor().getPool() == null);
Assert.assertEquals(initialNonPooledConnections + 1, nonPooledConnectionsAfterFlush);
et.commit();
em.clear();
int nonPooledConnectionsAfterCommit = ss.getNumberOfNonPooledConnectionsUsed();
System.out.println("nonPooledConnectionsAfterCommit = " + nonPooledConnectionsAfterCommit);
Assert.assertEquals(initialNonPooledConnections, nonPooledConnectionsAfterCommit);
Dog foundDog = em.find(Dog.class, d.getId());
foundDog.getOwner();
Assert.assertTrue(_sql.size() > 0);
int nonPooledConnectionsAfterFind = ss.getNumberOfNonPooledConnectionsUsed();
System.out.println("nonPooledConnectionsAfterFind = " + nonPooledConnectionsAfterFind);
Assert.assertEquals(initialNonPooledConnections, nonPooledConnectionsAfterFind);
} finally {
if (et.isActive()) {
et.rollback();
}
em.close();
System.out.println("*END testNonpooledConnection()");
}
}
use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.
the class AdvancedMultiTenantSchemaJunitTest method testPolicyConfigurationCustom.
public void testPolicyConfigurationCustom() {
if (!getPlatform().isMySQL()) {
warning("this test is supported on MySQL only");
return;
}
if (skipTest) {
warning("this test requires DB schema created in testSetup to be available.");
return;
}
// custom configuration: shared EMF = false, shared cache = true
Properties emfP = new Properties();
emfP.putAll(emfProperties);
emfP.put(PersistenceUnitProperties.MULTITENANT_SCHEMA_PROPERTY_DEFAULT, "not-shared");
emfP.put(PersistenceUnitProperties.MULTITENANT_SHARED_EMF, "false");
emfP.put(PersistenceUnitProperties.MULTITENANT_SHARED_CACHE, "true");
emf = Persistence.createEntityManagerFactory(getPersistenceUnitName(), emfP);
ServerSession session = ((EntityManagerFactoryImpl) emf).getServerSession();
MultitenantPolicy policy = session.getProject().getMultitenantPolicy();
assertNotNull("project MultitenantPolicy is null", policy);
assertTrue("not SchemaPerMultitenantPolicy", policy.isSchemaPerMultitenantPolicy());
assertFalse("shared EMF", ((SchemaPerMultitenantPolicy) policy).shouldUseSharedEMF());
assertTrue("not shared cache", ((SchemaPerMultitenantPolicy) policy).shouldUseSharedCache());
assertEquals(PersistenceUnitProperties.MULTITENANT_SCHEMA_PROPERTY_DEFAULT, ((SchemaPerMultitenantPolicy) policy).getContextProperty());
assertEquals(EntityManagerProperties.MULTITENANT_SCHEMA_PROPERTY_DEFAULT, ((SchemaPerMultitenantPolicy) policy).getContextProperty());
assertTrue("unknown context property", session.getMultitenantContextProperties().contains(PersistenceUnitProperties.MULTITENANT_SCHEMA_PROPERTY_DEFAULT));
}
Aggregations