Spring Boot + IntelliJ + Embedded Database = Headache

前端 未结 5 1649
执笔经年
执笔经年 2020-12-29 05:05

Either I\'m missing some core concept buried deep within some documentation (Spring, Spring Boot, H2, HSQLDB, Derby, IntelliJ) or I\'ve been staring at this for too long.

相关标签:
5条回答
  • 2020-12-29 05:49

    Using the example here to expose an in-memory DB via console and TCP I was able to connect using H2 Console and IntelliJ client as per screenshots.

    https://stackoverflow.com/a/52949164/2930427

    Connect using IntelliJ - jdbc:h2:tcp://localhost:9092/mem:default

    Connect using H2 Console: jdbc:h2:mem:default

    Example application.yml

    spring:
      application:
        name: example-service
      r2dbc:
        url: r2dbc:pool:h2:mem:///default?options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
        username: testuser
        password: testpass
        pool:
          initial-size: 100
          max-size: 500
          max-idle-time: 30m
          validation-query: SELECT 1
    
    0 讨论(0)
  • 2020-12-29 06:06

    H2 Automatic Mixed Mode should be fine for you. Use jdbc:h2:~/mydbInHomeDir;AUTO_SERVER=TRUE as your spring.datasource.url. In IntelliJ, create a remote H2 data source and use the exact same JDBC URL. You may have to explicitly press the Synchronize button in the IntelliJ Database window to get the database tables to show up.

    0 讨论(0)
  • 2020-12-29 06:06

    I had similar problem. It was due to the default create-drop ddl strategy of the hibernate. With this strategy after the application shutdown hibernate destroys the schema at the end of the session, that's why IntelliJ don't show anything. Change ddl strategy to the create and hibernate will create the schema and destroy previous data on the next application startup.

    Here is an example of my configuration:

    application.yml

    spring:
      datasource.url: jdbc:h2:./db/testDb
      jpa.hibernate.ddl-auto: create
    

    IntelliJ database properties

    Result

    0 讨论(0)
  • 2020-12-29 06:07

    If you follow the steps in this article: https://techdev.io/en/developer-blog/querying-the-embedded-h2-database-of-a-spring-boot-application

    I think it will provide the help in getting a Spring Boot application with H2 in-memory database exposed via a tcp server such that you can use the IntelliJ database client to connect to it.

    0 讨论(0)
  • 2020-12-29 06:09

    To add to what heenenee mentioned above. If you dont specify AUTO_SERVER only one connection will be permitted to your H2 instance.

    I am using spring-boot with spring-data-jpa. Make sure you have @Entity declared for your entities that represent each table(s).

    Following is my application.yml / application.properties

    spring.datasource.url: 
    jdbc:h2:file:/Users/blah[![enter image description here][1]][1]/db/vlad4;AUTO_SERVER=TRUE
    spring.datasource.username: sa
    spring.datasource.password:
    
    spring:
      jpa:
        hibernate:
          ddl-auto: create #will create schema based on entities
        show-sql: true
    

    Start your application and import some data into it. Spring boot will automatically import your data if you have import.sql in the classpath ex: /src/main/resources/import.sql

    Configure you IntelliJ like so

    If you are not using IntelliJ download the server/client combo @ http://www.h2database.com/html/download.html extract it and start the browser-based client using:

    h2/bin: java -cp h2*.jar org.h2.tools.Server
    

    Connect to your imbedded database by specifying the connection string:

    0 讨论(0)
提交回复
热议问题