Hi,

   I'm trying to unit test multiple threads interacting with an H2 database 
started as a TCP server and am not getting the results I expect.  I'm not 
quite sure if this is just a bad unit test or I'm having trouble with H2. 
 I'd appreciate some advice, if possible.

   I'm currently using H2 version: *1.3.169*

   Here's what I'm doing...

public class PfpInteractionDao {
public static final String CONNECTION_STRING = 
"jdbc:h2:%s;MV_STORE=FALSE;TRACE_LEVEL_FILE=0";
public static final String SERVER_CONNECTION_STRING = 
"jdbc:h2:tcp://localhost:%d/%s;MV_STORE=FALSE;TRACE_LEVEL_FILE=0";
private static final String DATASOURCE_NAME = "gpfp";
 protected SqlSessionFactory sqlSessionFactory;

protected Configuration configuration = null;

private static final Logger logger = 
LoggerFactory.getLogger(PfpInteractionDao.class);
 /**
 * Default interaction.  This constructor
 * should only be used for a direct connection.
 */
public PfpInteractionDao() {
    this(-1);
}

/**
 * TCP Connection.  This constructor should
 * be used for interacting with the datasource in
 * a client-server scenario.
 * @param port number to connect on.  Must match server port
 */
public PfpInteractionDao(int port) {
       //setup initial datasource
        JdbcDataSource datasource = new JdbcDataSource();
        datasource.setUrl(getConnectionString(port));
        datasource.setUser("sa");
        
        //initialize transaction factory & environment
        TransactionFactory transactionFactory = new 
JdbcTransactionFactory();
        Environment environment = new Environment("pd", transactionFactory, 
datasource);
        
        //initialize configuration, aliases, and mappers
        configuration = new Configuration(environment);
        configuration.addMapper(PfpInteraction.class);
        
        initialize();
}

The DAO is using MyBatis for ORM where the SqlSessionFactory is responsible 
for opening a session and executing queries.

public class TestTcpConnection {

    private static String args[] = { "-tcpPort", "15351" };
    
    private static Server server;
    
    private static final Logger logger = 
LoggerFactory.getLogger(TestTcpConnection.class);

    @BeforeClass
    public static void init() throws SQLException {
        //configure log4j
        PropertyConfigurator.configure("./deliverables/log4j.properties");

        //start the server
        server = Server.createTcpServer(args);
        server.start();
        
        logger.debug(server.getStatus());
    }

    @AfterClass
    public static void close() {
        server.stop();
    }

    @Test
    public void testTcpServer() throws SQLException, InterruptedException, 
IOException {
        //create a datasource connection
        PfpInteractionDao dao = new 
PfpInteractionDao(Integer.parseInt(args[1]));
        PfpEapg eapg = dao.getPfpEapg(1);
        assertNotNull(eapg);
    }
    
    @Test
    public void testConcurrentConnection() throws SQLException {
        //create and start thread 1
        new Thread(
                new MindlessQuery(new 
PfpInteractionDao(Integer.parseInt(args[1]))), 
                "Query-thread-1").start();
        //create and start thread 2
        new Thread(
                new MindlessQuery(new 
PfpInteractionDao(Integer.parseInt(args[1]))), 
                "Query-thread-2").start();
    }
}

Both tests are executing without any exceptions and providing 
somewhat-valid results.... The contents retrieved from the database are 
good for the query, but I'm never getting more then 1 result per thread. 
 The Mindless query Runnable is designed to execute a simple query 10000 
times; enough work to ensure both threads can take turns.

public class MindlessQuery implements Runnable {
    private PfpInteractionDao dao;
    private static final Logger logger = 
LoggerFactory.getLogger(MindlessQuery.class);
    public MindlessQuery(PfpInteractionDao dao) {
        this.dao = dao;
    }

    public void run() {
        for(int i = 0; i < 10000; i++) {
            PfpEapg eapg = dao.getPfpEapg(1);
            logger.debug("{}", eapg);
        }
    }
}

When running, the loops begins and successful retrieves the value I need, 
but only once.  I've tried to break on it, but even then debugger (in 
eclipse) just runs straight through; I've checked the "suspend thread" 
setting as well for eclipse and it's fine.

Is this a bogus test scenario?  Am I overlooking some H2 configuration?

My logged results are:

[DEBUG] TestTcpConnection:36 - TCP server running at 
tcp://143.122.1.181:15351 (only local connections)
[DEBUG] MindlessQuery:19 - PfpEapg{1, 2, 1, 0, 0, 0, 2, 2, 0, 2, 0}
[DEBUG] MindlessQuery:19 - PfpEapg{1, 2, 1, 0, 0, 0, 2, 2, 0, 2, 0}

Why am I only getting two results?  I'm expecting 20000 results 
intermingled between two threads.

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.

Reply via email to