H2 - The handle is invalid

283 views Asked by At

not sure why this error occurs when running a select statement from my groovy console against an H2 instance in a different JVM (on the same host).

def sql = Sql.newInstance("jdbc:h2:~/mydb", "sa", "", "org.h2.Driver")
println sql.rows("select * from MESSAGES")

org.h2.jdbc.JdbcSQLException: IO Exception: "java.io.IOException: The handle is invalid"; "C:/Users/myhome/mydb.h2.db"; SQL statement:
select * from MESSAGES [90031-187]

    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)

    at org.h2.message.DbException.get(DbException.java:168)

    at org.h2.message.DbException.convertIOException(DbException.java:330)

    at org.h2.store.FileStore.seek(FileStore.java:297)

    at org.h2.store.PageStore.readPage(PageStore.java:1324)

    at org.h2.store.PageStore.getPage(PageStore.java:749)

    at org.h2.index.PageDataIndex.getPage(PageDataIndex.java:233)

    ....
1

There are 1 answers

0
ataylor On

To access an H2 database from multiple VMs, it must run in server mode. The easiest way to do this is with AUTO_SERVER mode. Simply use this URL in both processes: jdbc:h2:~/mydb;AUTO_SERVER=TRUE

In AUTO_SERVER mode, H2 is smart enough to start up a server in the first VM and connect to it in the second, allowing transparent concurrent access to the same database.