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));
}
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.");
}
}
}
}
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.");
}
Aggregations