manage many database - java

I search a way in a spring 3.x application to manage many database.
Now we support mysql, but we need to support firebird and postgress sql.
we don't use orm.
what is the best way to support many database with spring?

You'll have multiple data sources - one for each JDBC driver you need. You'll have to find a way to select which one you want for a give case.

Once you define a data source per database, as duffymo said, define a transaction-manager for each, and you can use #Transactional("managerXname") for declarative transaction demarcation.

Related

Mixing jdbc with hibernate?

Background: I am working on a web application, which uses oracle jdbc and access database with java code and also sql queries which will be executed by our engine.
Now I am thinking about "replacing" jdbc over the time with hibernate t have -let's say - hibernate advantages.
My question is, can this be done step by step?
Can I start to create new tables with hibernate and work with hibernate without having risk with the existing database structure? can hibernate works in parallel with jdbc?
The answer is positive.
You can have both jdbc and hibernate data-sources in the same project, no problem.
Just be careful not to mix up those two data-sources, that's all.
Yes, JDBC and hibernate both can be used in parallel.
However, it depends on use cases. As per your question you just want to "replace" JDBC with Hibernate.
So, yes in this case you can start with :
creating new tables
creating hibernate mapping files
creating DAO layer
implementing hibernate code.

Spring Application JPA and JDBC

I am starting with a new project and currently evaluating whether to use JPA or JDBC. Our operations mostly are going to be an bulk-insert and bulk-read and very rarely single insert/read.
I checked a prototype with JPA and JDBC and realized that both has its own merits and limitations.
Considering the current use case that for a fact I will only have always a bulk read and bulk write, which one will be a better option to go with ?
Spring JPA Repository gives a simple method save(Collection) which can take a collection and save as well.
Also Validations are also not be considered here, as the payload will already be validated in the layers above and the database layer should just do the read/write operations.
Does the JPA save(Collection<>) method in turn uses the jdbc templates or is it entirely a different implementation ?
Thanks in advance !
We use both JPA and JDBC in one application wiht no problem. Preferably we use JPA/JPQL and fall back to JDBC if needed. For JPA we use Spring Data JPA and for JDBC Spring JDBC Template.

How to switch between db in hibernate?

I have two databases defined in my web application. One for fashion and one for sports. Currently Im accessing the sports database. Now that I want to change it to fashion database means how can I do that ? I'm using spring with hibernate. Is there any in-built support for this either in spring or hibernate ?
EDIT 1:
I will have a web page where I can choose which database to use and then the changes should get reflected automatically. Is there any way to do it ?
FYI : Both the databases will have the same design. Only the data will be different
Hibernate specific configuration may differ slightly, but with JPA you need to have two EntityManagerFactories configured, one for each database. Then you would use either one depending on which database you want to access.
You can't do this with a single EntityManagerFactory (or the pure Hibernate equivalent).
I think this is an answer to your question. You can simply add a second PersistenceUnit and create a second Factory for your Entity Managers for it.
I hope this helps!
Kind regards
Mysql only has one database. It has many schemas (which it calls databases). You can query a table in another schema by prefixing it: schema_name.table_name .

Support JPA and MongoDB in the same application

there exists a requirement which sounds quite simple: support a couple of RDBMS (which i intend to do by using JPA) and MongoDB (spring-data-mongodb is preferred) for persistence. More precisely either the one or the other has to be configured and used, i'm not talking about a cross store.
The procedure shall be the following: code the application, deliver the .war to the customer, in a config file the customer puts the persistence information like the databaseurl (i.e. either mongodb:localhost/test or jdbc:oracle:thin:1521#foo).
Additionally it would be nice to extend the implemenation for further datastores like couchdb.
Is there a best practice or at least any of a non-too-much-overhead-solution which is not that dirty?
Is Eclipselink an option? The latest supports JPA for both RDBMS and NOSQL (including Mongo)
https://blogs.oracle.com/theaquarium/entry/jpa_and_nosql_using_eclipselink
I am currently developing a project with similar needs. I can advise you according to my experience.
I believe that the major concern here is not regarding the technology but more regarding how you will structure data. For that I advise you to use the AbstractFactory and FactoryMethod design patterns. Regaring technology I am using Morphia for MongoDB and JPA for MySQL (as an example) and it's working like a charm.
So the easiest way is to create interfaces for all the objects you want to persist, and then do an implementation for MongoDB with Morphia tags and another with JPA tags. Create one factory for MongoDB that will deal with all the CRUD operations in the MongoDB objects and do the same with a JPA factory.
When the application is starting, you only have to verify the user choice for persistence and then initialize the corresponding factory.
DataNucleus JPA allows you to persist to RDBMS, MongoDB and a host of other datastores (LDAP, HBase, AppEngine, Neo4j, etc), with a simple change to the connection URL, and has done so for quite some time

What are best ways to implement a single functionality to store data in different databases based on configuration?

I want to implement a sample in java that reads a configuration from some config file and, based on that, when user interacts with the page the application will store some data on either MySql or Oracle according to the configuration parameters. How can we implement this sample in most efficient and smart way?
Indeed using Hibernate or JPA allows you to abstract the database differences away.
With a dependency injection framework like Spring or Guice you can then create 2 service instances which differ only in the persistence manager which is injected.
In this case you can keep almost 100% of the code identical for the 2 databases which guarantees they will not get out of sync over time.
Make an interface for data storage, and use that in your application
Make an abstract class which implements this interface and implements the functionality which MySQL and Oracle have in common.
Make two classes, one for MySQL and one for Oracle which implement the database-specific stuff.
In your configuration, specifiy which class to use (MySQL or Oracle database class).
Maybe use something like Hibernate, which abstracts the database away from you.
Following up on #Sjoerd's answer:
Any tools or open source libs that can be helpful?
Hibernate or some other JPA implementation is your best bet.
Alternatively, Spring has some JDBC support classes that do some of the work ... if you can figure out which of the various alternatives is a good match to your requirements.
Unfortunately, implementing an application that works against multiple database backends is hard work, no matter how you do it. In my experience, you usually end up with a solution that doesn't perform as well as a solution that is tailored to one and only one database back end.
If I had my way, database vendors who refuse to implement the SQL standard would be first against the wall ... come the revolution.

Categories

Resources