I have a spring boot application with java 8 ,jpa etc and a jboss application with j2ee applications which calls too many sql procedures to update the table.
I have a query something like this in spring boot to get all the employee:
#Cacheable("employeeList")
List{Employee} findByAddressId(Long addressId);
But if someone inserts a new record to Employee table in the same address id from sql procdure from jboss application, the spring boot application is not able to pick the new records , because the query is so generic to that address id.
So i want to create a trigger on that table on insert and update , so when ever insert/update happens it should update the cache with new records belongs to that address id.
Can somebody please tell me how to do this?
If I understand the question correctly you have a spring boot app and a separate jboss app that are connecting to the same database and are insert/updating to the same database tables.
With spring's #Cachable you need to be able to tell spring when you should evict the cached item. For example, having the method that updates the entity being marked as #CacheEvict is an easy way to evict the entity from the cache. The problem here is that if the jboss app updates a record there is no way for spring boot app to know this.
Using a database trigger would seem problematic since you'd have to somehow have the db trigger communicate to the spring boot app to allow eviction to happen.
One solution may be having both the jboss and spring boot app use a distributed caches, like ehcache with terracotta.
Related
I have a Spring boot application that uses JPA with Hibernate. It already connect to a primary data source for all transactions.
Now the task in hand is that I need to dynamically create Database Schema in a different database. For instance,
For project 1, create schema 1 in Database X
For project 2, create schema 2 in Database X
Later, these databases will be used by others externally. I am looking for the best way to get this done.
In case of defining multiple datasources in spring boot project, you have to declare associated beans in your configuration. With spring.jpa.hibernate.ddl-auto=create it will automatically create the schema in right database as answered here
Specification
Each tenant has their own database which handles users in greater detail, and there needs to exist a central database which handles:
Tokens (OAuth2)
Users (limited level of detail)
Mapping users to their database
Problem
I've found solutions for multi-tenancy which allows me to determine the datasource depending on the user. However, I'm not sure how I can also link certain crud repositories to this central datasource, and others to variable datasources.
Another solution involved updating the properties file, and using a configuration server (i.e. via git) to trigger #RefreshScope annotated configs. Though I'm not sure if this can work for Datasources, or if this could cause problems later on.
Extra Context
I'm using Spring Boot and Hibernate heavily in this project.
This blog gives a very good tutorial on how to do it.
After a lot of research it looks like hibernate just isn't built for doing that, but by manually writing the schema myself I can inject that into new tenant databases using native queries.
I also had a problem with MS Server DBs, as they don't allow simply appending ;createDatabaseIfNotExist to the JDBC URL, which meant even more native queries (Moving the project over to use MySQL anyway, so this is no longer a problem.)
My application is as follows:
Spring 4, Spring MVC, Spring Data JPA, Hibernate 5.0.12
DB2 as database
Websphere 16 as server
Also is my web application multi-tenant based on schema. The implementation is almost like this: http://stuartingram.com/2016/10/02/spring-boot-schema-based-multi-tenancy/
Everything is working fine with multi-tenant when a user does CRUD operations on entities but when I try to seed some data on startup (ContextRefreshedEvent) the data is not inserted to the schemas.
But a previous request if data is in there, gives the right results that no data is there. I need to add here that one schema has already the seeded data.
The procedure is as follows:
Set tenant and with this the schema of the database
check if repository count is 0
if this is the case, create a new entity
save the new entity
The result of point 1 is correct, and there is no exception when the entity is saved (3). But there is no data in the database after the seeding.
Also is the tenant and schema correctly set before the repository methods are called.
A normal saving after the startup in a schema is successful.
What could be the reason why saving data to different schemes is not working before the application started completely?
Edit:
I tested the Code also on a Tomcat 8.5.4 and it is working. So there is something different in the implementation concerning WebSphere in Spring/Hibernate I guess.
I have an application and it is deployed on four different container (jboss eap 6.2). I want that: when an instance of this application do an insert on a specific table all other applications should notify this insert to all connected user. I prefer to avoid direct connection from instance (ejb for example). It's possible using Spring data to osservare a table and to generate an event when someone do a insert? I need something like trigger? Can I have a equivalent full write java?. (DBMS: Oracle 12c)
Spring data itself does not have a mechanism to be notified of inserts made by another process to a database.
You need either a trigger on the database as you suggest, or a distributed event / caching framework, e.g. ehcache, whereby the container doing the insert would notify all the other containers of the insert.
do someone of you know a way to have 2 Databases running parallel?
We are using Hibernate 4 and as a main database Postgres 9.3 - this db is hosted on another machine then the application - if the database is down we still have to save some stuff.
So first intention was to write it into a csv, but I'm not a friend of writing stuff into an unordered file. So I want just to use a fallback Database (thinking of H2 Database). Does someone has experience with such a construct?
We are also using Spring 4 - I would just set up another datasource + sessionfactory + transactionmanager - and add the name at the #Transactional method to use the right manager. any other ideas?
Thank you!!
You can extend the Spring AbstractRoutingDataSource and configure two actual data sources:
a primary PostgreSQL data source
a secondary H2 data source
The application logic will see only one data source, which is the router which will decide which data source is going to switch to on demand.
When the primary data source is down you need to instruct the router to pick the fall-back H2 one.