Search in sources :

Example 1 with XmlFish

use of org.eclipse.persistence.jpa.test.basic.model.XmlFish 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 2 with XmlFish

use of org.eclipse.persistence.jpa.test.basic.model.XmlFish 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 3 with XmlFish

use of org.eclipse.persistence.jpa.test.basic.model.XmlFish in project eclipselink by eclipse-ee4j.

the class TestBasicPersistence method persistTest.

@Test
public void persistTest() {
    if (emf == null)
        return;
    EntityManager em = emf.createEntityManager();
    try {
        em.getTransaction().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.getTransaction().commit();
        em.clear();
        Dog foundDog = em.find(Dog.class, d.getId());
        foundDog.getOwner();
        Assert.assertTrue(_sql.size() > 0);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) XmlFish(org.eclipse.persistence.jpa.test.basic.model.XmlFish) Person(org.eclipse.persistence.jpa.test.basic.model.Person) Dog(org.eclipse.persistence.jpa.test.basic.model.Dog) Test(org.junit.Test)

Example 4 with XmlFish

use of org.eclipse.persistence.jpa.test.basic.model.XmlFish 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)

Aggregations

EntityManager (jakarta.persistence.EntityManager)4 Dog (org.eclipse.persistence.jpa.test.basic.model.Dog)4 Person (org.eclipse.persistence.jpa.test.basic.model.Person)4 XmlFish (org.eclipse.persistence.jpa.test.basic.model.XmlFish)4 Test (org.junit.Test)4 EntityTransaction (jakarta.persistence.EntityTransaction)3 HashMap (java.util.HashMap)3 EntityManagerFactoryImpl (org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl)3 ConnectionPolicy (org.eclipse.persistence.sessions.server.ConnectionPolicy)3 ServerSession (org.eclipse.persistence.sessions.server.ServerSession)3 PersistenceException (jakarta.persistence.PersistenceException)1 RollbackException (jakarta.persistence.RollbackException)1 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)1