Search in sources :

Example 1 with EmulatedConnection

use of org.eclipse.persistence.testing.tests.junit.failover.emulateddriver.EmulatedConnection in project eclipselink by eclipse-ee4j.

the class FailoverBase method prepare.

@Before
public void prepare() {
    DatabaseLogin login = new DatabaseLogin();
    login.useDirectDriverConnect();
    login.setDriverClass(EmulatedDriver.class);
    login.setConnectionString("jdbc:emulateddriver");
    login.getPlatform().setPingSQL("SELECT 1");
    Project p = new Project(login);
    ClassDescriptor cd = Address.descriptor();
    p.addDescriptor(cd);
    session = createSession(p);
    SessionLog log = new DefaultSessionLog(new OutputStreamWriter(System.out));
    int logLevel = AbstractSessionLog.translateStringToLoggingLevel(System.getProperty(PersistenceUnitProperties.LOGGING_LEVEL, "INFO"));
    session.setSessionLog(log);
    session.setLogLevel(logLevel);
    session.login();
    // this will actually store the results on the driver for subsequent connections.
    EmulatedConnection con = (EmulatedConnection) ((DatabaseSessionImpl) session).getAccessor().getConnection();
    Vector<DatabaseField> pingFields = new Vector<DatabaseField>() {

        {
            add(new DatabaseField("1"));
        }
    };
    con.putRows("SELECT 1", new Vector() {

        {
            add(new ArrayRecord(pingFields, pingFields.toArray(new DatabaseField[0]), new Object[] { "1" }));
        }
    });
    con.putRows(Address.getSQL(), Address.getData(cd));
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) EmulatedConnection(org.eclipse.persistence.testing.tests.junit.failover.emulateddriver.EmulatedConnection) ArrayRecord(org.eclipse.persistence.internal.sessions.ArrayRecord) DatabaseLogin(org.eclipse.persistence.sessions.DatabaseLogin) Project(org.eclipse.persistence.sessions.Project) AbstractSessionLog(org.eclipse.persistence.logging.AbstractSessionLog) DefaultSessionLog(org.eclipse.persistence.logging.DefaultSessionLog) SessionLog(org.eclipse.persistence.logging.SessionLog) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DefaultSessionLog(org.eclipse.persistence.logging.DefaultSessionLog) OutputStreamWriter(java.io.OutputStreamWriter) Vector(java.util.Vector) Before(org.junit.Before)

Example 2 with EmulatedConnection

use of org.eclipse.persistence.testing.tests.junit.failover.emulateddriver.EmulatedConnection in project eclipselink by eclipse-ee4j.

the class ConnectionPoolFailoverTest method connectionPoolFailureTest.

@Test
public void connectionPoolFailureTest() {
    List<Accessor> connections = new ArrayList<>();
    // prime connection pools.
    for (int i = 0; i < 10; ++i) {
        connections.add(getEmulatedSession().getConnectionPool("default").acquireConnection());
    }
    for (Accessor accessor : connections) {
        getEmulatedSession().getConnectionPool("default").releaseConnection(accessor);
    }
    List<Accessor> list = getEmulatedSession().getReadConnectionPool().getConnectionsAvailable();
    for (Accessor accessor : list) {
        ((EmulatedConnection) accessor.getConnection()).causeCommError();
    }
    for (int i = 0; i < 4; ++i) {
        try {
            getEmulatedSession().acquireClientSession().readObject(Address.class);
        } catch (DatabaseException ex) {
            Assert.fail("Should have reconnected an not thrown exception.");
        }
    }
    connections = new ArrayList<Accessor>();
    // prime connection pools.
    for (int i = 0; i < 10; ++i) {
        connections.add(getEmulatedSession().getConnectionPool("default").acquireConnection());
    }
    for (Accessor accessor : connections) {
        getEmulatedSession().getConnectionPool("default").releaseConnection(accessor);
    }
    list = getEmulatedSession().getReadConnectionPool().getConnectionsAvailable();
    for (Accessor accessor : list) {
        ((EmulatedConnection) accessor.getConnection()).causeCommError();
    }
    for (int i = 0; i < 4; ++i) {
        try {
            ReadObjectQuery query = new ReadObjectQuery(Address.class);
            query.setQueryTimeout(10000);
            getEmulatedSession().acquireClientSession().executeQuery(query);
        } catch (DatabaseException ex) {
            if (i != 0) {
                Assert.fail("Should have reconnected and not thrown exception.");
            }
        }
    }
}
Also used : EmulatedConnection(org.eclipse.persistence.testing.tests.junit.failover.emulateddriver.EmulatedConnection) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) ArrayList(java.util.ArrayList) Accessor(org.eclipse.persistence.internal.databaseaccess.Accessor) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) Test(org.junit.Test)

Example 3 with EmulatedConnection

use of org.eclipse.persistence.testing.tests.junit.failover.emulateddriver.EmulatedConnection in project eclipselink by eclipse-ee4j.

the class ConnectionPoolFailoverTest method fullDatabaseFailureTest.

@Test
public void fullDatabaseFailureTest() {
    try {
        List<Accessor> list = getEmulatedSession().getReadConnectionPool().getConnectionsAvailable();
        for (Accessor accessor : list) {
            ((EmulatedConnection) accessor.getConnection()).causeCommError();
        }
        EmulatedDriver.fullFailure = true;
        getEmulatedSession().acquireClientSession().readObject(Address.class);
    } catch (DatabaseException ex) {
        // Exception expected
        return;
    } finally {
        EmulatedDriver.fullFailure = false;
    }
    Assert.fail("Should have thrown exception as database connection is unavailable.");
}
Also used : EmulatedConnection(org.eclipse.persistence.testing.tests.junit.failover.emulateddriver.EmulatedConnection) Accessor(org.eclipse.persistence.internal.databaseaccess.Accessor) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) Test(org.junit.Test)

Aggregations

EmulatedConnection (org.eclipse.persistence.testing.tests.junit.failover.emulateddriver.EmulatedConnection)3 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)2 Accessor (org.eclipse.persistence.internal.databaseaccess.Accessor)2 Test (org.junit.Test)2 OutputStreamWriter (java.io.OutputStreamWriter)1 ArrayList (java.util.ArrayList)1 Vector (java.util.Vector)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)1 ArrayRecord (org.eclipse.persistence.internal.sessions.ArrayRecord)1 DatabaseSessionImpl (org.eclipse.persistence.internal.sessions.DatabaseSessionImpl)1 AbstractSessionLog (org.eclipse.persistence.logging.AbstractSessionLog)1 DefaultSessionLog (org.eclipse.persistence.logging.DefaultSessionLog)1 SessionLog (org.eclipse.persistence.logging.SessionLog)1 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)1 DatabaseLogin (org.eclipse.persistence.sessions.DatabaseLogin)1 Project (org.eclipse.persistence.sessions.Project)1 Before (org.junit.Before)1