Spring Session Creation - spring-boot

spring.session.store-type=jdbc
# Database schema initialization mode.
spring.session.jdbc.initialize-schema=always
# Path to the SQL file to use to initialize the database schema.
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-mysql.sql
# Name of the database table used to store sessions.
spring.session.jdbc.table-name=SPRING_SESSION
spring.main.allow-bean-definition-overriding=true
The above is my configuration in application.properties. When the application runs, Spring creates a session and persists it automatically in the Spring_session and spring_session_attributes table. My question is, can I tell Spring to do not persist the session details into the Database, and instead I create and save it to the same tables using JPA?
Thank you.

Related

liqibase multiple schema update with single user credentials

I have requirements as below:
Use liquibase DB manager to update DB objects of multiple schemas using single elevated DB credentials
Every schema has different Datasoure objects
Every Schema should maintain their own liquibase tables (DATABASECHANGELOG and DATABASECHANGELOGLOCK)
Can anyone help with this?
I am using:
Springboot
HikariCP as connection pool
I tried setting
spring.liquibase.liquibase-schema
and
spring.liquibase.default-schema
configuration but not getting a way to set schema for each datasource.

Spring Boot h2 initial data not creating table first

I am trying to create a small Spring Boot setup with a h2 database.
Now I have a weird problem, which I can't solve.
If I don't create an data.sql for initial data, the app starts fine and creates my entity tables.
If I create an data.sql for initial data and keep the existing table from previous step, everything works fine.
If I create an data.sql for initial data and remove my existing h2 file, I get the error that it can't import the data, because the table is missing.
How do I tell Spring to create my tables before importing the initial data?
This is covered in the release notes for Spring Boot 2.5:
By default, data.sql scripts are now run before Hibernate is initialized. This aligns the behavior of basic script-based initialization with that of Flyway and Liquibase. If you want to use data.sql to populate a schema created by Hibernate, set spring.jpa.defer-datasource-initialization to true. While mixing database initialization technologies is not recommended, this will also allow you to use a schema.sql script to build upon a Hibernate-created schema before it’s populated via data.sql.

How can I make my flyway default DB pointing to different db and my actual Spring boot Application to some other DB

I am trying to integrate flyway to my existing Spring boot app. My requirement is, the default table created by flyway needs to be created on different DB schema and my applications actual table migration must happen on different schema. Reason being, no addition table must be created on the applications DB schema. Currently we are using Mysql
We had a Spring Boot app with the flyway configuration in application.properties file for different environments:
flyway.enabled=true
flyway.url=jdbc:postgresql://localhost:5432/finance
flyway.schemas=user
flyway.password=password
flyway.user=postgres
flyway.baseline-on-migrate=true
We had the application-dev.properties, application-local.properties and application-aws.properties with the configuration specific to the environment
Flyway provides a property flyway.schemas. When the flyway.schemas property is set (multi-schema mode), the schema history table is placed in the first schema of the list.
https://flywaydb.org/documentation/commandline/migrate.html
ex:flyway.schemas=schema1,schema2,schema3

Database table creation and updation with springboot, spring data

For the database schema management with spring data/hibernate, setting spring.jpa.hibernate.ddl-auto option doesn't look like a cleaner approach.
Bcoz
1) We are forced to put the credentials of a user that has permission to create and delete in the application.properties.
2) In production, relaying on spring.jpa.hibernate.ddl-auto option could lead to dataloss, if not managed carefully.
So what is the best way out there to handle the database schema management automatically in a spring boot/spring data app?
Any pointers would help.
Thanks
If you want to track each change state of database then you can use flyway
See this link how to maintain database versioning in spring-boot
In production, you should ideally set spring.jpa.hibernate.ddl-auto property as none so that no schema changes are allowed.
For the database schema management with spring data/hibernate, I would suggest you go for Liquibase, it basically is an open source database-independent library for tracking, managing and applying database schema changes.
Every change to Schema is added as a changeset using property file in Liquibase , this is for the new changes.
In order to migrate the existing database structure into Liquibase, it provides you with commands to automatically generate Changesets by reading the current database.
So using this you can generate database schema, add constraints, load data.
More info at : https://www.liquibase.org/ , Why and when Liquibase?

How to disable loading schema.sql if database already exists?

I am using H2 database stored in a file and schema.sql script to initialize the database (using DatabasePopulator class in Spring). It works, but schema.sql is imported every time the application starts (establishes a connection).
My goal is to load it after database creation only. If a connection is established to already existing database, I want to skip it.
From what I've read:
H2 provides a way to init database using RUNSCRIPT in jdbc url (loads every time, not what I want).
Spring Data JPA and Hibernate allow database initialization by autogenerating DDL (not what I want, I have to use schema.sql) or loading schema.sql, but that one only works when spring.jpa.hibernate.ddl-auto property is set to either create or create-drop, which is also not what I want, because I have database in a file and it must not be deleted every time the application starts.
So my question is: How to load schema.sql on database creation only, without manually checking if the database exists? Is it even possible?

Resources