spring boot - Error connecting to embedded DB with DBeaver - Stack Overflow

admin2025-04-17  20

I'm using SpringBoot 3.4.1, Java 21.0.5, H2 database 2.3.232. follow my application.properties:

spring.datasource.url=jdbc:h2:mem:rest;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE;
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.h2.console.enabled=true

spring.jpa.hibernate.ddl-auto=create

I have just two JPA entity and when I run my app the DB is created and I can see it (and its tables) from

http://localhost:8080/h2-console

Now the web console works fine but I need DBeaver (24.3.2) to connect my DB. I tryed several ways from DBeaver DBMS menu:

With the H2 Embedded v1/v2 it creates a fresh db and is not my case.
With the H2 Server I obtain the follow errors:

with the default port 9092. If I switch to port 8080 I obtain a different error (but still an error):

After one hour trying the only way I found to make it work is: declaring a @Bean like follows:

@Bean(initMethod = "start", destroyMethod = "stop")
public Server h2Server() throws SQLException {
    return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
}

start the app, choose H2 Server from DBeaver menu and insert the follows parameter:

url: jdbc:h2:tcp://localhost:9092/mem:rest
user: sa
pass:

Is there a way to avoid the @Bean declaration and do something similar inside my application properties? I never had to declare config @Bean with all others DBMS like PostgreSQL, Oracle, SQLServer and so on.

I also tryed to downgrade the H2 Database version to 2.2.224 and putting in my .properties:

spring.datasource.url=jdbc:h2:file:D:/h2-dbms/rest-api/restapidb;FILE_LOCK=NO;

but nothing worked.

I'm using SpringBoot 3.4.1, Java 21.0.5, H2 database 2.3.232. follow my application.properties:

spring.datasource.url=jdbc:h2:mem:rest;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE;
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.h2.console.enabled=true

spring.jpa.hibernate.ddl-auto=create

I have just two JPA entity and when I run my app the DB is created and I can see it (and its tables) from

http://localhost:8080/h2-console

Now the web console works fine but I need DBeaver (24.3.2) to connect my DB. I tryed several ways from DBeaver DBMS menu:

With the H2 Embedded v1/v2 it creates a fresh db and is not my case.
With the H2 Server I obtain the follow errors:

with the default port 9092. If I switch to port 8080 I obtain a different error (but still an error):

After one hour trying the only way I found to make it work is: declaring a @Bean like follows:

@Bean(initMethod = "start", destroyMethod = "stop")
public Server h2Server() throws SQLException {
    return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
}

start the app, choose H2 Server from DBeaver menu and insert the follows parameter:

url: jdbc:h2:tcp://localhost:9092/mem:rest
user: sa
pass:

Is there a way to avoid the @Bean declaration and do something similar inside my application properties? I never had to declare config @Bean with all others DBMS like PostgreSQL, Oracle, SQLServer and so on.

I also tryed to downgrade the H2 Database version to 2.2.224 and putting in my .properties:

spring.datasource.url=jdbc:h2:file:D:/h2-dbms/rest-api/restapidb;FILE_LOCK=NO;

but nothing worked.

Share Improve this question edited Feb 1 at 7:58 CoderJammer asked Jan 31 at 21:37 CoderJammerCoderJammer 7454 gold badges11 silver badges39 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I solved this way. First of all I think that in-memory (jdbc:h2:mem:db) doesn't work with newer version of DBbeaver (I use the 24.3.2) beacause even if you open the Embedded Menu and click on H2 Embedded or H2 Embedded V2 you will se the follow suggestion for your URL to put:

And it seems refer to a file.

Anyway, for now, the configuration I put in my SpringBoot 3.4.1 is the follows:

spring.datasource.url=jdbc:h2:file:C:/h2-dbms/rest-api/restapidb;AUTO_SERVER=TRUE; // this is added to avoid file locking
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.h2.console.enabled=true
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

SpringBoot 3.4.1 bring with him the H2 2.3.232 version wich is newer than DBeaver one for the H2 Embedded DB. Therefore you must open DBbeaver -> Database -> Driver Manager: write H2 in the top search pane and search -> select H2 Embedded v2 -> Edit -> Libraries: remove the old .jar and add the new one (you can easily get it from the .m2 folder) -> Click OK.

After that, reopen H2 Embedded V2 and selecting:

Connected By -> URL
JDBC URL       -> jdbc:h2:file:C:/h2-dbms/rest-api/restapidb;AUTO_SERVER=TRUE;

Test it and, if everything goes fine, enjoy.

Hope helps.

转载请注明原文地址:http://anycun.com/QandA/1744845623a88427.html