Java Spring Boot - Connect dynamic multiple Database - spring

I'm confused about how to connect Spring dynamically to multiple databases, depending on the request?
For Example :
1. In admin site, register new member.
2. Database admin creates a new database for this new member.
3. Spring boot .jar, when in run mode, just adds some configuration lines to the external file, this service can connect to the new added database.
member 1 has url 1 => www.example.com/member-1 connect to db_member_1
member 2 has url 2 => www.example.com/member-2 connect to db_member_2
... etc
So I mean, I don't need to update my source code, but just add some lines in my external configuration file,so this spring boot .jar can connect to the new database.
I have tried connect two databases in 1 spring boot but I don't know how to make it dynamically without unloading my spring boot.
I use JPA to connect my master database and use JdbcTemplate to connect my transaction database.
Thanks in advance. :)
Regards,
Peter Susanto

Related

Spring Boot hook point / event after database connected

We have a Spring Boot 2.6 application with a PostgreSQL database. We use all the default database mechanisms (connection is defined in config via spring.datasource.* etc) - nothing special here.
Now we are looking for a hook point after the database connection was set up. We need to perform some checks before rest of the beans are loaded.
1. Spring start
2. Connect database
#. { hook point - to perform some db things }
3. Startup of other beans (which also rely on database)
Which hook point / Spring event / what else can we use to perform our checks?

Spring Boot: how to implement workflow as synchronous REST services integrating external applications

I'm here to ask you for a suggestion on which technology to use to add new features on an existing application based on Spring Boot.
The new features consist of some workflows, exposed as synchronous REST services, that must update the database and call REST services exposed by external applications, which consequently will update their own database.
For example, a service could implement this workflow:
insert rows in database
call the REST service of the application X, which will update its database
update rows in database
call the REST service of the application Y, which will update its database
update rows in database
This workflow must be synchronous and it is started from an human operator that will receive the outcome in few seconds.
If, for example, the step 4) fails, I need to:
make a compensation, calling another REST service of the application X, in order to undo what it made in step 2)
rollback the insert/update made in my database in steps 1) and 3)
Which technology, framework, tool or other would you use? In the past I implemented a similar scenario using Oracle SOA, but in this case I would avoid to introduce a new infrastructure in my application based on Spring Boot.
Thank you
I guess you need to learn a bit more about Spring Framework and Spring Boot.
1.insert rows in database : Spring Data JPA
2.call the REST service of the application X, which will update its database : A Http Client such as RestTemplate or WebClient
3.update rows in database : Spring Data JPA (again)
4.call the REST service of the application Y, which will update its database update rows in database : RestTemplate...
So so and so...
If you want to make a real workflow, you can use Activiti.

How to use Spring transactions with plain old jdbc connection without Spring jdbc template?

We have an old EJB based web application which we are supposed to migrate into Spring boot. For database connectivity, it is a plain old jdbc connection approach as of now.
We want to use Spring transactions and remove EJB transactions but willing to keep plain old jdbc connectivity same. In short we don't want to make changes in our DAO layer to convert plain old jdbc to Spring JdbcTemplate.
Please note that we have our own connection pooling algorithm and we create connection object and close it in the pool.
Along with this we want our application to be multi-tenant that can work with multiple databases on the basis of 'tenantID' that we will provide.
I actually tried to implement this but it is not working. I had to manually do con.commit(); and con.rollback();
Is there any way to use Spring transactions with plain old jdbc connectivity with above scenario?

How to read MS Access db(.mdb file) in Spring batch and load into mysql db

I want to read *.mdb file in Spring Batch and load into MySQL database.
There is no support for MS Access database format by default in Spring Batch. The first step is to see how to read data from a MS Access database using Java, then you can create an ItemReader and use it in a Spring Batch job.
Now in order to connect to a MS Access db from Java, you can see the following questions/answers:
Manipulating an Access database from Java without ODBC
How to connect java to Ms access Database
If you manage to create a jdbc DataSource pointing to a MS Access database, you can use the JdbcCursorItemReader (or JdbcPagingItemReader) and configure it with that DataSource (In this case there is no need to create a new reader).
Hope this helps.

Spring Data when does it connect to the database

I have been researching Spring Data Rest especially for cassandra and one of the questions my coworkers and I had was when does Spring Data connect to the database. We don't always want a rest controller to connect to the database so when does spring establish a connection if say we had a class extend the CRUDRepository? Does it connect to the database during the start of application itself? Is that something we can control?
For example, I implemented this example on Spring's website:
https://spring.io/guides/gs/accessing-data-rest/
At what point in the code does spring connect to the database?
Spring will connect to the DB as soon as the Datasource get initialized. Basically, Spring contexts will become alive somehow (Web listeners, manually calling them) and start creating beans. As soon as it reaches the Datasource, connection will be made and the connection pool will be populated.
Of course the above is based on a normal out of the box configuration and everything can be setup up to your taste.
So unless, you decide to control the connections yourself, DB connections will be sitting there waiting to be used.
Disagree with the above answer.
As part of research i initiated the datasource using a bean configuration and then changed my database password(not in my spring application but the real db username password)
The connection stays for a while and then in some point of time (maybe idle time) it stops working and throws credential exception.
This is enough to say the JPA does not keep the connection sitting and waiting to be used but uses some mechanism to occupy/release the db connection as per the need.

Resources