Search in sources :

Example 41 with JdbcDataSource

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();
    }
}
Also used : Server(org.h2.tools.Server) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) IOException(java.io.IOException) File(java.io.File) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 42 with JdbcDataSource

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();
    }
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) Test(org.junit.Test)

Example 43 with JdbcDataSource

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();
    }
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) Test(org.junit.Test)

Example 44 with JdbcDataSource

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();
    }
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) PooledDataSource(org.spf4j.pool.jdbc.PooledDataSource) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 45 with JdbcDataSource

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);
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Sampler(org.spf4j.stackmonitor.Sampler) File(java.io.File) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

JdbcDataSource (org.h2.jdbcx.JdbcDataSource)81 Connection (java.sql.Connection)24 Test (org.junit.Test)24 Before (org.junit.Before)15 XAResource (javax.transaction.xa.XAResource)13 DataSource (javax.sql.DataSource)9 Xid (javax.transaction.xa.Xid)9 SQLException (java.sql.SQLException)8 InitialContext (javax.naming.InitialContext)8 XAConnection (javax.sql.XAConnection)8 RecoveryModule (com.arjuna.ats.arjuna.recovery.RecoveryModule)7 CommitMarkableResourceRecordRecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule)7 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)7 XAResourceRecoveryHelper (com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)7 Statement (java.sql.Statement)7 BMScript (org.jboss.byteman.contrib.bmunit.BMScript)7 File (java.io.File)6 Enumeration (java.util.Enumeration)6 Properties (java.util.Properties)6 Vector (java.util.Vector)6