Use the same DAO for running same query across multiple databases? - java

I have a use case where the same native query : create user identified by passwd needs to be executed across multiple databases ( mostly oracle ) depending on the input from user. Will this require a separate dao (repository) for each database or can I create 1 DAO and re-use it across?
I am configuring the different databases via java-based configuration to get the jdbc-database url etc and have marked one of them as primary. I don't want to create multiple DAOs as all will contain the same piece of code.
If the same DAO can be reused, how do I specify on which database to run the query at?

Related

How do I write in two different databases in Spring Boot?

I need to make a REST API so two components can communicate with each other.
The flow starts like this:
Component 1 sends a request
The API processes it and, if everything's correct, writes inside Component 2 DB
Various data processing...
Component 2 sends a request
The API processes it and, if everything's correct, writes inside Component 1 DB
How do I make this appen in Spring Boot? I don't need any domain class, so I don't think I need to use JPA.
Update: I need to make it work with JdbcTemplate
Thanks in advance
I'm not sure what you intend to achieve here but this is what I would suggest you do if you really need to achieve this with JDBC template.
In your configuration file: e.g application.properties, you could specify keys to hold values used in configuring every different connection to the databases you need to interact with. A naive example could be:
app.datasource1.url=...
app.datasource1.driver=...
app.datasource1.username=...
app.datasource1.password=...
app.datasource2.url=...
app.datasource2.driver=...
app.datasource2.username=...
app.datasource2.password=...
You could create beans of these connections in a config class and differentiate them with names (qualifiers), one of them could be a tagged primary data source and the other a secondary data source. As an alternative, however, you can do 3 each time you need an instance of the DB connection.
Since you are using the JDBC template, in the service classes or implementations where you make calls to the database, you could start first by creating a connection (an instance of JDBC Template) before using it.
With this approach, you can create as many connections as you want to as many DB as you want. Don't know if this helps.

how can we use join concept in mongodb using spring framework

I am new to spring framework recent i have made small project on microservices, where i create two microservices
department service
User service
I need to know how can i use join in them, i have create one common field in both the service i.e departmentId,
when i use getmapping in user service containing department id fetching the data from department service in respective to that departmentId.
Using intellij, mongodb as database, spring framework,java
Since mongo is a document store type database.
It depends on how the data will be used. You'll need to think how the data will be queried, what will the response may be.
In a RDBMS, it is natural to denormalize your data and split it over several tables and use joins to create the views you need.
In a document store you do exactly the opposite you'll normalize your data and try to include as much as you can to satisfy most queries in one query.
When you use spring, you might also like to use https://spring.io/projects/spring-data-mongodb
If you want to gain in-depth knowledge on mongo, they have several courses available where they can teach you for free: https://university.mongodb.com/

Spring Hibernate JPA - Using many schemas

The company I am working for stores their client data in a separate database schema for each client. They indicate that this cannot be changed at this time. Is there an efficient way to pull data and update data in all schemas without configuring a connection for each schema? Everything I can find when I search seems to be talking about using one or a couple of schemas, but I need to use many (100+) simultaneously.
In any given persistence context, each JPA entity class is mapped to a specific base table. Whether and how easily you can access multiple schemas via a single DB connection is a function of your DBMS, your JDBC driver, and perhaps your particular database, but even a combination that in general supports the kind of access you would need will still not allow you to map the same entity class to multiple distinct base tables in the same persistence context.
You might be able to use the same entity classes for different clients by associating a different persistence context with each client, but that will not allow you use the same DB connection for all of them. Thus, if using the same connection were possible for you at all, it would require different entity classes per client.
Have you considered creating a new DB user and creating SYNONYMS for each of the tables in the separate database schemas ?
You could then map JPA entitys to the SYNONYM names that you have created..
Using this approach you could still use the one DB connection but with SYNONYMS to the DB tables in the other schemas...

Multiple databases with shared structure in JPA

I'm making a web service (in Spring Boot) with my friends where organizations can join and by joining each organization gets its own database, which is generated on the fly. The reason that every organization has a different database, is that the individual database consists of multiple tables that have unique data only for that organization and isolation is needed.
The database structure/schema should be shared among all companies and every change we do (for example add/edit a column or a table) should take effect in all the current databases without disrupting the already present data.
Also, we should be able to query every organization together to get a listing of booked times, names etc on one page with different organizations. Is it possible to achieve this with JPA or is there a better way to do this?

Relationships between databases with spring roo

Is it possible to create relationships (#OneToMany , #ManyToMany) between two entities in two different persistence units?
After following this set up http://viralpatel.net/blogs/spring-roo-two-database-configuration/
i want to see if an entity created with one persistent unit context can interact with another entity on another context.
If this isn't possible, do i just have to manage transactions on my own?
You cant have relationships across two different contexts/databases.
Some databases allow you to setup db-links. These create a virtual schema/db in the database which looks likes in the same db even though it is not. However, I have found these types of solutions to be slow and problematic.
Also, when dealing with 2 different db's, transaction management becomes more complicated. You can use the full 2 phase commit transaction management. Or you can use a custom transaction manager that works specifically on your 2 database instances.

Categories