Is it possible to achieve session scope behavior in Spring WebFlux? - java

Currently experimenting with reactive programming using Spring WebFlux.
I need to reset some values(create new instance of one class) every session.
It is not possible to inject a #RequestScope or #SessionScope bean as it will end up with IllegalStateExcepion error.
Is it possible to achieve session scope behavior in Spring Webflux? Any tips what can I use to achieve that?

Spring Session
In order to use sessions in Spring Webflux, you can use Spring Session, more specifically you can use the integration with Spring Webflux's WebSession
Some tutorial can be found here: https://www.baeldung.com/spring-session-reactive

I have resolved this by using hazelcast session replication.

Related

spring.session.timeout vs server.servlet.session.timeout

I'm looking to extend the user session in a Spring MVC app with Spring boot and Tomcat. Looking at the documentation there seems to be 2 relevant properties:
server.servlet.session.timeout
spring.session.timeout
Most examples out there seem to suggest to use server.servlet.session.timeout; what is the purpose of spring.session.timeout? Which one should be used to extend the user session?
spring.session.timeout is the property from a Spring sub-project called Spring Session. It will fallback to server.servlet.session.timeout if it is not set.
In short , Spring Session allows you to store HttpSession in RDBMS / Redis / Hazelcast Cluster / MongoDB rather than an internal map inside Tomcat .So the sessions is stored in the container agnostic way and make session clustering easier as you do not need to configure a Tomcat cluster.
So if you do not use Spring Session , you should use server.servlet.session.timeout
You should be able to set the server.session.timeout in your application.properties file to do the same
I think, If you add spring.session.timeout in application.properties the session timeout value mentioned using server.session.timeout will be overridden

Multi tenancy with Spring boot

I'm planning to work on a multi-tenancy application and for now I'm just looking at different implementations on the web to understand the requirements needed to implement such task.
Hibernate + Spring boot are the technologies I'm planning to use.
From my readings, all the different tutorials are using the same approach which is to declare the data sources in a config file so that session factories are launched with the boot of the application, but I really want to have a higher level of the app, where I can add tenants dynamically and input their data sources informations.
This way the application can get the information of the new tenant without the need to touch the config files and re-boot the app.
I thought about having a separate database where I can store my tenants data source credentials or something like that. Can you give me another approach to solve this requirement or a link to an existing implementation that I can refer to.
Thanks
I got similar requirements in the past.
I implemented DataSource proxy class. The class has tenant resolver and a map of simple DataSources. All the places where we need a DataSource use the proxy.
On any method call e.g. getConnection() it resolves tenant, check whether the map contains already created DataSource (if not a new DataSource is created for the tenant and stored in the DB). Then the same method of real DataSource from the map is invoked.
Tenant resolver is ThreadLocal based where tenant value is stored in a filter (got tenant from request header) and used in the DataSource proxy.
What you need to do is using dynamic datasource routing of Spring Framweork via AbstractRoutingDataSource. This answer explains all for you.
In my question.I implements MultiTenantConnectionProvider and CurrentTenantIdentifierResolver.And use DataSourceLookup to choose datasource by tenant.This links is helpful to me.
Here is a full working example of a database-per-tenant multitenat app I built using Spring Boot 2, Spring JPA (Hibernate), Spring Security 5 running on MySQL.
I have explained how it all works and have shared the entire code too.
Take a look here.

Use existing Spring Boot dataSource for spring-session-jdbc

In my spring-boot application I have an existing dataSource, which I use for Hibernate and/or JdbcTemplate.
I am planning to use spring-session with spring-session-jdbc in the future.
Can the already existing and configured dataSource of the application be used?
If yes, how?
Or do I need to configure an additional dataSource for spring-session-jdbc?
The answer is:
Yes, it is possible, like the documentation of the newly released Spring Session 1.2.0 states:
http://docs.spring.io/spring-session/docs/current/reference/html5/guides/httpsession-jdbc-boot.html#httpsession-jdbc-boot-configuration
It also works without Spring Boot. In an old Spring-MVC project based on xml-config, which does not use Spring Boot, the configured dataSource is automatically used by Spring Session.

How to make legacy HttpServlet Spring web aware

We have a legacy HttpServlet class that is the backbone of our application. At this point, the app doesn't have any Spring libraries in it. We are looking to introduce Spring, in particular so we can use Spring-Data in conjunction with Hibernate.
I was wondering if there is a way to make this legacy Servlet web-aware so we can have Request and Session scopes injected. In particular, we would like to be able to inject the HttpServletRequest object into some beans.
One of the main reasons we need to do this, is for a weird multi-tenancy solution we have in place. Hibernate can handle Multi-Tenancy using a combination of a AbstractMultiTenantConnectionProvider and a CurrentTenantIdentifierResolver When using Spring-JPA's Repositories, you lose control of the session creation. One way to take care of this is to implement the CurrentTenantIdentifierResolver Since our tenant identifier is partially determined by something that comes in on the request, it is necessary to inject the request into our CurrentTenantIdentifierResolver implementation.
Also, it would be great to get Spring involved for all the other benefits it can provide in a legacy app.
Do you know how we can accomplish this?
You can define org.springframework.web.context.ContextLoaderListener within your web.xml, which will load your spring application context.
Then, within your servlet code, you access the context using WebApplicationContextUtils.getWebApplicationContext(servletContext) helper method.
Take a look at the Spring docs here:
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#web-integration-common

Spring-JPA-MVC-Hibernate Session per Request with Java Config

I have a spring-jpa-mvc-rest-hibernate web application that I use Java Config almost 99% on the whole app.
How can I configure Session per Request on pure Java Config? Preferable not using hibernate specific code, if that is possible.
I'm using Spring 4.2.2.RELEASE, Spring Data 1.11.0.RELEASE, Spring JPA 1.9.0.RELEASE, Spring WebMVC 4.2.2.RELEASE and Hibernate 5.0.2.Final.
Thanks a lot.

Categories

Resources