Search in sources :

Example 1 with ClientSession

use of org.eclipse.persistence.sessions.server.ClientSession in project eclipselink by eclipse-ee4j.

the class CursoredStreamReleaseConnectionsTest method test.

@Override
public void test() {
    CursoredStream stream = null;
    try {
        ExpressionBuilder builder = new ExpressionBuilder();
        ReportQuery query = new ReportQuery(Employee.class, builder);
        query.addAttribute("lastName");
        query.useCursoredStream();
        query.retrievePrimaryKeys();
        clientSession = serverSession.acquireClientSession();
        if (useUOW) {
            Session uow = clientSession.acquireUnitOfWork();
            stream = (CursoredStream) uow.executeQuery(query);
        } else {
            stream = (CursoredStream) clientSession.executeQuery(query);
        }
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}
Also used : ClientSession(org.eclipse.persistence.sessions.server.ClientSession) Session(org.eclipse.persistence.sessions.Session)

Example 2 with ClientSession

use of org.eclipse.persistence.sessions.server.ClientSession in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method internalTestConnectionPolicy.

public void internalTestConnectionPolicy(boolean useSetProperty) {
    // setup
    String errorMsg = "";
    HashMap properties = null;
    if (!useSetProperty) {
        properties = new HashMap();
        properties.put(EntityManagerProperties.JDBC_USER, "em_user");
        properties.put(EntityManagerProperties.JDBC_PASSWORD, "em_password");
        properties.put(EntityManagerProperties.JTA_DATASOURCE, "em_jta_datasource");
        properties.put(EntityManagerProperties.NON_JTA_DATASOURCE, "em_nonjta_datasource");
        properties.put(EntityManagerProperties.EXCLUSIVE_CONNECTION_MODE, ExclusiveConnectionMode.Always);
    }
    // test
    EntityManager em = null;
    boolean isInTransaction = false;
    try {
        // assume that if JTA is used on server then EntityManager is always injected.
        boolean isEmInjected = isOnServer() && getServerSession().getLogin().shouldUseExternalTransactionController();
        if (isEmInjected) {
            em = createEntityManager();
            // In server jta case need a transaction - otherwise the wrapped EntityManagerImpl is not kept.
            beginTransaction(em);
            isInTransaction = true;
            ((EntityManagerImpl) em.getDelegate()).setProperties(properties);
        } else {
            EntityManagerFactory emFactory = getEntityManagerFactory();
            em = emFactory.createEntityManager(properties);
        }
        if (useSetProperty) {
            em.setProperty(EntityManagerProperties.JDBC_USER, "em_user");
            em.setProperty(EntityManagerProperties.JDBC_PASSWORD, "em_password");
            em.setProperty(EntityManagerProperties.JTA_DATASOURCE, "em_jta_datasource");
            em.setProperty(EntityManagerProperties.NON_JTA_DATASOURCE, "em_nonjta_datasource");
            em.setProperty(EntityManagerProperties.EXCLUSIVE_CONNECTION_MODE, ExclusiveConnectionMode.Always);
        }
        // verify
        ClientSession clientSession;
        if (isOnServer()) {
            clientSession = (ClientSession) ((EntityManagerImpl) em.getDelegate()).getActivePersistenceContext(null).getParent();
        } else {
            clientSession = (ClientSession) ((EntityManagerImpl) em).getActivePersistenceContext(null).getParent();
        }
        if (!clientSession.isExclusiveIsolatedClientSession()) {
            errorMsg += "ExclusiveIsolatedClientSession was expected\n";
        }
        ConnectionPolicy policy = clientSession.getConnectionPolicy();
        if (policy.isPooled()) {
            errorMsg += "NOT pooled policy was expected\n";
        }
        String user = (String) policy.getLogin().getProperty("user");
        if (!user.equals("em_user")) {
            errorMsg += "em_user was expected\n";
        }
        String password = (String) policy.getLogin().getProperty("password");
        if (!password.equals("em_password")) {
            errorMsg += "em_password was expected\n";
        }
        if (!(((DatasourceLogin) policy.getLogin()).getConnector() instanceof JNDIConnector)) {
            errorMsg += "JNDIConnector was expected\n";
        } else {
            JNDIConnector jndiConnector = (JNDIConnector) ((DatasourceLogin) policy.getLogin()).getConnector();
            String dataSourceName = jndiConnector.getName();
            if (dataSourceName == null) {
                errorMsg += "NON null dataSourceName was expected\n";
            } else {
                if (clientSession.getParent().getLogin().shouldUseExternalTransactionController()) {
                    if (dataSourceName.equals("em_nonjta_datasource")) {
                        errorMsg += "em_jta_datasource was expected\n";
                    }
                } else {
                    if (dataSourceName.equals("em_jta_datasource")) {
                        errorMsg += "em_nonjta_datasource was expected\n";
                    }
                }
            }
        }
    } finally {
        // clean-up
        if (isInTransaction) {
            rollbackTransaction(em);
        }
        if (em != null) {
            closeEntityManager(em);
        }
    }
    if (errorMsg.length() > 0) {
        fail(errorMsg);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) HashMap(java.util.HashMap) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) ClientSession(org.eclipse.persistence.sessions.server.ClientSession) JNDIConnector(org.eclipse.persistence.sessions.JNDIConnector) ConnectionPolicy(org.eclipse.persistence.sessions.server.ConnectionPolicy)

Example 3 with ClientSession

use of org.eclipse.persistence.sessions.server.ClientSession in project eclipselink by eclipse-ee4j.

the class ProxyAuthenticationConnectionCustomizerTestCase method testServerSession.

void testServerSession() {
    ServerSession ss = (ServerSession) getSession();
    if (shouldEnableStatementCaching) {
        ss.getPlatform().setShouldCacheAllStatements(true);
    }
    verifyUser("ServerSession first read", getReadUser(ss), expectedMainSessionUser);
    // The first ClientSession created without proxy properties - should always use the same user as ServerSession.
    ClientSession cs1 = ss.acquireClientSession();
    if (shoulUseExclusiveIsolatedSession) {
        // verify that IsolatedSession is used if required.
        if (!(cs1 instanceof ExclusiveIsolatedClientSession)) {
            throw new TestProblemException("The ClientSession must be ExclusiveIsolatedClientSession");
        }
    }
    verifyUser("ClientSession1 before transaction read", getReadUser(cs1), expectedMainSessionUser);
    cs1.beginTransaction();
    verifyUser("ClientSession1in transaction read", getReadUser(cs1), expectedMainSessionUser);
    verifyUser("ClientSession1 wrote", getWriteUser(cs1), expectedMainSessionUser);
    cs1.rollbackTransaction();
    verifyUser("ClientSession1 after transaction read", getReadUser(cs1), expectedMainSessionUser);
    cs1.release();
    // The second ClientSession created with proxy properties.
    ClientSession cs2 = ss.acquireClientSession(clientSessionProxyProperties);
    if (shoulUseExclusiveIsolatedSession) {
        verifyUser("ExclusiveIsolatedClientSession2 before transaction read", getReadUser(cs2), expectedClientSessionUser);
    } else {
        verifyUser("ClientSession2 before transaction read", getReadUser(cs2), expectedMainSessionUser);
    }
    cs2.beginTransaction();
    verifyUser("ClientSession2 in transaction read", getReadUser(cs2), expectedClientSessionUser);
    verifyUser("ClientSession2 wrote", getWriteUser(cs2), expectedClientSessionUser);
    cs2.rollbackTransaction();
    if (shoulUseExclusiveIsolatedSession) {
        verifyUser("ExclusiveIsolatedClientSession2 after transaction read", getReadUser(cs2), expectedClientSessionUser);
    } else {
        verifyUser("ClientSession2 after transaction read", getReadUser(cs2), expectedMainSessionUser);
    }
    cs2.release();
    // verify that ServerSession still uses the correct user.
    // Because there is one one connection in each connection pool (internal pool case) this would fail in case proxy customizer
    // wasn't removed by cs2.
    verifyUser("ServerSession second read", getReadUser(ss), expectedMainSessionUser);
    // The third ClientSession created without proxy properties again - should always use the same user as ServerSession.
    // Because there is one one connection in each connection pool (internal pool case) this would fail in case proxy customizer
    // wasn't removed by cs2.
    ClientSession cs3 = ss.acquireClientSession();
    verifyUser("ClientSession3 before transaction read", getReadUser(cs3), expectedMainSessionUser);
    cs3.beginTransaction();
    verifyUser("ClientSession3 in transaction read", getReadUser(cs3), expectedMainSessionUser);
    verifyUser("ClientSession3 wrote", getWriteUser(cs3), expectedMainSessionUser);
    cs3.rollbackTransaction();
    verifyUser("ClientSession3 after transaction read", getReadUser(cs3), expectedMainSessionUser);
    cs3.release();
}
Also used : ServerSession(org.eclipse.persistence.sessions.server.ServerSession) ExclusiveIsolatedClientSession(org.eclipse.persistence.internal.sessions.ExclusiveIsolatedClientSession) ExclusiveIsolatedClientSession(org.eclipse.persistence.internal.sessions.ExclusiveIsolatedClientSession) ClientSession(org.eclipse.persistence.sessions.server.ClientSession) TestProblemException(org.eclipse.persistence.testing.framework.TestProblemException)

Example 4 with ClientSession

use of org.eclipse.persistence.sessions.server.ClientSession in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testSessionEventListeners.

// Bug 332581 - SessionBroker session event issues to consider and resolve when implementing JPA Session Broker
public void testSessionEventListeners() {
    EntityManager em = createEntityManager();
    UnitOfWorkImpl uow = em.unwrap(UnitOfWorkImpl.class);
    SessionBroker clientBroker = (SessionBroker) uow.getParent();
    SessionBroker broker = clientBroker.getParent();
    List<SessionEventListener> brokerListeners = getCompositeAndMemberListeners(broker);
    if (brokerListeners.size() > 1) {
        fail("broker expected to have a single instance of CompositeEventListener, found " + brokerListeners.size());
    } else {
        if (!(brokerListeners.get(0) instanceof CompositeEventListener)) {
            fail("broker expected to have a single instance of CompositeEventListener, found " + brokerListeners.get(0).getClass().getName());
        }
    }
    List<SessionEventListener> clientBrokerListeners = getCompositeAndMemberListeners(clientBroker);
    if (!brokerListeners.equals(clientBrokerListeners)) {
        fail("broker and clientBroker expected to share the same listeners");
    }
    List<SessionEventListener> uowListeners = getCompositeAndMemberListeners(uow);
    if (!clientBrokerListeners.equals(uowListeners)) {
        fail("clientBroker and uow expected to share the same listeners");
    }
    // classes mapped by member persistence units
    int n = 3;
    Class<?>[] classes = { Address.class, Employee.class, Project.class };
    for (int i = 0; i < n; i++) {
        ClientSession clientSession = (ClientSession) clientBroker.getSessionForClass(classes[i]);
        List<SessionEventListener> clientSessionListeners = getCompositeAndMemberListeners(clientSession);
        if (clientSessionListeners.size() != 2) {
            fail("clientSession expected to have a CompositeEventListener and a MemberEventListener, found " + clientSessionListeners.size());
        } else {
            // Listeners set after ServerSession has been created appear on the list in chronological order.
            if (!(Helper.getShortClassName(clientSessionListeners.get(0)).equals("MemberEventListener")) || !(Helper.getPackageName(clientSessionListeners.get(0).getClass()).equals(Helper.getPackageName(classes[i])))) {
                fail("the first clientSession's listener expected to be MemberEventListener from its package, found " + clientSessionListeners.get(0).getClass().getName());
            }
            if (!(clientSessionListeners.get(1) instanceof CompositeEventListener)) {
                fail("the second clientSession's listener expected to be CompositeEventListener, found " + clientSessionListeners.get(1).getClass().getName());
            }
        }
        ServerSession serverSession = clientSession.getParent();
        List<SessionEventListener> serverSessionListeners = getCompositeAndMemberListeners(serverSession);
        if (!clientSessionListeners.equals(serverSessionListeners)) {
            fail("clientSession and serverSession expected to share the same listeners");
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) ClientSession(org.eclipse.persistence.sessions.server.ClientSession) SessionBroker(org.eclipse.persistence.sessions.broker.SessionBroker) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) SessionEventListener(org.eclipse.persistence.sessions.SessionEventListener) CompositeEventListener(org.eclipse.persistence.testing.models.jpa.composite.advanced.CompositeEventListener)

Example 5 with ClientSession

use of org.eclipse.persistence.sessions.server.ClientSession in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method internalTestConnectionPolicy.

public void internalTestConnectionPolicy(boolean useSetProperty) {
    // setup
    String errorMsg = "";
    // number of composite members
    int n = 3;
    Map mapOfProperties = new HashMap();
    for (int i = 1; i <= n; i++) {
        String prefix = "em_" + i + "_";
        Map properties = new HashMap();
        properties.put(EntityManagerProperties.JDBC_USER, prefix + "user");
        properties.put(EntityManagerProperties.JDBC_PASSWORD, prefix + "password");
        properties.put(EntityManagerProperties.JTA_DATASOURCE, prefix + "jta_datasource");
        properties.put(EntityManagerProperties.NON_JTA_DATASOURCE, prefix + "nonjta_datasource");
        properties.put(EntityManagerProperties.EXCLUSIVE_CONNECTION_MODE, ExclusiveConnectionMode.Always);
        mapOfProperties.put(getCompositeMemberPuName(i), properties);
    }
    HashMap properties = null;
    if (!useSetProperty) {
        properties = new HashMap(1);
        properties.put(EntityManagerProperties.COMPOSITE_UNIT_PROPERTIES, mapOfProperties);
    }
    // test
    EntityManager em = null;
    boolean isInTransaction = false;
    try {
        // assume that if JTA is used on server then EntityManager is always injected.
        boolean isEmInjected = isOnServer() && getDatabaseSession().hasExternalTransactionController();
        if (isEmInjected) {
            em = createEntityManager();
            // In server jta case need a transaction - otherwise the wrapped EntityManagerImpl is not kept.
            beginTransaction(em);
            isInTransaction = true;
            ((EntityManagerImpl) em.getDelegate()).setProperties(properties);
        } else {
            EntityManagerFactory emFactory = getEntityManagerFactory();
            em = emFactory.createEntityManager(properties);
        }
        if (useSetProperty) {
            em.setProperty(EntityManagerProperties.COMPOSITE_UNIT_PROPERTIES, mapOfProperties);
        }
        for (int i = 1; i <= n; i++) {
            // verify
            SessionBroker clientSessionBroker;
            if (isOnServer()) {
                clientSessionBroker = (SessionBroker) ((EntityManagerImpl) em.getDelegate()).getActivePersistenceContext(null).getParent();
            } else {
                clientSessionBroker = (SessionBroker) ((EntityManagerImpl) em).getActivePersistenceContext(null).getParent();
            }
            ClientSession clientSession = (ClientSession) clientSessionBroker.getSessionForName(getCompositeMemberPuName(i));
            if (!clientSession.isExclusiveIsolatedClientSession()) {
                errorMsg += "ExclusiveIsolatedClientSession was expected\n";
            }
            ConnectionPolicy policy = clientSession.getConnectionPolicy();
            if (policy.isPooled()) {
                errorMsg += "NOT pooled policy was expected\n";
            }
            String user = (String) policy.getLogin().getProperty("user");
            String prefix = "em_" + i + "_";
            if (!user.equals(prefix + "user")) {
                errorMsg += prefix + "user was expected\n";
            }
            String password = (String) policy.getLogin().getProperty("password");
            if (!password.equals(prefix + "password")) {
                errorMsg += prefix + "password was expected\n";
            }
            if (!(((DatasourceLogin) policy.getLogin()).getConnector() instanceof JNDIConnector)) {
                errorMsg += "JNDIConnector was expected\n";
            } else {
                JNDIConnector jndiConnector = (JNDIConnector) ((DatasourceLogin) policy.getLogin()).getConnector();
                String dataSourceName = jndiConnector.getName();
                if (dataSourceName == null) {
                    errorMsg += "NON null dataSourceName was expected\n";
                } else {
                    if (clientSession.getParent().getLogin().shouldUseExternalTransactionController()) {
                        if (dataSourceName.equals(prefix + "nonjta_datasource")) {
                            errorMsg += prefix + "jta_datasource was expected\n";
                        }
                    } else {
                        if (dataSourceName.equals(prefix + "jta_datasource")) {
                            errorMsg += prefix + "jta_datasource was expected\n";
                        }
                    }
                }
            }
        }
    } finally {
        // clean-up
        if (isInTransaction) {
            rollbackTransaction(em);
        }
        if (em != null) {
            closeEntityManager(em);
        }
    }
    if (errorMsg.length() > 0) {
        fail(errorMsg);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) HashMap(java.util.HashMap) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) ClientSession(org.eclipse.persistence.sessions.server.ClientSession) SessionBroker(org.eclipse.persistence.sessions.broker.SessionBroker) JNDIConnector(org.eclipse.persistence.sessions.JNDIConnector) ConnectionPolicy(org.eclipse.persistence.sessions.server.ConnectionPolicy) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ClientSession (org.eclipse.persistence.sessions.server.ClientSession)8 EntityManager (jakarta.persistence.EntityManager)3 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)3 ServerSession (org.eclipse.persistence.sessions.server.ServerSession)3 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)2 Connection (java.sql.Connection)2 HashMap (java.util.HashMap)2 EntityManagerImpl (org.eclipse.persistence.internal.jpa.EntityManagerImpl)2 JpaEntityManagerFactory (org.eclipse.persistence.jpa.JpaEntityManagerFactory)2 JNDIConnector (org.eclipse.persistence.sessions.JNDIConnector)2 SessionBroker (org.eclipse.persistence.sessions.broker.SessionBroker)2 ConnectionPolicy (org.eclipse.persistence.sessions.server.ConnectionPolicy)2 Employee (org.eclipse.persistence.testing.models.employee.domain.Employee)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Expression (org.eclipse.persistence.expressions.Expression)1 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)1 Accessor (org.eclipse.persistence.internal.databaseaccess.Accessor)1 ExclusiveIsolatedClientSession (org.eclipse.persistence.internal.sessions.ExclusiveIsolatedClientSession)1 UnitOfWorkImpl (org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)1