Kristof
Kristof

Reputation: 445

H2 connection fails

I'm trying to connect to a local H2 database, using the simple code :

    Class.forName("org.h2.Driver");
    Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");

On the other hand, I also connect to a distant Oracle server with its own jdbc driver. In my understanding, both can live together since the jdbc drivers are different and the correct one is used depending on the connection url. So this should not be the source of my problem.

It fails with the following error :

org.h2.jdbc.JdbcSQLException: File corrupted while reading record: "[16] stream data key:7 pos:11 remaining:0". Possible solution: use the recovery tool [90030-190]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
    at org.h2.message.DbException.get(DbException.java:179)
    at org.h2.message.DbException.get(DbException.java:155)
    at org.h2.index.PageBtreeIndex.getPage(PageBtreeIndex.java:156)
    at org.h2.index.PageBtreeIndex.<init>(PageBtreeIndex.java:69)
    at org.h2.table.RegularTable.addIndex(RegularTable.java:234)
    at org.h2.store.PageStore.addMeta(PageStore.java:1738)
    at org.h2.store.PageStore.readMetaData(PageStore.java:1637)
    at org.h2.store.PageStore.recover(PageStore.java:1405)
    at org.h2.store.PageStore.openExisting(PageStore.java:367)
    at org.h2.store.PageStore.open(PageStore.java:288)
    at org.h2.engine.Database.getPageStore(Database.java:2464)
    at org.h2.engine.Database.open(Database.java:672)
    at org.h2.engine.Database.openDatabase(Database.java:269)
    at org.h2.engine.Database.<init>(Database.java:263)
    at org.h2.engine.Engine.openSession(Engine.java:65)
    at org.h2.engine.Engine.openSession(Engine.java:175)
    at org.h2.engine.Engine.createSessionAndValidate(Engine.java:153)
    at org.h2.engine.Engine.createSession(Engine.java:136)
    at org.h2.engine.Engine.createSession(Engine.java:28)
    at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:349)
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:107)
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:91)
    at org.h2.Driver.connect(Driver.java:72)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)

I can still connect using the web console that runs on port 8082.

Upvotes: 4

Views: 6578

Answers (2)

Mincong Huang
Mincong Huang

Reputation: 5552

According to H2 Features • Database URL Overview, there're many available URL expressions, here're some examples :

  • Embedded (local) connection:
    • jdbc:h2:~/test
    • jdbc:h2:file:/data/sample
    • jdbc:h2:file:C:/data/sample (Windows only)
  • In-memory (named):
    • jdbc:h2:mem:test_mem
  • Server mode (remote connections) using TCP/IP:
    • jdbc:h2:tcp://localhost/~/test
    • jdbc:h2:tcp://dbserv:8084/~/sample
    • jdbc:h2:tcp://localhost/mem:test
  • ...

There're many others, please check the above link to see the complete list.

Upvotes: 0

Kristof
Kristof

Reputation: 445

Well, I found out how to solve my problem : change the connection url to

"jdbc:h2:tcp://localhost/~/test"

Upvotes: 3

Related Questions