(Spring Boot and H2) Can't use h2 database - spring-boot

I'm learning Spring boot and I'm trying to create a very simple RESTful API that access an in-memory database and perform CRUD actions.
However, everytime I try to Connect or Test Connection on http://localhost:8080/h2-console, I get this error:
"Database "C:/Users/XXX/test" not found, and IFEXISTS=true, so we cant auto-create it [90146-199] 90146/90146"
https://imgur.com/a/oYgkK1C
I followed EXACTLY the instructions from http://www.springboottutorial.com/spring-boot-crud-rest-service-with-jpa-hibernate. I have tried everything I could find online: using jdbc:h2:mem:test as JDBC URL etc, but none of them worked for me and I'm not sure what am I doing wrong.
I didn't install h2 database from the official website as I read it is not necessary to use the in-memory module (and I still don't know if I should've installed it or not, as there is not a single mention of it online, as far as I checked).
Any thoughts? I'm a beginner when it comes to Spring Boot and I'm really lost. I just want to test CRUD actions and I don't care about the persistence of the DB.
I have provided my application.properties below.
Thank you! :)
# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2
# Datasource
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.username=user
spring.datasource.password=user
spring.datasource.driver-class-name=org.h2.Driver

Make sure your url in h2-console(refer screenshot below) is same as your 'spring.datasource.url=jdbc:h2:mem:test'.
It worked for me.
UPDATE: Other alternative solution is,
you can avoid setting spring.datasource.url property. Spring will automatically set this default JDBC url for you.
happy learning.. Upvote, if it is sovled your issue.

In your spring application.properties file, set the property
spring.datasource.url=jdbc:h2:~/test
Then restart the application and open http://localhost:8080/h2-console/
Click on Test Connection button, you should see "Test successful".

Related

Can I just add an application.properties file to a Spring Boot project?

I'm fairly new to Spring Boot and MongoDB. Currently I have a project that can send data back and forth to a server that is running locally on my computer, but I want to change this and make it edit and retrieve data from an externally running database. In other tutorials I have followed I have had an application.properties file that I can edit details in to get it to connect, but I can't find this in any of the sub folders (I pulled the code from a tutorial) and I can't find anything to say that it is specifically connecting to the local instance.
Would it be okay to just create the application.properties file in the right sub folder and enter the external database's details there? Or am I going to have to try a separate method and tutorial to try and connect to the external database in another way?
I have a feeling that to answer it you will need to see/understand more of the code, but I'm not sure how to summarise anything else or what would actually be relevant. Thank you.
Spring Boot has several default folders, where it searches for properties.
One of those places is for example src/main/resources/application.properties, there you can just create this file.
An overview of other possible places for Spring Boot properties can be found here:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
In my German blog I wrote an article about how to use Spring-data to access MongoDB - there I used also application.properties file:
https://agile-coding.blogspot.com/2020/10/keine-ahnung-von-mongodb-dann-nimm.html

Microsoft SQL server auto create schema if not exists on spring boot (application.properties)

I could not find how to tell spring boot to create schema if it does not exist on ms SQL, for example, in MySQL, I was able to achieve this goal by doing this:
spring.datasource.url=jdbc:mysql://localhost:3306/example?createDatabaseIfNotExist=true
This is the code for MS SQL:
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=Example
ofcurse with email and password confioration...
Hibernate Configuration:
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServerDialect
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
I tried to look over the internet and here but could not find an answer.
Have a look at the EDIT section of a similar question on SO.
In particular, note the links to Spring Boot's application.properties file, and the database initialization section of the Spring Boot reference docs.
Also see the Hibernate documentation for Automatic schema generation, which tells you what the options to hibernate.hbm2ddl.auto mean.

How to pass database name explicitly in Spring Boot Configuration?

I've a Spring Boot application of mine, which connects to a Postgres database. I've specified in application.properties the datasource url as -
spring.datasource.url=jdbc:postgresql://< server ip here >:5432/mydb
The jdbc url (jdbc:postgresql://< server ip here >:5432/) is actually stored in a separate external location which my application is able to read. Therefore, I want to specify only the database name in my properties file.
I don't want to pass the database name as some environment variable since it's not going to change.
I'm stuck at this point for quite some time now, how can I achieve the same?
Add this in your application.properties file
spring.jpa.hibernate.ddl-auto=create\update\none
spring.datasource.url=jdbc:postgresql://host:port/db
spring.datasource.username=username
spring.datasource.password=password
Have you tried using ${var} syntax like:
spring.datasource.url=jdbc:postgresql://${server-ip}:5432/mydb
See:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-placeholders-in-properties
I finally implemented it this way.
Specified only database name in my application and created Datasource bean in a separate Spring Boot application (so that it can be reused across other projects as well).

spring boot datasource tomcat jdbc properties not working

I have a Spring Boot application (version 1.5.1.RELEASE) and I am using spring-boot-starter-data-jpa as a dependency to manage my database. I am using postgres as my database and configured it using the below properties.
spring.datasource.url=${POSTGRES_URL}
spring.datasource.username=${POSTGRES_USER}
Now when I run my tests which are almost 120, I get too many client already open error for abou 10 test cases while starting the test case itself and it fails.(remaining 100 test cases pass with success as they are able to get a connection to database)
First thing I did is increased my default postgres max connections count from 100 to 200 in the postgres server config file and my tests pass successfully after this change.
Now I investigated a bit and tried setting the parameters for connection pooling properties such as :
spring.datasource.tomcat.max-active=200
spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.max-wait=10000
However these properties do not work and the tests fails again giving the same error as above. I tried reading from multiple different blogs and spring documentation for setting the connection pool properties but did not find what might be going wrong with me.
I also think that if I set the above property spring.datasource.tomcat.max-active to 100 connections it should work with the help of tomcat jdbc pooling as i think in current scenario it is trying to open a new connection to database for each test case and I am in a fear that this same scenario might happen when I deploy this code to production environment and a new connection will be opened to the database for each request.
Does anyone have faced this problem before or is there something wrong I am doing.
Thanks in advance for the help.
Try upgrading Spring boot version, 1.5.10-RELEASE is the current version.
Also, I found the connection pool properties for my application were not being applied when the property prefix tomcat was included. If you are still having issues try removing that.
i.e.
spring.datasource.tomcat.max-active=200
Becomes
spring.datasource.max-active=200
See https://artofcode.wordpress.com/2017/10/19/spring-boot-configuration-for-tomcats-pooling-data-source

Springboot In memory hsqlDb cannot see tables from sql browser

I am using in memory hsqldb in my spring boot project. Here is a sample of my appliction.properties
spring.datasource.url=jdbc:hsqldb:mem
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
When I start my application, it creates couple of files like mem.script, mem.log in my project home directory.
mem.log has all the sql statements it has excuted on the database. And it gets updated whenever some action is triggered from my repository class. Functionality as a whole is working, I can even query from this hsqldb and it works.
But for some debugging purpose I want to connect to this db through some db client (DBVisualizer). When I tried connecting(DB Type : HSQLDB Embedded) using this mem.log file, I was successfully connected, but cannot see any of my tables there.
I even tried setting up a server in my application as mentioned here,
How to start HSQLDB in server mode from Spring boot application
Even in this case I can connect from DBVisualizer(DB Type : HSQLDB Server) but cannot see any tables.
Please let me know if I am missing something.
Thanks
First of all, the url you are using is too similar to an all-in-memory database with no files (jdbc:hsqldb:mem:anyname) but it is not an in-memory URL.
Follow the the instructions for the server mode in the question you linked to. Report your version of the configuration if you cannot see the tables. Note the configuration parameters that I quote below:
<prop key="server.database.0">mem:testdb</prop>
<prop key="server.dbname.0">testdb</prop><!--DB name for network connection-->
and connect with this URL from DBVisualiser
jdbc:hsqldb:hsql://localhost/testdb

Resources