Search in sources :

Example 16 with JNDIConnector

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

the class SessionExchanger method cloneAndSetDataSource.

DatabaseLogin cloneAndSetDataSource(DatabaseLogin login, boolean useExternalConnectionPooling, int minConnections, int maxConnections) {
    DatabaseLogin cloneLogin = (DatabaseLogin) login.clone();
    if (useExternalConnectionPooling) {
        createDataSource(login.getConnectionString(), minConnections, maxConnections);
        cloneLogin.setConnector(new JNDIConnector(dataSource));
        cloneLogin.setUsesExternalConnectionPooling(true);
    }
    return cloneLogin;
}
Also used : DatabaseLogin(org.eclipse.persistence.sessions.DatabaseLogin) JNDIConnector(org.eclipse.persistence.sessions.JNDIConnector)

Example 17 with JNDIConnector

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

the class EntityManagerJUnitTestSuite method testGenerateSessionNameFromConnectionProperties.

public void testGenerateSessionNameFromConnectionProperties() {
    // the test requires passing properties to createEMF or createContainerEMF method.
    if (isOnServer()) {
        return;
    }
    String errorMsg = "";
    EntityManagerFactory emf;
    Map properties;
    String puName = "default";
    String customizer = "org.eclipse.persistence.testing.tests.jpa.advanced.EntityManagerJUnitTestSuite$SessionNameCustomizer";
    String myJtaDS = "myJtaDS";
    String myNonJtaDS = "myNonJtaDS";
    String myUrl = "myUrl";
    String myUser = "myUser";
    String myDriver = "myDriver";
    String myPassword = "myPassword";
    ServerSession originalSession = JUnitTestCase.getServerSession();
    String loggingLevel = originalSession.getSessionLog().getLevelString();
    SessionNameCustomizer.ss = null;
    // 0: The session name specified in persistence.xml is NOT overridden -> the existing session is used by emf.
    // If persistence.xml no longer specifies session name then comment out this part of the test.
    properties = new HashMap();
    // required by the test
    properties.put(PersistenceUnitProperties.SESSION_CUSTOMIZER, customizer);
    // log on the same level as original session
    properties.put(PersistenceUnitProperties.LOGGING_LEVEL, loggingLevel);
    properties.put(PersistenceUnitProperties.TRANSACTION_TYPE, "RESOURCE_LOCAL");
    // to override data source if one is specified in persistence.xml
    properties.put(PersistenceUnitProperties.JTA_DATASOURCE, "");
    // to override data source if one is specified in persistence.xml
    properties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, "");
    properties.put(PersistenceUnitProperties.JDBC_URL, myUrl);
    properties.put(PersistenceUnitProperties.JDBC_USER, myUser);
    properties.put(PersistenceUnitProperties.JDBC_DRIVER, myDriver);
    properties.put(PersistenceUnitProperties.JDBC_PASSWORD, myPassword);
    emf = Persistence.createEntityManagerFactory(puName, properties);
    try {
        emf.createEntityManager();
    } catch (Exception ex) {
    // Ignore exception that probably caused by attempt to connect the session with invalid connection data provided in the properties.
    } finally {
        emf.close();
        if (SessionNameCustomizer.ss != null && SessionNameCustomizer.ss != originalSession) {
            errorMsg += "0: Session name NOT overridden by an empty string - original session expected; ";
        }
        // clear for the next test
        SessionNameCustomizer.ss = null;
    }
    // 1: New session with DefaultConnector with myUrl, myDriver, myUser, myPassword
    properties = new HashMap();
    // required by the test
    properties.put(PersistenceUnitProperties.SESSION_CUSTOMIZER, customizer);
    // log on the same level as original session
    properties.put(PersistenceUnitProperties.LOGGING_LEVEL, loggingLevel);
    // to override SESSION_NAME if one is specified in persistence.xml
    properties.put(PersistenceUnitProperties.SESSION_NAME, "");
    properties.put(PersistenceUnitProperties.TRANSACTION_TYPE, "RESOURCE_LOCAL");
    // to override data source if one is specified in persistence.xml
    properties.put(PersistenceUnitProperties.JTA_DATASOURCE, "");
    // to override data source if one is specified in persistence.xml
    properties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, "");
    properties.put(PersistenceUnitProperties.JDBC_URL, myUrl);
    properties.put(PersistenceUnitProperties.JDBC_USER, myUser);
    properties.put(PersistenceUnitProperties.JDBC_DRIVER, myDriver);
    properties.put(PersistenceUnitProperties.JDBC_PASSWORD, myPassword);
    emf = Persistence.createEntityManagerFactory(puName, properties);
    try {
        emf.createEntityManager();
    } catch (Exception ex) {
    // Ignore exception that probably caused by attempt to connect the session with invalid connection data provided in the properties.
    } finally {
        emf.close();
        if (SessionNameCustomizer.ss == null && SessionNameCustomizer.ss == originalSession) {
            errorMsg += "1: New session expected; ";
        } else {
            if (SessionNameCustomizer.ss.getLogin().shouldUseExternalConnectionPooling()) {
                errorMsg += "1: internal connection pooling expected; ";
            }
            if (SessionNameCustomizer.ss.getLogin().shouldUseExternalTransactionController()) {
                errorMsg += "1: no externalTransactionController expected; ";
            }
            if (!myUser.equals(SessionNameCustomizer.ss.getLogin().getUserName())) {
                errorMsg += "1: myUser expected; ";
            }
            Connector connector = SessionNameCustomizer.ss.getLogin().getConnector();
            if (connector instanceof DefaultConnector) {
                if (!myUrl.equals(((DefaultConnector) connector).getDatabaseURL())) {
                    errorMsg += "1: myUrl expected; ";
                }
                if (!myDriver.equals(((DefaultConnector) connector).getDriverClassName())) {
                    errorMsg += "1: myDriver expected; ";
                }
            } else {
                errorMsg += "1: DefaultConnector expected; ";
            }
        }
        // clear for the next test
        SessionNameCustomizer.ss = null;
    }
    // 2: New session with JNDIConnector with myJtaDs, myNonJtaDs
    properties = new HashMap();
    // required by the test
    properties.put(PersistenceUnitProperties.SESSION_CUSTOMIZER, customizer);
    // log on the same level as original session
    properties.put(PersistenceUnitProperties.LOGGING_LEVEL, loggingLevel);
    // to override SESSION_NAME if one is specified in persistence.xml
    properties.put(PersistenceUnitProperties.SESSION_NAME, "");
    properties.put(PersistenceUnitProperties.TRANSACTION_TYPE, "JTA");
    properties.put(PersistenceUnitProperties.JTA_DATASOURCE, myJtaDS);
    properties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, myNonJtaDS);
    // to override user if one is specified in persistence.xml
    properties.put(PersistenceUnitProperties.JDBC_USER, "");
    // note that there is no need to override JDBC_URL, JDBC_DRIVER, JDBC_PASSWORD with an empty string, they will be ignored.
    // JDBC_URL, JDBC_DRIVER - because data source(s) are specified; JDBC_PASSWORD - because JDBC_USER is not specified
    emf = Persistence.createEntityManagerFactory(puName, properties);
    try {
        emf.createEntityManager();
    } catch (Exception ex) {
    // Ignore exception that probably caused by attempt to connect the session with invalid connection data provided in the properties.
    } finally {
        emf.close();
        if (SessionNameCustomizer.ss == null && SessionNameCustomizer.ss == originalSession) {
            errorMsg += "2: New session expected; ";
        } else {
            if (!SessionNameCustomizer.ss.getLogin().shouldUseExternalConnectionPooling()) {
                errorMsg += "2: external connection pooling expected; ";
            }
            if (!SessionNameCustomizer.ss.getLogin().shouldUseExternalTransactionController()) {
                errorMsg += "2: externalTransactionController expected; ";
            }
            if (SessionNameCustomizer.ss.getLogin().getUserName().length() > 0) {
                errorMsg += "2: empty string user expected; ";
            }
            Connector connector = SessionNameCustomizer.ss.getLogin().getConnector();
            if (connector instanceof JNDIConnector) {
                if (!myJtaDS.equals(((JNDIConnector) connector).getName())) {
                    errorMsg += "2: myJtaDS expected; ";
                }
            } else {
                errorMsg += "2: JNDIConnector expected; ";
            }
            if (!SessionNameCustomizer.ss.getReadConnectionPool().getLogin().shouldUseExternalConnectionPooling()) {
                errorMsg += "2: resding: external connection pooling expected; ";
            }
            if (SessionNameCustomizer.ss.getReadConnectionPool().getLogin().shouldUseExternalTransactionController()) {
                errorMsg += "2: reading no externalTransactionController expected; ";
            }
            if (SessionNameCustomizer.ss.getReadConnectionPool().getLogin().getUserName().length() > 0) {
                errorMsg += "2: reading: empty string user expected; ";
            }
            Connector readConnector = ((DatasourceLogin) SessionNameCustomizer.ss.getReadConnectionPool().getLogin()).getConnector();
            if (readConnector instanceof JNDIConnector) {
                if (!myNonJtaDS.equals(((JNDIConnector) readConnector).getName())) {
                    errorMsg += "2: reading: myNonJtaDS expected; ";
                }
            } else {
                errorMsg += "2: reading: JNDIConnector expected; ";
            }
        }
        // clear for the next test
        SessionNameCustomizer.ss = null;
    }
    if (errorMsg.length() > 0) {
        fail(errorMsg);
    }
}
Also used : Connector(org.eclipse.persistence.sessions.Connector) DefaultConnector(org.eclipse.persistence.sessions.DefaultConnector) JNDIConnector(org.eclipse.persistence.sessions.JNDIConnector) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) DatasourceLogin(org.eclipse.persistence.sessions.DatasourceLogin) HashMap(java.util.HashMap) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) JNDIConnector(org.eclipse.persistence.sessions.JNDIConnector) Map(java.util.Map) HashMap(java.util.HashMap) TestProblemException(org.eclipse.persistence.testing.framework.TestProblemException) ValidationException(org.eclipse.persistence.exceptions.ValidationException) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) EntityExistsException(jakarta.persistence.EntityExistsException) OptimisticLockException(jakarta.persistence.OptimisticLockException) QueryException(org.eclipse.persistence.exceptions.QueryException) TransactionRequiredException(jakarta.persistence.TransactionRequiredException) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException) EntityNotFoundException(jakarta.persistence.EntityNotFoundException) SQLException(java.sql.SQLException) RollbackException(jakarta.persistence.RollbackException) NoResultException(jakarta.persistence.NoResultException) PersistenceException(jakarta.persistence.PersistenceException) PersistenceUnitLoadingException(org.eclipse.persistence.exceptions.PersistenceUnitLoadingException) DefaultConnector(org.eclipse.persistence.sessions.DefaultConnector)

Example 18 with JNDIConnector

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

the class UCPCallbackUnitOfWorkTests method buildServerSession.

@Override
public Server buildServerSession() {
    try {
        PoolDataSource dataSource = PoolDataSourceFactory.getPoolDataSource();
        // dataSource.setONSConfiguration("nodes=adcdbc01-r.us.oracle.com:6200");
        dataSource.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
        dataSource.setURL(getSession().getLogin().getConnectionString());
        dataSource.setUser(getSession().getLogin().getUserName());
        dataSource.setPassword(getSession().getLogin().getPassword());
        dataSource.setInitialPoolSize(5);
        dataSource.setMinPoolSize(5);
        dataSource.setMaxPoolSize(10);
        dataSource.setFastConnectionFailoverEnabled(true);
        Project project = getSession().getProject().clone();
        project.setLogin(project.getLogin().clone());
        project.getLogin().setConnector(new JNDIConnector(dataSource));
        project.getLogin().setUsesExternalConnectionPooling(true);
        project.getLogin().setPartitioningCallback(new UCPDataPartitioningCallback());
        Server server = project.createServerSession();
        server.setPartitioningPolicy(new RoundRobinPartitioningPolicy("node1", "node2"));
        server.setSessionLog(getSession().getSessionLog());
        server.login();
        return server;
    } catch (SQLException error) {
        throw new TestErrorException("UCP failed to initialize.", error);
    }
}
Also used : Project(org.eclipse.persistence.sessions.Project) RoundRobinPartitioningPolicy(org.eclipse.persistence.descriptors.partitioning.RoundRobinPartitioningPolicy) Server(org.eclipse.persistence.sessions.server.Server) SQLException(java.sql.SQLException) UCPDataPartitioningCallback(org.eclipse.persistence.platform.database.oracle.ucp.UCPDataPartitioningCallback) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) JNDIConnector(org.eclipse.persistence.sessions.JNDIConnector) PoolDataSource(oracle.ucp.jdbc.PoolDataSource)

Example 19 with JNDIConnector

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

the class ProxyTestHelper method setProxyConnectorIntoLogin.

public void setProxyConnectorIntoLogin(DatabaseLogin login, Session session) throws SQLException {
    if (oracleDataSource != null) {
        return;
    }
    JNDIConnector connector = createProxyConnector(session);
    connector.setDataSource(oracleDataSource);
    login.setConnector(connector);
    login.setUsesExternalConnectionPooling(true);
}
Also used : JNDIConnector(org.eclipse.persistence.sessions.JNDIConnector)

Example 20 with JNDIConnector

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

the class UnwrapConnectionCustomSQLTestModel method setup.

@Override
public void setup() {
    if (!getSession().getPlatform().isOracle()) {
        throw new TestWarningException("WARNING: This model is not supposed to be run on databases other than Oracle.");
    }
    DatabaseSession session = (DatabaseSession) getSession();
    session.logout();
    // save the connector to restore later
    originalConnector = session.getLogin().getConnector();
    DataSource dataSource = new TestOracleDataSource(session.getLogin().getDriverClassName(), session.getLogin().getConnectionString(), (Properties) session.getLogin().getProperties().clone());
    session.getLogin().setConnector(new JNDIConnector(dataSource));
    originalServerPlatform = session.getServerPlatform();
    session.setServerPlatform(new TestServerPlatform(session));
    originalShouldUseExternalConnectionPooling = session.getLogin().shouldUseExternalConnectionPooling();
    session.getLogin().useExternalConnectionPooling();
    session.login();
    super.setup();
}
Also used : DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) JNDIConnector(org.eclipse.persistence.sessions.JNDIConnector) TestWarningException(org.eclipse.persistence.testing.framework.TestWarningException) DataSource(javax.sql.DataSource)

Aggregations

JNDIConnector (org.eclipse.persistence.sessions.JNDIConnector)23 HashMap (java.util.HashMap)7 Connector (org.eclipse.persistence.sessions.Connector)7 DefaultConnector (org.eclipse.persistence.sessions.DefaultConnector)6 EntityManager (jakarta.persistence.EntityManager)5 DataSource (javax.sql.DataSource)5 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)5 JpaEntityManagerFactory (org.eclipse.persistence.jpa.JpaEntityManagerFactory)5 DatasourceLogin (org.eclipse.persistence.sessions.DatasourceLogin)5 ServerSession (org.eclipse.persistence.sessions.server.ServerSession)5 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)4 SQLException (java.sql.SQLException)4 Map (java.util.Map)4 DatabaseSession (org.eclipse.persistence.sessions.DatabaseSession)4 ConnectionPolicy (org.eclipse.persistence.sessions.server.ConnectionPolicy)4 PersistenceException (jakarta.persistence.PersistenceException)3 ValidationException (org.eclipse.persistence.exceptions.ValidationException)3 DatabaseLogin (org.eclipse.persistence.sessions.DatabaseLogin)3 Project (org.eclipse.persistence.sessions.Project)3 OptimisticLockException (jakarta.persistence.OptimisticLockException)2