0

I am new in Spring. I am trying to work with H2 Database and JDBC (not JPA/Hibernate).

So with Spring Initializr, when i add theses dependencies :

  • spring-boot-starter-web
  • spring-boot-starter-data-jpa
  • h2

and putting this property to true : spring.h2.console.enabled=true

The database mem:testdb is created and i can connect to it at : localhost:8080/h2-console

But when i change the jpa dependency to :

  • spring-boot-starter-data-jdbc

The database is not created and I have this error message :

Database "mem:testdb" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-200] 90149/90149 (Help)

So I would like to know why it doesn't work with JDBC, and if you know a solution to make it work.

I found two articles on the web, and it seems they can have the database created with JDBC :

UPDATE :

I tried to do the same thing at home in my personal computer and it works...

I don't know why it doesn't work in job computer although it works with JPA.

In the logs, it is missing these lines :

com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...

com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.

o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testdb'

UPDATE2 :

I found the solution :

As I said, I am using Spring Initializr to bootstrap the project. In the job pc, I was not able to use the last version of Spring boot (2.2.4), so i choosed 2.1.12

Version Spring Boot

With this version, it works with JPA but not with JDBC...

1
  • Did you go through this Commented Feb 5, 2020 at 16:42

3 Answers 3

1

From the documentation

You need a dependency on spring-jdbc for an embedded database to be auto-configured.

spring-boot-starter-data-jpa and spring-boot-starter-data-jdbc gets spring-jdbc transitively through its dependency on spring-boot-starter-jdbc

This means for both these dependencies h2 will be auto-configured.

Update:

For both configurations I am able to access the db with http://localhost:8080/h2-console/

and you will be getting a similar log when spring boot application starts up

2020-02-05 01:27:16.135[0;39m [32m INFO[0;39m [35m55966[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.b.a.h2.H2ConsoleAutoConfiguration   [0;39m [2m:[0;39m H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testdb'
Sign up to request clarification or add additional context in comments.

1 Comment

As i said in the Update, this line is missing, but it works in my personal computer...
0

Both links provided by you allow me to create spring boot application with the following dependencies: Web, JDBC, H2, DevTools. If change 'spring-boot-starter-jdbc' dependency to 'spring-boot-starter-data-jpa' everything works the same way, e.g. H2 database is created and accessible. Make sure you specified the same configuration in both cases in the application.properties file, 'spring.datasource.url' hasn't been changed.

1 Comment

I am using the same property : spring.datasource.url=jdbc:h2:mem:testdb in both cases (which is the default value), but still not working
0

I found the solution :

As I said, I am using Spring Initializr to bootstrap the project. In the job pc, I was not able to use the last version of Spring boot (2.2.4), so i choosed 2.1.12

Version Spring Boot

With this version, it works with JPA but not with JDBC...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.