H2 database connecting to in memory instead of file database - spring

I am stuck in this, trying to change my h2 db from in memory to file based. But everytime on restart, it automatically connects to in memory.
Here is my application.properties
server.port=8010
spring.datasource.url=jdbc:h2:file:~/transactionsdb;AUTO_SERVER=TRUE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=user
spring.datasource.password=password
#spring.sql.init.continue-on-error=true
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true
spring.h2.console.enabled=true
#spring.jpa.defer-datasource-initialization=true
spring.jpa.hibernate.ddl-auto=none
Please help me to progress. Thanks in advance

This is my application.yml file and on restart H2 database is connecting to the file database. I have created test1.mv.db file in /Users/rohit/ directory.
spring:
h2:
console:
enabled: true
datasource:
url: jdbc:h2:file:/Users/rohit/test1
username: sa
password:
data:
jpa:
repositories:
bootstrap-mode: default
In application start-up logs also I can see the same information

Related

Use JAAS config file with hikari data-source-properties

Is there a way to use a JAAS config file with Hikari data-source-properties? I have multiple Login contexts in the JAAS config file (to connect to Cassandra and Kafka) and would like to connect to Oracle db in the same way.
These are the current properties:
spring:
datasource:
url:
username:
driver-class-name: oracle.jdbc.OracleDriver
hikari:
data-source-properties:
oracle.net.kerberos5_cc_name: <ticker cache path here>
oracle.net.authentication_services: KERBEROS5
oracle.net.kerberos5_mutual_authentication: true
Instead of specifying ticket cache through these properties I'd like to keep them in the JAAS config file and specify the config path with VM args/System props (java.security.auth.login.config). The reason for this is that while specifying ticket cache paths in application properties, they get overriden. (If connection order is Oracle->Cassandra->Kafka, then the Oracle config will be overriden by Cassandra config, and then the Cassandra config will be overriden by Kafka configuration.)

Not able to connect to H2 database in Spring Boot

I have the below in my yaml file.I keep getting the below error.how to resolve the below error.I am using latest spring boot version .
org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database
"C:/Users/mynname/test" not found, either pre-create it or allow
remote database creation (not recommended in secure environments)
[90149-214]
I can see the H2 console in http://localhost:8080/h2.But when i click on connect button i get the error.
spring:
h2:
console:
enabled: true
path: /h2
datasource:
url: jdbc:h2:mem:testdb
username: sa
password:
driverClassName: org.h2.Driver
jpa:
spring.jpa.database-platform: org.hibernate.dialect.H2Dialect
hibernate.ddl-auto: create
On the h2 console change the default ur that appears there (jdbc:h2:mem:test) to jdbc:h2:mem:testdb
And you should be able to connect

Aplication.properties error when changing format

I have:
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=Europe/Madrid
spring.datasource.username=root
spring.datasource.password=secret
spring.jpa.hibernate.ddl-auto=update
and It works fine.
But when I try to do it this way:
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimezone=Europe/Madrid
username: root
password: secret
jpa:
hibernate:
ddl-auto: update
It fails with:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
I assume it is a silly syntax error, but I can't notice where it is.
If you are going to use the YAML syntax, you need to specify it by changing the name of the file from application.properties to application.yml.
YML is very sensitive. Make sure you:
Use spaces instead of tabs
Use the proper extension: application.yml
See this: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-yaml

how to configure both mongodb and postgres in spring boot application.yml?

We have spring boot project , in which we need to use mongo and postgres , but i thought am failing in configuring mongo and postgres details applicaiton.yml , below my what i configured , can any one please help me
spring:
profiles: stage
data:
mongodb:
host: mongodb-host
port: 27017
password: password
username: username
authentication-database: database
database: database
datasource:
driver-class-name: org.postgresql.Driver
url: postgres-url
username: user-name
password: password
tomcat:
validation-interval: 30000
test-on-borrow: true
validation-query: SELECT 1
when i start my spring-boot application , it is failing to start with below error
Caused by: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
can any one please help
Solution 1: remove spring.datasource.driver-class-name property
Solution 2: rename the spring.datasource.url property to spring.datasource.jdbc-url.
For solution 1 Spring Boot will figure out the default driver class name based on the spring.datasource.url property value.

Database application.yml for Spring boot from applications.properties

I've got a working Spring Boot Application that connects to a Postgres database. I've got the project set up with an application.properties file, but would like to make the switch over to an application.yml file. However when I make the switch, my application errors out while attempting to connect to the db.
Original applications.properties file:
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=foo
spring.datasource.password=bar
And Here's what I've got so far in the application.yml file:
spring.jpa:
database: POSTGRESQL
hibernate.ddl-auto: create-drop
show-sql: true
spring.datasource:
platform: postgres
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/mydb
username: foo
password: bar
Am I missing something in the translation between file types?
You need to treat each . character in property names as levels in the yaml file:
spring:
jpa:
database: POSTGRESQL
show-sql: true
hibernate:
ddl-auto: create-drop
datasource:
platform: postgres
url: jdbc:postgresql://localhost:5432/mydb
username: foo
password: bar
driverClassName: org.postgresql.Driver
EDIT: edits have been suggested, thanks for that. The driverClassName property actually should be under spring.datasource. However, the purpose of this answer was to show how a properties file is converted into yaml format. So I have changed the driverClassName property to be at the right path, that is not part of the transformation from properties to yaml.
Please upvote the other answer (Z0lt#n's answer)
But pasting here for future readers... a sql server version.
spring:
jpa:
show-sql: true
hibernate:
ddl-auto: update
properties:
hibernate.jdbc.batch_size: 20
hibernate.cache.use_query_cache: false
hibernate.cache.use_second_level_cache: false
hibernate.cache.use_structured_entries: false
hibernate.cache.use_minimal_puts: false
datasource:
#SPRING_DATASOURCE_URL environment variable will be something like -> jdbc:sqlserver://MySqlServer\\MyInstance:1433;DatabaseName=MyDbName;
url: ${SPRING_DATASOURCE_URL}
username: ${SPRING_DATASOURCE_USERNAME}
password: ${SPRING_DATASOURCE_PASSWORD}
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
and maven entry
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.0.0.jre8</version>
</dependency>
APPEND
This seems to be the "standard" name for the driverClassName.
SPRING_DATASOURCE_DRIVER-CLASS-NAME
And of course, in my example, you would use the value of:
com.microsoft.sqlserver.jdbc.SQLServerDriver
NOW, some spring, springboot, environment variable voodoo alert.
Sometimes, when specifying the environment variable...for some command lines items, I would have to change the hyphens and make them underscores.
(aka, "SPRING_DATASOURCE_DRIVER-CLASS-NAME" vs "SPRING_DATASOURCE_DRIVER_CLASS_NAME"
below the -e generically represents "passing environment variable values via the command line"
MyCommandLineProgram.exe -e SPRING_DATASOURCE_URL="jdbc:sqlserver://myServerName:1433;DatabaseName=MyDB;" -e SPRING_DATASOURCE_USERNAME="myUserName" -e SPRING_DATASOURCE_PASSWORD="myPassword" -e SPRING_DATASOURCE_DRIVER_CLASS_NAME="com.microsoft.sqlserver.jdbc.SQLServerDriver"
There's some voodoo for you.
Those interested in the logging (logback.xml) issue, maybe want to find my answer here as well:
Spring Boot Logback DB Appender Properties
application.yml file for postgresql
Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring:
datasource:
driver-class-name: org.postgresql.Driver
username: postgres
password: root
url: jdbc:postgresql://localhost/postgres
platform: postgres
initialization-mode: always
continue-on-error: true
jpa:
show-sql: true
generate-ddl: true
hibernate:
ddl-auto: create
database: postgresql
server:
port: 1111
spring:
application:
name: client-one
datasource:
password: postgres
url: jdbc:postgresql://localhost:5432/postgres?currentSchema=education
username: postgres
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
show-sql: true

Resources