Perform search operations in multiple Databases in Spring Boot project - java

My Question- I have mongodb installed in ubuntu server and it has multiple databases and I want to connect to mongodb installed on server and get all the databases (name of databases, collections of every databases) and perform search operations on all the databases.
Note - I did not know the names of database

It won't work out-of-the-box. If you include the spring-boot mongodb starter project, it will look for the property 'spring.data.mongodb.uri' to connect to a single database, and if it does not find it, it will try to connect to 'localhost:27017'. This single database will then be used for all Spring Data Repositories automatically.
You can add extra MongoClient beans for different databases, but you'll need some work to connect different Spring Data Repositories to those different beans.
And if you want to work with a dynamic set of databases, i.e. when you don't know which databases or how many, you can't work with fixed MongoClients as spring beans anyway. You'll need some sort of factory that creates multiple MongoClient beans based on the number of databases, and multiple SearchRepository beans connected to each of those MongoClient beans.
In depends on the complexity of your project, but for this usecase I would not use Spring Data at all, but stick to the MongoDB Java client API:
Map<String, MongoClient> clientsPerDatabase = new HashMap<>();
// Setup
MongoClient mongoClient = new MongoClient(/* default database */);
MongoCursor<String> dbsCursor = mongoClient.listDatabaseNames().iterator();
while(dbsCursor.hasNext()) {
String databaseName = dbsCursor.next();
if (!"admin".equals(databaseName)) {
clientsPerDatabase.put(databaseName, new MongoClient(...+databaseName);
}
}
// Query
for(MongoClient client: clientsPerDatabase.values()){
...
}

I'd configure the Spring Boot application with admin access to the database and then use native queries to retrieve info on all existing databases, schemas, etc

Related

Ingore flyway for multiple datasources

So I have two Springboot applications that are independant. Both are using distinct mariadb databases with distince schemas names.
To make it simple:
For some reason, my app1 has to be able to access db2 data. Ive been able to add a second datasource in my app1, however, due to differences between flyway and liquibase, it fails to start.
How can i do so that my flyway configuration is ignored for datasource 2 (db2). App1 should just access data (read/write whatever) but the whole database structure is handled by App2.

Java and hibernate one application separate databases

I have been trying to find a solution but with no luck.
I have an Java application (Tapestry) and Hibernate to connect to database. Now there are 3 companies that want to use the same instance of application but different database.
What I have to do is depending on company that accesses the app use different database.
You can load different config Files in Hibernate Depending on the company. Just have the Hibernate Config files in Different location per company and configure session from different databases using:
File f = new File("C:\\company_name\\hibernate.cfg.xml");
SessionFactory sessionFactory = new Configuration().configure(f).buildSessionFactory();

How to connect akka(java) with RDBMS

I am new to Akka and want to create a CRUD application by using any RDBMS. I have gone through the akka-persistence which is quite confusing for me. But it lacks any implementation code.Can anybody point me to the relative links/ git hub repository which will help in understanding.
Till now I have created a simple hello world application.
I suggest to use Slick. There are several supported RDBMS connectors such as Postgres, Sql Server (Mssql), Mysql, Oracle, DB2. Previously I had to connect to an Oracle 12 database.
I added the dependency (in my case it was gradle) which can be found in docs.
"com.lightbend.akka:akka-stream-alpakka-slick_2.11:<version>",
After that I configured the slick as you can see below.
slick-oracle {
profile = "slick.jdbc.OracleProfile$"
db {
dataSourceClass = "slick.jdbc.DriverDataSource"
connectionPool = disabled
properties {
driver = "oracle.jdbc.OracleDriver"
url = "jdbc:oracle:thin:#//localhost:1521/xe"
user = user
password = password
}
}
}
Akka persistence allows you to persist the internal state of stateful actors in your system. Its rather convoluted to use it for CRUD applications, Instead you should start looking at a orm solution such as slick. You can use akka-http or spray(deprecated) to create a REST API that enables you to expose CRUD endpoints.

Multi tenant architecture in spring mvc with jpa

I stucked with a serious problem in architecture of my enterprise application. My current application is a web application using spring framework 3.2+ and jpa 2.0. Now I need to support multi tenancy in current application.
My requirement is that when a user logged in to the system the data for the user should be served from respective database. In short I need multiple database support which may cause different connection string. So how can I connect to database dynamically?
My another problem is that tenant (particular client of the applications) can register himself on the fly, and on successful registration I need to create a environment containing database creation and initialization etc. for that tenant and on successful creation of environment users of tenants are able to access the application. So problem is how to create environment dynamically, how create EntityManagerFactory dynamically?
Any suggestions to achieve the multi tenancy are most welcome...
In a web environment, the multi-tenancy can be implemented with the creation of tenant-specific aliases to be defined in your /etc/hosts file or Windows\System32\drivers\etc\hosts in order to map tenant-specific hosts to the same servlet container.
Once you've done that you can define a servlet filter which gonna read the current used host and compare it to another mapping, to be defined in a properties file, for example, and populate a HTTP session parameter indicating the current tenant.
Here you've got the tenant in the session.
You have to target a common database and a specific 'tenant_database' table. This table must hold the database name, the password, the port, etc. All the datasource information to establish the connection to a targeted tenant database.
You can build an entityManagerFactory at this point:
Map<String, String> properties = new HashMap<String, String>();
properties.put("javax.persistence.jdbc.user", "admin");
properties.put("javax.persistence.jdbc.password", "admin");
EntityManagerFactory emf = Persistence.createEntityManagerFactory(
"jdbc:mysql://localhost:3306/myDB", properties);
You just have to think about obtaining it from the right place now.. :)
Hope it helps

Connecting two datasources to the same database

I am working on an application where we have decided to go for a multi-tenant architecture using the solution provided by Spring, so we route the data to each datasource depending on the value of a parameter. Let's say this parameter is a number from 1 to 10, depending on our clients id.
However, this requires altering the application-context each time we add a new datasource, so to start we have thought on the following solution:
Start with 10 datasources (or more) pointing to different IPs and the same schema, but in the end all routed to the same physical database. No matter the datasource we use, the data will be sent to the same schema in this first scenario.
The data would be in the same schema, so the same table would be shared among datasources, but each row would only be visible to each datasource (using a fixed where clause in every CRUD operation)
When we have performance problems, we will create another database, migrate some clients to the new schema, and reroute the IP of one of the datasources to the new database, so this new database gets part of the load of the old one
Are there any drawbacks with this approach? I am concerned about:
ACID properties lost
Problems with hibernate sessionFactory and second level cache
Table locking issues
We are using Spring 3.1, Hibernate 4.1 and MySQL 5.5
i think your spring-link is a little outdated, hibernate 4 can handle multi-tenancy pretty well on it's own. i would suggest to use the multiple schemas approach because setting up and initializing a new schema is programmatically relativly easy to do (for example on registration-time), if you have so much load though (and your database-vendor does not provide a solution to make this transparent to your application) you need the multiple database approach, you should try to incorporate the tenant-id in the database-url or something in that case http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch16.html

Categories