Search in sources :

Example 81 with JdbcDataSource

use of org.h2.jdbcx.JdbcDataSource in project spf4j by zolyfarkas.

the class BadSemaphoreHandler method main.

public static void main(final String[] args) throws InterruptedException, TimeoutException, SQLException {
    String connectionString = args[0];
    String semaphoreName = args[1];
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL(connectionString);
    ds.setUser("sa");
    ds.setPassword("sa");
    JdbcSemaphore semaphore = new JdbcSemaphore(ds, semaphoreName, 3);
    semaphore.acquire(1, 1L, TimeUnit.SECONDS);
    System.exit(0);
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource)

Example 82 with JdbcDataSource

use of org.h2.jdbcx.JdbcDataSource in project spf4j by zolyfarkas.

the class JdbcSemaphoreTest method testMultiProcess2.

@Test
public void testMultiProcess2() 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");
        tempDB.deleteOnExit();
        String connStr = "jdbc:h2:tcp://localhost:" + port + "/nio:" + tempDB.getAbsolutePath() + ";AUTO_SERVER=TRUE";
        JdbcDataSource ds = new JdbcDataSource();
        ds.setURL(connStr);
        ds.setUser("sa");
        ds.setPassword("sa");
        createSchemaObjects(ds);
        JdbcSemaphore semaphore = new JdbcSemaphore(ds, "test_sem2", 1, true);
        String o1 = org.spf4j.base.Runtime.jrun(DecentSemaphoreHandler.class, 10000000, connStr, "test_sem2").toString();
        String o2 = org.spf4j.base.Runtime.jrun(DecentSemaphoreHandler.class, 10000000, connStr, "test_sem2").toString();
        Assert.assertTrue(semaphore.tryAcquire(1, TimeUnit.SECONDS));
        Assert.assertFalse(semaphore.tryAcquire(10, TimeUnit.SECONDS));
        LOG.debug("P1: {}", o1);
        LOG.debug("P2: {}", o2);
        String[] nr1 = o1.split("\n");
        String[] nr2 = o2.split("\n");
        int totatl = nr1.length + nr2.length;
        Set<String> numbers = new HashSet<>(totatl);
        numbers.addAll(Arrays.asList(nr1));
        numbers.addAll(Arrays.asList(nr2));
        Assert.assertEquals(totatl, numbers.size());
    } finally {
        JdbcHeartBeat.stopHeartBeats();
        server.shutdown();
    }
}
Also used : Server(org.h2.tools.Server) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

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