views:

74

answers:

3

Hi,

I have 4 web applications, that have a common reference to an Hibernate implementation but that run on different Tomcat instances. And so, for example, a connection pool configured in Hibernate with a min size of 3 (with c3p0), will result in 12 connection open (3 for each instance) when all the projects are running .

I'd like to "share" the Hibernate implementation with the instances (and so to have always 3 connections open instead of 12), and I was wondering about the best solution to achieve it.

Any tips?

Thanks

A: 

You can't do it. Each Tomcat instance runs in its own JVM, so you can't share object or classes between them.

Why don't you like 12 DB connections? Modern database systems are capable of handling millions of connections.

iimuhin
+1  A: 

One approach could be to create an additional backend service which runs Hibernate and which is connected to the frontend services via a Web Service. This way there would be one Hibernate instance.

Timo Westkämper
This would remove the multiple `SessionFactory` instances (but introduce extra overhead for the remoting) but won't lower the number of connections required to serve concurrent requests from the 4 client applications (the sum of all max).
Pascal Thivent
True, there's some remoting overhead, but this is a way to share one Hibernate instance for multiple frontends.
Timo Westkämper
+5  A: 

I'd like to "share" the Hibernate implementation with the instances (and so to have always 3 connections open instead of 12), and I was wondering about the best solution to achieve it. Any tips?

This is not a good idea. In short, a connection pool should be sized to contain enough connections to serve incoming concurrent requests without having them to wait for a connection to become available. With 4 applications, you'll very likely need more than 3 connections. There is something wrong (or not clear) with your logic.

My tip would thus be to allocate enough resources and to size your thread pools and connection pools appropriately to serve the concurrent requests each application is supposed to handle, and this without exhausting the pool.

See also

Pascal Thivent
Every application has its own db consumption rate. For example, let's say that app1, app2, app3 and app4 demand 1 min connections in the pool (total connections open: 4). Suddenly app4 increases the work on db, so I'd like to increase the active min conenctions on the db from 4 to 6 (2 more for app4). Because Hibernate is instantiated 4 times and the configuration is the same for every application, I can't tune app4 to a specific setting: if I increase the min connection to 3 then I will have 12 active connections on the db instead of 6.
Marco
The solution could be to export the Hibernate implementation on a separate instance and then access the DAOs through remoting or something like that. I was just wondering which could be the best solution.
Marco
@Marco I don't understand why you can't configure each application separately. Just override the settings per application, for example by providing an `hibernate.properties` file on the classpath. There is no technical reason that prevent you from doing this. So you are either not saying everything or you're creating a constraint that doesn't exist. Personally, I prefer running Hibernate in the same VM and I would stick to the current configuration but provide application specific settings.
Pascal Thivent
You're right, I was stuck in my conviction to make it independent and I was thinking so hard about that, that I couldn't see the obvious solution. Thanks!
Marco