Configure Multiple (Routing) Datasources in Spring Boot + Spring Data And Switch Them At Runtime

  Рет қаралды 7,589

Cyecize

Cyecize

Күн бұрын

Пікірлер: 36
@Cyecize
@Cyecize 2 жыл бұрын
An issue with dialects was found after I recorded the video. This commit fixed the issue: github.com/Cyecize/Spring-Data-Routing-Datasources/commit/184ebcd1b31944081e66000fc1ef8ea543224754 If you only want to use one type of SQL Server, then this will not affect you!
@ShwetaSingh-je1mu
@ShwetaSingh-je1mu Жыл бұрын
What was the dialect issue can you please elaborate, because I might be facing same issue
@Cyecize
@Cyecize Жыл бұрын
@@ShwetaSingh-je1mu The issue is that once you select a dialect for hibernate it will use that same dialect even if you change the connection. So when I change the connection from MySQL to MSSQL for example, it uses the MySQL dialect, which does not work well with MSSQL. Regardless, if you get the latest version from my github page, you will now have this issue.
@ShwetaSingh-je1mu
@ShwetaSingh-je1mu Жыл бұрын
@@Cyecize Thanks for your reply. I was facing the same issue and implemented your solution it worked, but there was another issue: I am using liquibase and after I implemented your solution liquibase is having issue while table creation only for postgres db. Can you help me with this?
@Cyecize
@Cyecize Жыл бұрын
@@ShwetaSingh-je1mu I've only used flyway, but I guess the idea is the same. The logic for applying DB migrations should not be dependent on spring data, so you can just pass the connection that it needs. Additionally, when I use multi DB, I keep 2 copies of the same flyway file. Eg. v1_mysql_some_updates and then v1_posgres_some_updates This way I can choose which migration to apply based on what DB I am running
@ShwetaSingh-je1mu
@ShwetaSingh-je1mu Жыл бұрын
@@Cyecize Thank you so much!
@vut9690
@vut9690 Ай бұрын
This is EXACTLY what I am looking for my project at work. THANKS!!!
@Cyecize
@Cyecize Ай бұрын
And this is EXACTLY the comments I was hoping for when I did this back then ♥
@huutuan209
@huutuan209 3 ай бұрын
Thank you very much, your lesson still valid to this day
@maurocaredda5717
@maurocaredda5717 2 жыл бұрын
I like this implementation, i added some logic to manage the switch via header with an interceptor and works flawless, thanks a lot
@shoaibpatel6461
@shoaibpatel6461 2 жыл бұрын
I am looking for similar implementation where switch is managed based on header value. Can you point to some material on this?
@maurocaredda5717
@maurocaredda5717 2 жыл бұрын
hi, sure i posted an example code in my GitHub , you can check here github.com/Mauro2888/spring_multi_datasource
@UlquiorraEspada88
@UlquiorraEspada88 2 жыл бұрын
It's possible to enable the annotation at service class level for all methods ?
@Cyecize
@Cyecize 2 жыл бұрын
Of course, you just have to code it that way :D
@sachinacharya6740
@sachinacharya6740 2 ай бұрын
i don't want to set a default datasource in abstract routing datasource, how can i implement it
@TsvetelinYakimov
@TsvetelinYakimov 2 ай бұрын
I did this long time ago so I don't remember if it could be possible but you can always have a dummy connection as a default. You can set up empty in memory database database to use just to boot up spring
@sachinacharya6740
@sachinacharya6740 2 ай бұрын
​@@TsvetelinYakimov my case here is i have a parameter based on which i want to select specific datasource where the entity must be saved/updated i dont want any datasource as primary or default, all my datasource will be using the same set of entities and repositories
@EliSanchez99
@EliSanchez99 Жыл бұрын
It helped me a lot, thank you very much!
@artshorts512
@artshorts512 3 күн бұрын
@Cyecize can we connect two database to one application , im trying to connect first one is primary database which have 3 month old data and second database which has old data than 3 month . i cant do changes in existing code. can i implement it without change in all repository, services packages bcoz there are more than 500 apis . please give me suggestion . two database for one package .
@TsvetelinYakimov
@TsvetelinYakimov 3 күн бұрын
I am not sure what logic you want to use to switch between dbs, but there must be at least one place to add the routing logic. I think it is possible with very little code change, but with no changes at all it will be hard.
@abdulrahmanelrawas2029
@abdulrahmanelrawas2029 Жыл бұрын
This is very helpful. Thank you.
@rajivperera1624
@rajivperera1624 2 жыл бұрын
Can you do a sample spring data app where application connect to database schema based on username and pwd dynamically as arguments when running the app. The system should have one db but multiple schemas based on username and password. I tried with spring data but was not able to achive.
@Cyecize
@Cyecize 2 жыл бұрын
Hey, Check out my other video kzbin.info/www/bejne/o5SroJSGfs5_bNk It's pretty much what you need.
@rajivperera1624
@rajivperera1624 2 жыл бұрын
@@Cyecize I watched this video and good work as always. But in that you have created dynamic datasource for different db types and it is pre setup by declaring the db types your going to set. What I am trying to achive is only one db but has multiple schemas and the jpa datasource should load them dynamicaly based on user input. Ex: spring cli app reads args where args are username and pwd and when user enters userXX and pwdXX it should go to userXX schema and if userYY it should go to userYY schema. This setup cannot be pre setup like one is for mssql, another is for mysql wise as their are hundreds of schemas. So I am trying to load datasource dynamically based on user input to the same db. What I cant figure is how to do this in JPA?
@Cyecize
@Cyecize 2 жыл бұрын
@@rajivperera1624 That is also shown in that video 😀 In addition to having the ability to connect to different types of DBs, you can also choose which schema to connect to. So if you strip out the unnecessary things you can end up with what you need. Ofcourse, my logic is to keep the connection per session, but you can modify the manager to make it so that it stores that connection based on some user or whatever else is needed. For your case I imagine a manager written something like this: - Have a list of schema names (loaded when the app is starting) - Have a Map to keep already established connections and lazy load new ones. - Use similar logic for swapping data sources as on this video or the other one. - Think of some way to hint which data source to use. Maybe header or body param.
@rajivperera1624
@rajivperera1624 2 жыл бұрын
@@Cyecize can you point what classes to be taken out of the backend code to achive this as for example I think I cannot drop classes like the entity manager you have written.
@0equalsTo1
@0equalsTo1 9 ай бұрын
can we use Map insted of ThreadLocal ?
@cyesoft
@cyesoft 9 ай бұрын
The idea of ThreadLocal is to keep a unique value per Thread. This code is called from multiple threads and we want to make sure different threads do not mix and stack transactions on the same place. The way we ensure that nested transactions for example are executed in order, is by ensuring that they are properly positioned in the stack. I can see a way we can do it with a Map, but that map has to keep track of the ID of the thread, then this map has to be synchronized or it has to be a ConcurrentHashMap. This adds quite a bit of complexity and ThreadLocal is in this case exactly the solution we need.
@rajatchaturvedi6393
@rajatchaturvedi6393 Жыл бұрын
@Cyecize i want to do same thing with mongodb...can u provide the direction
@Cyecize
@Cyecize Жыл бұрын
Hey, the solution should be pretty similar. Since we're using the repository pattern here, just replace the repository implementation with a mongo db one and you should be good.
@anwartan5076
@anwartan5076 2 жыл бұрын
Bro, the annotation doesn't working in controller
@TsvetelinYakimov
@TsvetelinYakimov 2 жыл бұрын
I'll check that and see why. But anyway, you should use this annotation on a service level really.
@anwartan5076
@anwartan5076 2 жыл бұрын
@@TsvetelinYakimov sorry, i mean when i run the service which has withdatabase annotation, it doesn't work properly and the result always follow withdatabase the first time run
@cyesoft
@cyesoft 2 жыл бұрын
@@anwartan5076 It is tested and it should work. Could you please share a link to your code, I will try to help out.
@alexkorn3692
@alexkorn3692 Жыл бұрын
Just add @Transactional in your Controller class
@xandrviking1113
@xandrviking1113 Жыл бұрын
So loud keyboard 😂
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 45 МЛН
Mom Hack for Cooking Solo with a Little One! 🍳👶
00:15
5-Minute Crafts HOUSE
Рет қаралды 22 МЛН
When you have a very capricious child 😂😘👍
00:16
Like Asiya
Рет қаралды 3 МЛН
Configuring Multiple Datasources in Spring Boot (Step-by-Step Guide)
24:49
Java Tech Solutions
Рет қаралды 1,8 М.
Spring Data JPA with Snowflake database
11:28
TechStack Brains
Рет қаралды 373
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 45 МЛН