Change spring data r2dbc context to use R2dbcRepositories in a different database - spring-data-r2dbc

How can I change spring data r2dbc repositories target-datatabase (maybe I am talking about 'Context')?
Let me explain better.
I am managing(ConnectionFactory Properties) 'connection factory' to execute SQL-DDL scripts in any database I want - OK; however, R2dbcRepositories (REactiveCruRepository) methods, are not follow this database I have configured ConnectionFactory Properties.
How Can I change ConnectionFactory Properties, in order to, REactiveCruRepository points for this configuration done (meaning a new database)?

It is very easy to configure another connection factory for your R2dbc Repositories.
Check my example about multiple R2dbc connection factories.

Related

One repository to two databases, MongoDB and Spring Boot

I have a repository which I want to save inside two different MongoDB databases, programmatically.
If the user enters an URL with the parameter DB1, the repository will save inside the database DB1, if it is DB2, to the database DB2, etc.
Is there any way to do this?
Not automatically. You need to have the application connected to 2 DBs and call each one depending on the parameter that comes in from the request. You'll need two separate repositories as far as I'm aware.
It is not possible to do with repositories easily (use multiple repositories with a little modification, because we can't use one, for each parameter in the URL is a madness.
So to avoid a lot of duplicate code we have to use the Java Driver.
MongoClient mongoClient = new MongoClient("localhost", 27017);
//here we can change the database name
MongoOperations mongoOperations = new MongoTemplate(mongoClient, database);
mongoOperations.save(YOUR_POJO);
mongoClient.close();
The POJO object has to use de #Documentannotation, if not you will have codec problems. Here you can solve them: http://mongodb.github.io/mongo-java-driver/3.2/bson/codecs/

Creating Datasource Dynamically in SpringBoot OpenJPA Application (Implementing Multitenancy with OpenJPA)

I am creating a Springboot application with OpenJPA.
My requirement is that I need to connect to multiple datasources dynamically and the datasource credentials are obtained at runtime by calling some rest-endpoints.
Here is the controller class:
#RestController
public class StationController {
#Autowired
BasicDataSource dataSource;
I have a service which returns me the jdbc_url depending on the customer name:
public String getDSInfo(String customername){
// code to get the datasource info (JDBC URL)
}
My questions are:
Is there a way in which I can create datasources at runtime by getting datasource credentials by calling some other service (which takes the customer id and returns the customer specific datasource) ?
Since my application is a web based application, many customers will be accessing it at the same time, so how to create and handle so many different datasources?
NOTE:
The code will get information about the customer specific data source only by firing some service at the runtime, so I cannot hardcode the datasource credentials in the XML configuration file.
I found some implementations with Hibernate but i am using Springboot with OpenJPA. So need OpenJPA specific help.
It sounds like you want a multi-tenancy solution.
Datasources are easy to create programmatically, just use a DataSourceBuilder with your connection details pulled in from a central source (e.g. a central config database or Spring Config Server).
Then you'll need to look at a multi-tenancy framework to tie the datasources back to clients.
See here:
https://www.youtube.com/watch?v=nBSHiUTHjWA
and here
https://dzone.com/articles/multi-tenancy-using-jpa-spring-and-hibernate-part
The video is a long watch but a good one. Basically you have a map of customer data sources (from memory) that allow an entityManager to pick up a datasource from the map by using a thread scoped custom spring scope of "customer" which is set when a user for a particular customer somehow logs into your app.

Spring Data JPA | handle multiple DB without entityManager, DataSource and TransactionManager

I've read several examples ex1, ex2, ex3 for using several database on Spring Data (JPA), all of them suggest creating one Configuration file for each database, and three bean (EntityManager, DataSource and TransactionManager) for each entity. Is it the only way we can handle multiple database or there is easier way to do this ?

How to read the schema used by a JPA implementation

My EntityManager is using a persistence unit that uses a data source provided by our Websphere configuration. The DS configuration includes an environment specific DB to use.
The EM successfully uses this schema, but I can't figure out a way to log or display the schema being used. I was thing something like em.getCurrentSchema would be available..
Any help would be great, thanks.
No API to do this (in JPA). You could do it via JDBC and use of DatabaseMetaData.
JPA is to provide an object view of the data and ease persistence of those objects, not to just present datastore specifics to the user.

JPA build entity manager with a specific JDBC connection instance

I want to manage the database connection outside entity manager context so that I can use it for different entity managers. The question is how can I build the entity manager factory or entity manager with my own connection instead of providing it with properties in persistence.xml?
In either case the answer is that you cannot, in SE you can specify the db connection properties when creating the EntityManagerFactory, but the db connection is still maintained by the EntityManager and for good reason, if you controlled this you could commit and rollback behind the EntityManager's back. If you gave the same db connection to multiple EntityManager's this would be chaos indeed, I am not sure why on earth you want to do this.
The best I can suggest is that you look into the EntityManager.getDelegate() (JPA 1.0) or EntityManager.unwrap(java.lang.Class cls) (JPA 2.0) methods they may return the underlying provider (ie. Hibernate) object which you may be able to pry the database connection out of, but you certainly won't be able to replace it.
In short really bad idea.