I am using Spring Boot with Spring Data JPA to query database through Repository. If the backend database is booted for some reason, then Spring Boot cannot connect to that database again unless rebooted. Is there a way to get the Data JPA to repopulate connections to database after DB reboot?
In Spring Boot, we can solve this problem adding these configurations in the application.properties file:
spring.datasource.testWhileIdle = true
spring.datasource.timeBetweenEvictionRunsMillis = 60000
spring.datasource.validationQuery = SELECT 1
Checkout the full explanation here
Related
Is there a way in Spring Data R2DBC to setup a second datasource as slave read-only ?
It is easy to configure multi R2dbc connections in the same application.
Check my example project: https://github.com/hantsy/spring-puzzles/blob/master/multi-r2dbc-connectionfactorie
I am using spring boot 2.5.3 with Jetty 9.4.43.v20210629. I want to share the session across multiple nodes. In Jetty 9.4 there are lot of changes in session management. I want some reference materials for session replication (using JDBC / File system) with java configurations.
Session replication can be done using spring session with following storage options,
Hazlecast
Database
Redis
MongoDB
spring.session.store-type property should be used to decide the storage type. When multiple nodes / containers point to same storage it will share the session and no need to maintain a sticky session.
Need to add proper dependencies in pom.xml and add annotations when required (ex: #EnableJDBCHttpSession)
sample application yaml changes.
JDBC
spring:
application.name: console-bs
main.allow-bean-definition-overriding: true
profile: default
session.store-type: jdbc
session.jdbc.initialize-schema: always
jpa.database: mysql
jpa.database-platform: org.hibernate.dialect.MySQL5Dialect
datasource.url: jdbc:mysql://mysqldb:3306/user
datasource.username: user
datasource.password: user1234#
datasource.driver-class-name: com.mysql.cj.jdbc.Driver
Redis
spring:
application.name: console-bs
main.allow-bean-definition-overriding: true
profile: default
session.store-type: redis
redis.host: redis
redis.port: 6379
I have a problem with Hikari connection pooling in Spring boot. I use spring boot 2.1.7 in oauth2 server 2.3.4.RELEASE program that check tokens from oracle database. when I see v$session every thing is ok but in listener.log There are many logons and logoffs. Some sessions logon and logoff during 1 second!
I change Hikari connection pooling properties according to spring jdbc hikari connection pool - constantly logs on and off to database but nothing change!!
my parameters are:
spring.datasource.auto-commit=true
spring.datasource.connection-timeout=1000
spring.datasource.maximum-pool-size=10
spring.datasource.TYPE=com.zaxxer.hikari.HikariDataSource
spring.datasource.minimum-idle=1
spring.datasource.pool-name=oauth-POOL
#spring.datasource.maxLifetime=2000000
spring.datasource.idle-timeout=900000
We have an application that uses several data sources. A DB underlying one of those data sources is down at the moment: IOError. Network adapter couldn't establish the connection & Socket read timed out.
Is there an annotation (or other means) of configuring Spring Boot such that it bypasses the culprit data source and still starts up: the DB is not essential in current development work. spring.datasource.continue-on-error=true doesn't seem to work. This is Spring 2.2.2.RELEASE.
using multiple datasource, so when your apps fail at start up your apps still work, i mean using memory db / sqlite to handle fail at connection error...
I'm confused about how to connect Spring dynamically to multiple databases, depending on the request?
For Example :
1. In admin site, register new member.
2. Database admin creates a new database for this new member.
3. Spring boot .jar, when in run mode, just adds some configuration lines to the external file, this service can connect to the new added database.
member 1 has url 1 => www.example.com/member-1 connect to db_member_1
member 2 has url 2 => www.example.com/member-2 connect to db_member_2
... etc
So I mean, I don't need to update my source code, but just add some lines in my external configuration file,so this spring boot .jar can connect to the new database.
I have tried connect two databases in 1 spring boot but I don't know how to make it dynamically without unloading my spring boot.
I use JPA to connect my master database and use JdbcTemplate to connect my transaction database.
Thanks in advance. :)
Regards,
Peter Susanto