We are building a multi-tenant application using spring boot, JPA (Eclipselink). We have done below steps.
Defined AbstractRoutingDatasource
Defined new Bean of LocalContainerEntityManagerFactoryBean for each tenant
Now in our code, we have two types of flavour.
#Persistencecontext
Both do not have a qualifier. Problem is we can not add qualifier example persistence unit name as my tenants will be dynamic.
Is there any way we can solve it. Means on runtime, it will attach the right entity manager with out hardcoding unitname in the persitenceContext
Thanks a lot
Related
How can I configure persistence.xml for production? My question specifically concerns the definition of parameters such as user, password and bank url, for example.
I would like to not include this data directly in the file, but in an environment variable, for example.
However, it can be any other approach, except overwriting the file settings via the entity manager factory as was said in these questions 1 e 2
This restriction exists because my EntityManager is injected via #PersistenceContext. This even generated a comment
I'm using a JavaEE application with JAX-RS, Hibernate JPA and Spring MVC
Hello fellow developers,
i created a library using the Spring boot framework.
This library is creating a dynamic database connection using #Beans where i create a "data" Bean which holds the unlimited Datasource beans provided by a Postgresql db. At the end i wanted to have a dynamic db connection which could be triggered from outside to change the db i want to connect to. The information of the different databases where stored as mentioned inside of a postgres. This is loaded at the application start into this bean. My Problem is, that i'm not able to switch between the different Datasource beans. Spring boot is creating them, but it seems like it's not possible to change the bean started at runtime of the application which only holds one of the unlimited Datasources... So also after a retriggering of the creation of the original bean it still uses the old datasource.
Is there a way to use the beans from spring boot and change them on runtime?
Regards,
Andreas
I believe you are asking for DB multitenancy support where tenants information is stored in a Postgres DB.
Configuring the persistent layer for multi-tenancy support involves configuring:
Hibernate, JPA and Datasources properties
Datasources beans
Entity manager factory bean
Transaction manager bean
Spring Data JPA and annotation-driven transactions
I recently blog about Multi-tenant applications using Spring Boot, JPA, Hibernate and Postgres and although the tenants data is stored in a yml "properties" file, it shouldn't be difficult to convert it to read tenant data from a DB. I think it would be a starting point for what you would like to accomplish.
In Spring Boot - is there anyway to only connect to the database when it is required the first time?
For example - lazy load the database setup?
I understand this is not the usual pattern but would interested in hearing if there is a solution to this
Thanks
Damien
n Spring Boot - is there anyway to only connect to the database when it is required the first time? For example - lazy load the database setup?
Spring Data and Hibernate can do that setup.
I wonder if you can use #Configuration and #Lazy at the same time, and the documentation suggests it is doable but it will create all the bean lazily.
Whereas, if you want to selectively create datasource bean lazily among other beans, then, in that case you need to that use #Lazy on datasource bean
#Configuration
#Lazy
public class YourDataSourceConfigClass {//datasource bean}
I use Flyway in my application and it is configured as Spring bean, which performs migration as init-method(example configuration can be found here in the bottom). Of course migration should be performed before any app's interaction with database.
That's why my datasource bean depend on Flyway bean. But i create Flyway bean conditionally using #Profile, because i do not want to create it using the same app's context in integration tests.
The problem is that when i use test profile which does not create Flyway bean, datasource instantiation fails with NoSuchBeanDefinitionException, because it depends-on="flyway" which is not created with current profile. Can i somehow make spring to ignore this depends-on dependency, if related bean does not exist? If no, what is the way to decouple Flyway bean from context when i use it under test profile?
I have looked over the code of OpenEntityManagerInViewFilter and I noticed it looks for one entityManagerFactoryBeanName defined on my Spring context. I do have a setter to adjust a different bean name case I wanted.
But let's suppose that I had on my application multiple EntityManagers, one for each of my DataSources defined. Spring can define repositories to lead data for those EntityManagers, but how can I inform the OpenEntityManagerInViewFilter about this behavior?
It seems the code is only ready to treat one EntityManager by time, so I would not be able to keep lazy data for different repositories on my application.
Is there any way to set it up? I'd appreciate any idea.
OpenEntityManagerInViewFilter source code:
http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-orm/4.2.0.RELEASE/org/springframework/orm/jpa/support/OpenEntityManagerInViewFilter.java#OpenEntityManagerInViewFilter
Cheers,
After looking over many codes, I found out we need to create a filter for each entity manager with your datasource. So if we have a context root application, we will have many filters being applied for this context.