Search in sources :

Example 11 with EntityManagerFactoryImpl

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);
    }
}
Also used : JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl)

Example 12 with EntityManagerFactoryImpl

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()");
    }
}
Also used : EntityTransaction(jakarta.persistence.EntityTransaction) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) HashMap(java.util.HashMap) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) EntityManager(jakarta.persistence.EntityManager) XmlFish(org.eclipse.persistence.jpa.test.basic.model.XmlFish) PersistenceException(jakarta.persistence.PersistenceException) ConnectionPolicy(org.eclipse.persistence.sessions.server.ConnectionPolicy) Person(org.eclipse.persistence.jpa.test.basic.model.Person) Dog(org.eclipse.persistence.jpa.test.basic.model.Dog) Test(org.junit.Test)

Example 13 with EntityManagerFactoryImpl

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()");
    }
}
Also used : EntityTransaction(jakarta.persistence.EntityTransaction) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) HashMap(java.util.HashMap) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) RollbackException(jakarta.persistence.RollbackException) EntityManager(jakarta.persistence.EntityManager) XmlFish(org.eclipse.persistence.jpa.test.basic.model.XmlFish) ConnectionPolicy(org.eclipse.persistence.sessions.server.ConnectionPolicy) Person(org.eclipse.persistence.jpa.test.basic.model.Person) Dog(org.eclipse.persistence.jpa.test.basic.model.Dog) Test(org.junit.Test)

Example 14 with EntityManagerFactoryImpl

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()");
    }
}
Also used : EntityTransaction(jakarta.persistence.EntityTransaction) UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) HashMap(java.util.HashMap) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) EntityManager(jakarta.persistence.EntityManager) XmlFish(org.eclipse.persistence.jpa.test.basic.model.XmlFish) ConnectionPolicy(org.eclipse.persistence.sessions.server.ConnectionPolicy) Person(org.eclipse.persistence.jpa.test.basic.model.Person) Dog(org.eclipse.persistence.jpa.test.basic.model.Dog) Test(org.junit.Test)

Example 15 with EntityManagerFactoryImpl

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));
}
Also used : ServerSession(org.eclipse.persistence.sessions.server.ServerSession) SchemaPerMultitenantPolicy(org.eclipse.persistence.descriptors.SchemaPerMultitenantPolicy) MultitenantPolicy(org.eclipse.persistence.descriptors.MultitenantPolicy) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) EntityManagerProperties(org.eclipse.persistence.config.EntityManagerProperties) PersistenceUnitProperties(org.eclipse.persistence.config.PersistenceUnitProperties) Properties(java.util.Properties)

Aggregations

EntityManagerFactoryImpl (org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl)22 ServerSession (org.eclipse.persistence.sessions.server.ServerSession)10 HashMap (java.util.HashMap)9 Test (org.junit.Test)8 EntityManager (jakarta.persistence.EntityManager)5 Properties (java.util.Properties)5 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)4 EntityManagerSetupImpl (org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl)4 EntityTransaction (jakarta.persistence.EntityTransaction)3 PersistenceException (jakarta.persistence.PersistenceException)3 Map (java.util.Map)3 PersistenceUnitLoadingException (org.eclipse.persistence.exceptions.PersistenceUnitLoadingException)3 SEPersistenceUnitInfo (org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo)3 JpaEntityManagerFactory (org.eclipse.persistence.jpa.JpaEntityManagerFactory)3 Dog (org.eclipse.persistence.jpa.test.basic.model.Dog)3 Person (org.eclipse.persistence.jpa.test.basic.model.Person)3 XmlFish (org.eclipse.persistence.jpa.test.basic.model.XmlFish)3 ConnectionPolicy (org.eclipse.persistence.sessions.server.ConnectionPolicy)3 RollbackException (jakarta.persistence.RollbackException)2 MultitenantPolicy (org.eclipse.persistence.descriptors.MultitenantPolicy)2