简体   繁体   English

H2连接失败

[英]H2 connection fails

I'm trying to connect to a local H2 database, using the simple code : 我正在尝试使用简单的代码连接到本地H2数据库:

    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. 另一方面,我还使用自己的jdbc驱动程序连接到远程Oracle服务器。 In my understanding, both can live together since the jdbc drivers are different and the correct one is used depending on the connection url. 在我的理解中,由于jdbc驱动程序不同,并且根据连接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. 我仍然可以使用在端口8082上运行的Web控制台进行连接。

好吧,我找到了解决问题的方法:将连接网址更改为

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

According to H2 Features • Database URL Overview , there're many available URL expressions, here're some examples : 根据H2功能•数据库URL概述 ,有许多可用的URL表达式,以下是一些示例:

  • Embedded (local) connection: 嵌入式 (本地)连接:
    • jdbc:h2:~/test
    • jdbc:h2:file:/data/sample
    • jdbc:h2:file:C:/data/sample (Windows only) jdbc:h2:file:C:/data/sample (仅Windows)
  • In-memory (named): 内存中 (命名):
    • jdbc:h2:mem:test_mem
  • Server mode (remote connections) using TCP/IP: 使用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. 还有很多,请检查上面的链接以查看完整列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM