Question

I'm using H2 database as the persistence layer for unit testing an application. I'm using H2 as an in-memory database.

The unit tests load a large volume of data into a single transactions table (the database is created with only this table in it). There is a thread that creates, updates and deletes rows based on some defined processing rules. The operations are sequential and work well, at full execution speed.

I then added another thread that reads from the table the number of records that were inserted (it's just a select count(*) from transactions) to see where I'm at with the data load and H2 started spiting out an exception with TABLE_OR_VIEW_NOT_FOUND_1 (42102 error code). The table it cannot find is the transactions table. I'm using a JdbcConnectionPool to get two connections to the database.

I know the H2 database engine is single threaded but my understanding is that it locks the entire table for concurrent operations witch means the reads will be blocked by writes and the other way around but not get a dumb error like that.

Has anyone encountered this? What can be the cause?

Was it helpful?

Solution

Depending on the jdbc url, H2 in in-memory mode can create a new private db with each new connection (ex jdbc:h2:mem:), or a db shared by many connections in the same VM using the same URL (ex jdbc:h2:mem:db1).

If you're using the private db (former) URL, could be that the 2nd connection is creating a new db which does not have the table definition created by the first connection?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top