Hibernate with multiple databases in the same application

Asked

Viewed 1,204 times

3

A system uses for each client a separate database, let’s say I will have 500 clients, in which case my database server would have 500 data bases, example:

cliente1 

cliente2 

cliente3

In the Hibernate configuration a Factory where in these settings contains in which bank the system will connect.

There will be 500 Factory in the application?

  • You want to know if you’re gonna keep 500 instacing of Entitymanagerfactory?

  • I’m using only Hibernate and not JPA, in the same application I want to connect say the 500 mysql database, I wanted to know if it had how to create only a Factory and from that I connect to 500 database

  • 1

    I would not like to create a Factory for each connection to the database, this will detonate the server memory!

  • Consider showing your code (Hibernate setting for more than one database and session creation). Tip: You can create the contexts of on-demand data access and destroy when inactive, so you’ll only have 500 contexts if all customers access it at the same time. If the 500 simultaneously connected clients do not compensate for the infrastructure investment, you can reconsider your database architecture (other than per client separation and other types of non-relational databases).

  • So we can conclude that we can not only have a Factory and use it for several databases, but for each database I will have to give a Factory = config.buildSessionFactory(); ie, it is totally unfeasible in my case, remembering that can have more than 500 clients!

  • 1

    If when you say "Factory" you mean "Sessionfactory," yes, it is one for each database. Sessionfactory maintains a connection pool for a given database. You can measure to know in advance how much memory and other infrared resources you will need. 500 Databases or 500 simultaneous connections on an SQL server can cost more than 500 instances of Sessionfactory.

  • Blz Caffe, then I’ll do a simulation and anything put the result here, valeww

Show 2 more comments

1 answer

1

We have a similar situation in Nhibernate (.NET) due to the architecture of our system, which uses a schema (database) for each branch.

In this case, you can use a SessionFactory only, assuming the responsibility to create the connections.

For that, you must:

1) Create an inherited class from org.hibernate.connection.DriverConnectionProvider (example in C#):

internal class MeuDbConnectionProvider : DriverConnectionProvider
{
    protected override string ConnectionString {
        // To-Do: Aqui você deve retornar a String de conexão baseada em uma variável
        //        'static' (neste meu caso, chamei de "SessionManager") que provê a base em que vc está conectado no momento
        get { return SessionManager.CurrentDatabaseConnection.ConnectionString; }
    }
    public override System.Data.IDbConnection GetConnection()
    {
        // To-Do: Criar uma nova conexão MySql baseada na String de Conexao
        //        da "base corrente"
        (...)
        return novaConexao;
    }
}

2) Set Hibernate to use your Connectionprovider for connection creation.

Configuration configuration = new Configuration()
    .setProperty(Environment.CONNECTION_PROVIDER, "com.my.package.MeuDbConnectionProvider");

From then on, power will be in your hands - using only one SessionFactory.

Remarks:

  • What would be the advantage of the AP in doing this? In . Net, ADO.NET maintains a connection pool - it reuses a pool connection when you request a new one; it does so based on the connection string. In Java, it is Hibernate himself who maintains the connection pool through an internal implementation of the Connectionprovider interface that you are suggesting is replaced. If you use this design in Java ("Create a new connection..."), what will happen is that there will be no connection pool; every new request a new connection will be opened and the cost will be absurdly high.

  • @Caffé: The problem is not the connection itself, but the object ISessionFactory. This object is extremely costly to create and maintain (time and memory, respectively). Create a ISessionFactory just by varying the Schema of the database implies a overhead unnecessary. About the connection being kept "open", this is not true: (N)Hibernate closes the connection when it is no longer needed (provided the object ISession has been created without a predefined connection).

  • 1

    I do not want to discuss it at length here but the question of the PA is important and an answer like yours can disorient those who arrive here. Some of your mistakes: Isessionfactory is not an object, it is an interface. A Sessionfactory in Java has a reason to be costly: opening connections to the database and managing these connections is costly - there is no other "overhead". Finally, I didn’t say anything "about the connection being kept open" so I didn’t understand what you meant. A hint: do not answer Java question in . Net ignoring the important differences between these platforms.

  • Just for the record, I’m still using a Sessionfactory for each connection, I haven’t simulated 500 connections to see memory consumption (rsrrs fear). The 1 Sessions Factory scheme for each client is working, if necessary I will have to increase the server capacity in terms of memory mainly, until I find another solution!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.