I'm trying to connect to an h2 database on my local machine to create a sql DataSource object. I'm running windows and i'm having some issues defining the path to the data file in my projects app.properties file.
Say the path to the local directory data file is:
D:\projects\myproject\data\project
How would one go about defining a connection url for this?
I've tried the many things including the following:
project.db.url = jdbc:h2:tcp://localhost\\\\D:\\projects\\myproject\\data\\project
Then I thought maybe it's the JDBC URL that's the issue, so I tried:
project.db.url = jdbc:h2:tcp:\\\\localhost\\\\D:\\projects\\myproject\\data\\project
Change application.properties to the following:
spring.jpa.open-in-view=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
Set H2 Console to the following:
jdbc:h2:mem:testdb
As per documentation, default JDBC connection string is
jdbc:h2:~/test
And, for TCP connection
jdbc:h2:tcp://localhost/~/test
==Update==
But, if you wanted to create/read h2 database to/from specific folder, then it should be
jdbc:h2:tcp://localhost/<path_to_database>
That means,
jdbc:h2:tcp://localhost/D:/myproject/data/project-name
Thanks #Sam for sharing info.
If you are using Spring Boot and don't want to change the default name you can look out for this log statement and copy the JDBC connection info from there:
2021-08-31 20:27:13.295 INFO 12032 --- [ restartedMain] o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:4c0a3d2c-9aab-4c06-ab22-da777660ab4a'
So in this example the connection string is "jdbc:h2:mem:4c0a3d2c-9aab-4c06-ab22-da777660ab4a"
Related
This is application properties code,please check and tell me how to fix the error.
debug=true
server.port=9090
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
#create, update, create-drop, validate
spring.jpa.hibernate.ddl-auto=update
#file related all configurations
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
project.image=images/
#here we are allowing debug so that we can get every log related to security on console
logging.level.org.springframework.security=DEBUG
#spring.profiles.active will tell us which profile we are using
spring.profiles.active= prod
This is application-developer properties code
spring.datasource.url=jdbc:mysql://localhost:3306/blog_app_apis
spring.datasource.username =
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
This is application-production properties code
spring.datasource.url= jdbc:mysql://blog-db.cx8gwdujroiq.ap-south-1.rds.amazonaws.com:3306/blog_app_apis
spring.datasource.username = pankaj
spring.datasource.password=kicker12345
This is the error message:
APPLICATION FAILED TO START
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (the profiles prod are currently active).
Your production configuration is missing the database driver property, which you should clearly understand from the reason section in the exception message:
Reason: Failed to determine a suitable driver class
Since you have the application.properties file with the default configuration properties, you can use the fix suggested to you by the Spring framework:
Consider the following: If you want an embedded database (H2, HSQL, or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (the profiles prod are currently active).
In order to fix the error for the production profile you need to add the following property to your application.properties file: spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
The resulting file should look like this:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
debug=true
server.port=9090
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
#create, update, create-drop, validate
spring.jpa.hibernate.ddl-auto=update
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
project.image=images/
#here we are allowing debug so that we can get every log related to security on console
logging.level.org.springframework.security=DEBUG
#spring.profiles.active will tell us which profile we are using
spring.profiles.active=prod
This will add the default database driver for all your profile. So be mindful to change this if some specific profile will use a database that differs from MySQL.
Updated:
You also have the incorrect name of the active profile. In your application.properties file you have a such line that sets the active profile of your application:
spring.profiles.active=prod
But your profile-based file is called application-production.properties. The Spring framework has a such naming format for configuration properties files:
application-[PROFILE_NAME].properties
So you should also rename your application-production.properties file to application-prod.properties to match the value from the spring.profiles.active property.
I want to have spring boot init-sql dynamic property by vendor or platform. Is this possible?
spring.datasource.tomcat.mysql-initSQL=mysql query
spring.datasource.tomcat.h2-initSQL=h2 query
I do know that is possible with other properties for example with flyway migrations:
flyway.locations=db/migration/{vendor}
or with sql initilization file
schema-${platform}.sql
You can do this using the Spring Profiles.
Create 2 profiles.
1) H2 2) MySQL
Create two init sql files in your class path schema-h2.sql, schema-mysql.sql
Add the spring.datasource.platform property to the profiles
In H2 Profile => "spring.datasource.platform = h2"
In MySQL Profile => "spring.datasource.platform = mysql"
This works as below.
When you application is started with h2 profile (spring.profile.active=h2) then the schema-h2.sql is picked for initialization.
When you application is started with mysql profile (spring.profile.active=h2) then the schema-mysql.sql is picked for initialization.
I'm unable to get spring boot to automatically load my database schema when I start it up.
I follow this example: https://spring.io/guides/gs/accessing-data-mysql/#scratch
Here is my application.properties:
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/sakila
spring.datasource.username=root
spring.datasource.password=root1
sakila it is a default database included in mysql
enter image description here
Error:
Unable to create initial connections of pool
I am using Spring Boot 1.4.3 and have a whole bunch of tests that are annotated with #DataJpaTest. By default, they run against an in-memory database. I would like to be able to run all of them against a local MySQL temporarily. How can I do this in an easy way?
I have found that I can make it work for one by adding #ActiveProfiles("local") where I have an application-local.properties that points to my local MySQL, but it is just too much work to add that everywhere, run the tests and then remove it again (since I only want to run this manually against MySQL, the CI environment will run against the in memory db).
I am using Maven if that would matter.
UPDATE:
So I have an application-local.properties which contains the db properties to connect to my local MySQL database (Which I use already to run my application against the local MySQL)
Then I right-click in IntelliJ on a package and select "Run all tests in package". In the settings of that run configuration, I add -Dspring.profiles.active=local to the "VM options" field.
I would have thought that this would activate the local profile during the tests, but it does not. If I stop the local MySQL, the tests still run fine.
In the docs it states that you are able to remove the autoconfigured h2 datasource with #AutoConfigureTestDatabase(replace= Replace.NONE) on the test class https://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test.
Also you then need to provide your db setup in properties, so that it does not use your app properties e.g.:
# Database
spring.datasource.url=jdbc:mariadb://localhost:3303/test
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
i put this in application.properties in the test package
You can add the profile with the MySQL datasource properties in the same application.properties (or .yml) as:
application.yml
# Existing properties
---
spring:
profiles: h2
# More h2-related properties
---
spring:
profiles: postgres
database:
driverClassName: org.postgresql.Driver
datasource:
url: jdbc:postgresql://localhost:5432/db_dvdrental
username: user_dvdrental
password: changeit
jpa:
database: POSTGRESQL
generate-ddl: false
# More postgres-related properties
and either use #ActiveProfiles("postgres") in an integration test class or start teh container using VM argument as:
java -Dspring.profiles.active=h2 ...
Add application.yml(properties) with jdbc connection into src/test/resources
Run your JPA test with #AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE) - it disables using embedded database (h2), otherwise :
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'dataSource': Invocation of init method
failed; nested exception is java.lang.IllegalStateException: Failed to
replace DataSource with an embedded database for tests. If you want an
embedded database please put a supported one on the classpath or tune
the replace attribute of #AutoConfigureTestDatabase.
As mentioned in dropwizard-migrations documentation, you can dump you existing schema to migrations.xml using command
java -jar hello-world.jar db dump helloworld.yml
But I am using postgresql which can have multiple schemas, so how can I configure my db to always get status/dump of the default schema i am woking on instead of public schema.
I tried changing the search_path for database but that doesn't worked out.
Liquibase has a defaultSchemaName property. I haven't used dropwizard myself but it seems they use JDBI and a yaml based config file for the db connection.
So, have you tried just putting the option defaultSchemaName into your service configuration file like this:
database:
# the name of your JDBC driver
driverClass: org.postgresql.Driver
# the username
user: pg-user
# the password
password: iAMs00perSecrEET
# the JDBC URL
url: jdbc:postgresql://db.example.com/db-prod
# any properties specific to your JDBC driver:
properties:
charSet: UTF-8
defaultSchemaName: <yourSchemaName>
Jens was very close, you need to specify the currentSchema not defaultSchemaName property for the JDBC driver. like:
database:
driverClass: org.postgresql.Driver
# the other attributes
properties:
currentSchema: <yourSchemaName>