【问题标题】:Error while running a spring application( org.h2.jdbc.JdbcSQLNonTransientConnectionException: URL format error)运行 spring 应用程序时出错(org.h2.jdbc.JdbcSQLNonTransientConnectionException: URL 格式错误)
【发布时间】:2020-06-28 18:14:03
【问题描述】:

我正在尝试使用 h2 嵌入式持久数据库。

并在运行应用程序时收到以下错误-

    2020-03-17 21:12:59.811  INFO 6204 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
    2020-03-17 21:13:00.868 ERROR 6204 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.

    org.h2.jdbc.JdbcSQLNonTransientConnectionException: URL format error; must be "jdbc:h2:{ {.|mem:}[name] | [file:]fileName | {tcp|ssl}:[//]server[:port][,server2[:port]]/name }[;key=value...]" but is "jdbc:h2:file:C:/data/sample" [90046-200]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:622) ~[h2-1.4.200.jar:1.4.200]
        at ...

我的应用程序.properties

spring.datasource.url=jdbc:h2:file:C:/data/sample;SET CACHE_SIZE 8192;DB_CLOSE_ON_EXIT=TRUE;AUTO_RECONNECT=TRUE

spring.datasource.driverClassName=org.h2.Driver

spring.datasource.username=sa

spring.datasource.password=

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.h2.console.enabled=true

【问题讨论】:

  • 您的错误显示给定的 URL 是 jdbc:h2:/data/sample 而不是 jdbc:h2:file:C:/data/sample
  • 我已经改进了这个问题,当我使用 'jdbc:h2:file:C:/data/sample' 时我仍然遇到同样的问题
  • 好像你的新网址现在有双冒号 (::)
  • 也修复了这个问题。仍然得到同样的错误。我错过了什么吗?

标签: java spring web-applications h2


【解决方案1】:

您需要将SET CACHE SIZE 8192; 替换为CACHE_SIZE=8192;

jdbc:h2:file:C:/data/sample;CACHE_SIZE=8192;DB_CLOSE_ON_EXIT=TRUE;AUTO_RECONNECT=TRUE

【讨论】:

  • 在这种情况下,我收到以下错误 - Web 应用程序 [ROOT] 似乎已经启动了一个名为 [MVStore background writer nio:C:/data/sample.mv.db] 的线程,但有未能阻止它。这很可能造成内存泄漏。线程堆栈跟踪:
  • 这个问题与你的问题没有任何关系。如果您在应用程序的类路径中使用带有 H2 的 Web 应用程序中的嵌入式数据库,则需要添加带有 @WebListener 注释的 ServletContextListener 的实现,并在其 contextDestroyed() 方法中执行数据库中的 SHUTDOWN 命令直接使用 JDBC (DriverManager.getConnection(url, user, password).createStatement().execute("SHUTDOWN")) 或以某种方式通过 Hibernate。
【解决方案2】:

它在application.properties 中使用以下内容:

spring.datasource.url=jdbc:h2:file:${user.dir}/data/sample;CACHE_SIZE=8192;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE

【讨论】:

    猜你喜欢
    • 2015-04-18
    • 2017-02-18
    • 2013-12-29
    • 2021-06-17
    • 2022-06-24
    • 1970-01-01
    相关资源
    最近更新 更多