use of org.h2.jdbcx.JdbcDataSource in project spf4j by zolyfarkas.
the class JdbcSemaphoreTest method testMultiProcess.
@Test
@SuppressFBWarnings("AFBR_ABNORMAL_FINALLY_BLOCK_RETURN")
public void testMultiProcess() throws SQLException, IOException, InterruptedException, ExecutionException, TimeoutException {
int port = PORT.getAndIncrement();
Server server = Server.createTcpServer(new String[] { "-tcpPort", Integer.toString(port), "-ifNotExists" }).start();
try {
File tempDB = File.createTempFile("test", "h2db");
String connStr = "jdbc:h2:tcp://localhost:" + port + "/nio:" + tempDB.getAbsolutePath() + ";AUTO_SERVER=TRUE";
try {
JdbcDataSource ds = new JdbcDataSource();
ds.setURL(connStr);
ds.setUser("sa");
ds.setPassword("sa");
createSchemaObjects(ds);
testReleaseAck(ds, "testSem", 2);
JdbcSemaphore semaphore = new JdbcSemaphore(ds, "test_sem2", 3);
org.spf4j.base.Runtime.jrun(BadSemaphoreHandler.class, 10000, connStr, "test_sem2");
org.spf4j.base.Runtime.jrun(BadSemaphoreHandler.class, 10000, connStr, "test_sem2");
Assert.assertTrue(semaphore.tryAcquire(1, TimeUnit.SECONDS));
Assert.assertTrue(semaphore.tryAcquire(10, TimeUnit.SECONDS));
} finally {
if (!tempDB.delete()) {
throw new IOException("Cannot delete " + tempDB);
}
}
} finally {
JdbcHeartBeat.stopHeartBeats();
server.shutdown();
}
}
use of org.h2.jdbcx.JdbcDataSource in project spf4j by zolyfarkas.
the class JdbcSemaphoreTest method testSingleMultipleInstance.
@Test(expected = IllegalStateException.class)
public void testSingleMultipleInstance() throws SQLException, IOException, InterruptedException, TimeoutException {
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:mem:test" + MEM_IDX.getAndIncrement() + ";DB_CLOSE_DELAY=-1");
ds.setUser("sa");
ds.setPassword("sa");
try (Connection conn = ds.getConnection()) {
// only to keep the schema arround in this section
createSchemaObjects(ds);
JdbcLock lock = new JdbcLock(ds, SemaphoreTablesDesc.DEFAULT, "testLock", 10);
JdbcLock lock2 = new JdbcLock(ds, SemaphoreTablesDesc.DEFAULT, "testLock", 10);
lock.lock();
Assert.assertFalse(lock.tryLock());
lock.unlock();
lock2.lock();
lock.unlock();
} finally {
JdbcHeartBeat.stopHeartBeats();
}
}
use of org.h2.jdbcx.JdbcDataSource in project spf4j by zolyfarkas.
the class JdbcSemaphoreTest method testSingleProcessLock.
@Test
public void testSingleProcessLock() throws SQLException, IOException, InterruptedException, TimeoutException {
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:mem:test" + MEM_IDX.getAndIncrement() + ";DB_CLOSE_DELAY=-1");
ds.setUser("sa");
ds.setPassword("sa");
createSchemaObjects(ds);
try (Connection conn = ds.getConnection()) {
// only to keep the schema arround in thsi section
JdbcLock lock = new JdbcLock(ds, SemaphoreTablesDesc.DEFAULT, "testLock", 10);
lock.lock();
Assert.assertFalse(lock.tryLock());
lock.unlock();
} finally {
JdbcHeartBeat.stopHeartBeats();
}
}
use of org.h2.jdbcx.JdbcDataSource in project spf4j by zolyfarkas.
the class JdbcSemaphoreTest method testSingleProcess.
@Test
@SuppressFBWarnings("AFBR_ABNORMAL_FINALLY_BLOCK_RETURN")
public void testSingleProcess() throws SQLException, IOException, InterruptedException, TimeoutException, ObjectCreationException, ObjectDisposeException {
JdbcDataSource hds = new JdbcDataSource();
hds.setURL("jdbc:h2:mem:test" + MEM_IDX.getAndIncrement() + ";DB_CLOSE_DELAY=-1");
hds.setUser("sa");
hds.setPassword("sa");
PooledDataSource ds = createPooledDS(hds);
createSchemaObjects(ds);
try (Connection conn = ds.getConnection()) {
// only to keep the schema arround in thsi section
JdbcHeartBeat heartbeat = JdbcHeartBeat.getHeartBeatAndSubscribe(ds, HeartBeatTableDesc.DEFAULT, (JdbcHeartBeat.LifecycleHook) null);
try {
long lb = heartbeat.getLastRunDB();
LOG.debug("last TS = {}", Instant.ofEpochMilli(lb));
heartbeat.beat();
testReleaseAck(ds, "testSem", 2);
testReleaseAck(ds, "testSem2", 2);
} finally {
heartbeat.close();
}
} finally {
ds.close();
}
}
use of org.h2.jdbcx.JdbcDataSource in project spf4j by zolyfarkas.
the class DecentSemaphoreHandler method main.
@SuppressFBWarnings("MDM_THREAD_YIELD")
public static void main(final String[] args) throws InterruptedException, TimeoutException, SQLException, IOException {
String connectionString = args[0];
String semaphoreName = args[1];
JdbcDataSource ds = new JdbcDataSource();
ds.setURL(connectionString);
ds.setUser("sa");
ds.setPassword("sa");
Sampler s = new Sampler(5, 10000);
s.registerJmx();
s.start();
LOG.info("started sampling");
JdbcSemaphore semaphore = new JdbcSemaphore(ds, semaphoreName, 3);
for (int i = 0; i < 50; i++) {
semaphore.acquire(1, 1L, TimeUnit.SECONDS);
Thread.sleep((long) (Math.random() * 10) + 10);
LOG.info("beat");
Thread.sleep((long) (Math.random() * 10) + 10);
semaphore.release();
}
semaphore.close();
File dumpToFile = s.dumpToFile();
LOG.info("stack samples dumped to {}", dumpToFile);
s.stop();
System.exit(0);
}
Aggregations