I have a current requirement of allowing multiple data sources for the same repositories/entities in spring boot using jpa.
What I've managed to find was always related to multiple datasources, with multiple repositories/entites.
However, here I would like to have the same repositories and entities (ie same database structure) for multiple datasources.
And based on same property, programmatically activate a specific datasource.
I've tried the setup demo'ed here: https://github.com/spring-projects/spring-data-examples/tree/master/jpa/multiple-datasources
And in both configurations use the same package in the factoryBean.setPackagesToScan() call. This does now work however, one datasource overrides the other.
My current test can be found here: https://github.com/nWidart/spring-data-multi-datasource/tree/master/src/main/java/com/example/multidatasources
It shows the 2 configuration files (client1 and client2) with a repository and entity. The controller has 2 endpoints for both data sources (not working).
Thanks!
you can do like below :
1. application.properties (as default) : as you see, you can switch between datasource files.
2. application-test.properties (sample)
I hope this can help you. :D
Related
Is there a way to configure the H2 Compatibility Mode for the H2 Database that Spring Boot can auto configure to replace your regular database without just replacing it?
There are documented ways of disabling the autoconfiguration test database replacement:
https://stackoverflow.com/a/43557541/141042
I don't mind doing something like this, but most of the alternatives come with other complexities:
if you add a application.properties in your test classpath, this replaces your main application.properties during test runs, so then you're stuck maintaining two files (e.g. https://github.com/spring-projects/spring-boot/issues/10271)
if you set up a profile for test runs, then you have to make sure that any test needing the test database is marked with the profile
Is there a better way of doing this? I like the simplicity of the Spring Boot auto configured test database, but it seems like I have to force it into MySQL compatibility mode now to continue to work with my existing migrations.
So is there:
a way to configure the compatibility mode of h2 when spring autoconfigures the test database without disabling that mechanism?
a way of specifying the jdbc url for all tests without having to modify each test (e.g. to include a profile) or maintaining two application property files (e.g. a new application.properties in src/test/resources)
There isn't an option to set a custom URL for the embedded datasource that Spring Boot replaces in your tests. We offer a way to specify which connection type you want but that doesn't include the URL itself. I have no idea how easy we could add that but it's worth looking at least, I've created issue #19038
As for specifying the URL, you shouldn't add an application.properties in your test classpath for the reason you've mentioned. The SO thread you've referenced already has an answer that refers to application-test.properties.
I'm trying to configure a Spring Boot application with two datasources using same repositories and models, but when spring boot start says that repositories are already defined and can't create another on the bean container. That's true I mean.. I accept the error..but I would like to know if there is any way to do this. Below an idea of what I would like to do
Datasoruce(H2) for local deploy ----> com.myapp.repositories
Datasource(SQLServer) for remote deploy ----> com.myapp.repositories
My problem is that even if I'm doing the deploy on my local machine ..I need my remote datasource to do some operation.. In other world both local and remote Datasource connections should be up during local deploy.
I found actually three solution:
The first is #Profile the #Configuration one for "prod" one for "dev"..but in my case don't work.
Second one is to put different repositories package for each configuration. This work but I need to move all repositories when I go live.
Third solution is to do always two configuration and don't specify the basePackages on the remote Datasource. This work but I need to change this parameter when I go live.
Is there any other solution to do this?
Thank you for help.
Is there a 'community/Spring' approved method with Boot to have different datasource target for the same project?
Should I include both connector (H2 and Mysql) in the project dependencies and just change the jdbc url in my application.yml?
We are switching our tomcat instance to a Boot project, old habits of having jdbc jar as provided. I was wondering if this was still supported or desired in a boot fat jar/war exec.
If I understood Your question correctly, there are two scenarios, that You could be intrested in.
First is where You use both datasources at once in your project (ex. getting data from both H2 and MySQL in the same time, or one after another).
Second scenario is when You use two datasources but not at once, for example: H2 for test/debug project build, MySQL for production. Another sub-scenario is like You want something like primary/secondary datasources.
Solution for first scenario is to add two dependencies, disable Boot autoconfiguration (autoconfiguration won't work for multiple datasources) for databases and manually configure tho separate EntityManagers etc. (more info here: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html#howto-use-two-entity-managers)
Sorry that I can't provide any code sample but I can't access my work repo from home.
Moving to the second scenario what You can do is, use profiles.
You have to create separate profile and application-something.yml file for that profile. Inside you configure your second datasource, and then in dependencies you can make your second database dependency to add only with specific profile as well - but here im not 100% sure, I don't remember how we did it in work ;d.
And again, I cant paste any example but here is some help:
Profiles,
Profile-specific configuration files
An then there is a sub-scenario that I mentioned earlier. Marking datasource as #Primary But here I've never used it, I just know it exists: Link
Edit2: After some rethinking, I think this is the way to go with Boot and active profiles: Spring Boot Maven Plugin
Sorry for a lot of spam, and reconsiderations. That was quite confusing for sure.
Hope that helps,
I am building a stand alone java application using Spring JPA frame
work. I am able to access the DB in below scenario: if I give the
DB details in application.properties file as
spring.datasource.url=******** spring.datasource.username=******
spring.datasource.password=******
then it's working properly.
but I have to create two DB connections in the same application so,
I changes the names as below
spring.Datasource1.url=********* spring.Datasource1.username=******
spring.Datasource1.password
spring.Datasource2.url=************ spring.Datasource2.username=****
spring.Datasource2.password=*****
then it's not working.
Can you please provide the solution for it?
I have uploaded my code base in below location.
https://github.com/nagtej/MultipleDataSource
This might be helpful to you http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-two-datasources
Also, to connect to multiple data sources you would need to manually configure a DataSource, EntityManagerFactory and JpaTransactionManager.
For this, you can have a look at code placed at https://github.com/spring-projects/spring-data-examples/tree/master/jpa/multiple-datasources
Another good example for this is shared at http://xantorohara.blogspot.com.au/2013/11/spring-boot-jdbc-with-multiple.html
I use liquibase to set up my database schema. I disable hibernate to create anything. Because of that, my import.sql is ignored. Is there any way to configure spring boot or liquibase or any other part to load test data after liquibase has created the tables?
If you need something crude (i.e, not for actual data migrations), you can use Spring's JDBC initializer in Spring Boot. In a nutshell, you'll need to:
create a data-test.sql to load your data, and place it in your src/main/resources directory. For different environments, just use the naming convention data-{platform}.sql
add applications-test.properties to src/main/resources with the following:
spring.datasource.platform=test # this actives data-test.sql above
spring.datasource.continueOnError=???? # depends on your needs
to active the application-test.properties during your testing, make sure "test" is one of the profiles that's active during your integration test. One way to do this is to annotate your test class with #ActiveProfiles({"test", ...}).
The simplest way seems to load the data with liquibase. You can do it with a normal Changeset (XML or JSON) or a Changeset in SQL-Format.
The most common way is to load CSV-Data or run an existing SQL-File.