Java and hibernate one application separate databases - java

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();

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.

Perform search operations in multiple Databases in Spring Boot project

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

How to access multiple database schemas from single persistence unit?

I am working on a project in which currently I have single persistence unit file as I have only one database schema there in my db. Now I need to separate that schema into two different schema. So I made two different ORM files and mapped it into the PU. Now when i build my EJB project its working fine but as soon as I build my WEB project it starts giving me compilation error.
So, is there any other way so that I can manage two different schema together??
Note that both the schema are related with foreign keys.
Please help me out.
If you are using Oracle and you have SCHEMA_1 and SCHEMA_2 and you can define synonyms:
As SCHEMA_2, grant the appropriate privileges to SCHEMA_1
Define synonyms in SCHEMA_1 for the tables in SCHEMA_2
Now in SCHEMA_1 you should be able to use SCHEMA_2 tables as if they were there

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

JPA persistence.xml data-source for SE app

Various sources (e.g. Oracle) say that you have to specify jdbc connection details in your persistence.xml for Java SE apps which use JPA via things like <property name="javax.persistence.jdbc.url" value="..."/>.
This is a real pain when you want to run the app against different databases, for example local, test and production. I currently get around this by having multiple persistence-units that are all effectively the same but with different connection details, and then get the app to pick the right persistence-unit based on the environment that it is running in.
The problems with this are:
Duplication of config. When I add an Entity class I have to add <class>MyClass</class> to every persistence-unit. I would rather just specify it once.
Database connection config packaged with the app. If I want to change what database is being used in an environment, I need to fiddle with persistence.xml and re-build the app. I would rather have a config file in each of my environments which specifies the database credentials. That way, I could have many environments but just a single persistence-unit defined, and I could change the database credentials for a given environment by just editing one file in that environment and then re-starting the app.
Do you know of a better way to configure persistence? Is there some way of getting <jta-data-source> or <non-jta-data-source> to do something appropriate in a Java SE environment?
U can config it manually in code
Map<String, String> props = new HashMap<String, String>();
props.put("javax.persistence.jdbc.url", "YourURL");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("UnitName", props);
EntityManager em = emf.createEntityManager();

Categories