Jetty Spring Boot with session replication - spring

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

Related

Unable to connect to multiple astradb keyspaces from springboot microserviec

I am new to casasndra and using astradb on aws + springboot as microsrvice (its monolith as of yet). As we quickly reached 200 tables for one keyspace for our application, we are thinking of using multiple keyspaces for multiple modules. Also there is one limitation of using storage index (50 sai per db). Need help to get details about how do I access multiple keyspaces for my spring boot app ?
using org.springframework.boot:spring-boot-starter-data-cassandra
I am using below configurations:
application.yml
astra:
api:
application-token: <your_token>
database-id: <your_db_id>
database-region: <your_db_region>
cql:
enabled: true
download-scb:
enabled: true
driver-config:
basic:
session-keyspace: <your_keyspace>

Spring Boot Application and Hazelcast NearCache

I am trying to use Hazelcast caching with Spring Boot, having read some documentation I decided to settle for Near Cache configurations, I would like to cache some method calls and use it for Hibernate L2 caching.. the trouble is I haven't exactly seen an example of using Near Cache specifically creating near cache clients and starting the server.
Can I have some code examples for in configuring that setup (nearcache configurations) and how to start the server side.
Configure Hibernate to use Hazelcast client:
<!-- hibernate.cfg.xml -->
<property name="hibernate.cache.hazelcast.use_native_client">true</property>
<property name="hibernate.cache.hazelcast.configuration_file_path">hazelcast-client.xml</property>
and in client config, configure near-cache:
<!-- hazelcast-client.xml -->
<near-cache name="default">
<time-to-live-seconds>90</time-to-live-seconds>
<max-idle-seconds>100</max-idle-seconds>
<in-memory-format>OBJECT</in-memory-format>
</near-cache>
Alternatively, you can set different configurations for each cache region via:
<near-cache name="<entity-cache-region-name>">
These will enable client's near-cahce for Hibernate L2C. You do not need to add any additional config on server side at this point. However, if you also want to configure near-cache for members, you can configure them independently of Hibernate & client side.
Configuration details for both client and member are here in the documentation.

Connection Pooling configuration with HikariCP and AWS Aurora Serverless

I am creating microservices applications using spring boot. All these microservices will connect to a single database AWS Aurora Serverless. I am planning to configure the connection pooling in my applications using HikariCP. Based on my initial research, Aurora Serverless manages connection pooling automatically. My question are the following.
Do I still need to configure connection pooling in my microservices using HikariCP? If yes, what is the recommended configuration in spring boot application.yml considering these are microservices that would share the same database?
If connection pooling configuration is not needed in my microservices, should i disable it? And, how should I do it my application.yml.
I have initial hikaricp configuration as shown in my code below
Springboot version is 1.5.7.RELEASE
application.yml
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
hikari:
connection-timeout: 30000
minimum-idle: 30
maximum-pool-size: 200
idle-timeout: 30000
... database details (url, password, etc...)
A connection pool is still useful since there will be a cost to build a connection from your app to their connection pool.
What is different is that when you don't have any activity during a significant amount of time (night/weekend/periods of no activity) or when speed is not a necessity you should put the minimum-idle parameter to zero.
This will make sure the number of connections goes down to zero and enable your aurora serverless db to go into pause.
When it is paused you pay only for storage costs.

Resuming Spring Data JPA connection after database is restarted

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

Specify which logical database to use with spring-data-redis

I'm using spring-data-redis, spring-session and Spring Boot to connect to my Redis instance. However I would like spring-data-redis to connect not to DB 0 (which is default) but to another local database (say DB 1). This is where I'd like the sessions to be stored. Is this possible with spring-data-redis?
The ConnectionFactory used by RedisTemplate offers configuration options for setting a default DB. Depending on the Redis driver in use both JedisConnectionFactory as well as LettuceConnectionFactory offer void setDatabase(int index).
Using Spring Boot RedisProperties allows to set the default DB via setDatabase or by providing spring.redis.database.

Resources