Total Pageviews

2021/01/08

[Spring Boot] Cannot insert data in h2 database

 Environment

  • JDK: 1.8
  • Spring Boot: 2.4.1
  • Database: H2
  • yaml configuration
spring:
  datasource:
    url: jdbc:h2:file:./database/test;DB_CLOSE_DELAY=-1
    driver-class-name: org.h2.Driver
    username: sa
    password: password
    schema: classpath:/sql/schema.sql
    data: classpath:/sql/data/*.sql
    hikari:
      connection-timeout: 30000
      idle-timeout: 600000
      max-lifetime: 1800000
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        show_sql: false
        use_sql_comments: true
        format_sql: true
  h2:
    console:
      enabled: true
      path: /console
      settings:
        trace: false
        web-allow-others: false


Problem

As I startup Spring Boot, it created table successfully, but fail to insert data.


How-To

Set initialization-mode to always in yaml, then this problem will be solved.

spring:
  datasource:
    initialization-mode: always
    url: jdbc:h2:file:./database/test;DB_CLOSE_DELAY=-1
    driver-class-name: org.h2.Driver
    username: sa
    password: password
    schema: classpath:/sql/schema.sql
    data: classpath:/sql/data/*.sql
    hikari:
      connection-timeout: 30000
      idle-timeout: 600000
      max-lifetime: 1800000
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        show_sql: false
        use_sql_comments: true
        format_sql: true
  h2:
    console:
      enabled: true
      path: /console
      settings:
        trace: false
        web-allow-others: false





No comments: