I have a small special-purpose application server that uses hibernate in a managed environment scenario. The server has has hibernate and libraries in classpath, and loads modules using separate classloaders. Each module initializes hibernate:
Code:
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
Configuration cfg = new Configuration();
cfg.setProperties(moduleProperties);
// add module specific persistent classes
sf = cfg.buildSessionFactory();
a typical
moduleProperties looks like this:
Code:
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost/modulesDatabase
hibernate.connection.username=user
hibernate.connection.password=pass
hibernate.c3p0.min_size=1
hibernate.c3p0.max_size=5
hibernate.c3p0.timeout=1800
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.show_sql=false
This way modules all use the same hibernate classes, but have their isolated view of the database and everything.
My problem is that the modules grew quite a few over time, and now I have tons of separate connections to the database server with very low concurrency. All modules connect to the same database server, but access different databases.
Is it possible to make somehow all modules request connections from the same c3p0 pool, and have them change the current database before issuing any requests? If this could be done, I would have a single pool with a few connections that would be used more efficiently.