Well, this is the right time to make things properly.
There is no way to add a second connection AND keep the pattern you've described.
Instead you may try to get things right.
You have two options.
First option. Keep the mess and make it a little worst ( without telling your clients )
- Create an utility class that handle the connections for you. This time don't limit to only two. Use a map instead and don't make it static.
- Change your static class use that new utility object.
Create your new connection utility ( HibernateUtility2) and use that new utility object too.
// Create a new utility class using a map instead.
class HibernateUtility {
private Map<String, SessionFactory> factories;
private HibernateUtility instance;
public static HibernateUtility getInstance() {
if( instance == null ) {
instance = new HibernateUtility();
}
return instance;
}
private HibernateUtility() {}
private void check( String whichConnection ) {
if( !factories.containsKey( whichConnection ) ) {
// start the connection
factories.put( whichConnection, new Configu..
//... etc etc ().configure().buildSessionFactory() );
}
}
void openSession( String whichConnection ) {
check( whichConnection );
//open session with sessionFactory
factories.get( whichConnection ).open(); //etcetc
}
Session currentSession( whichConnection ) {
check( whichConnection );
//return the currently open session
factories.get( whichConnection ) .....
}
void closeSession( String whichConnection ) {
check( whichConnection );
//close the currently open session
factories.get( whichConnection );
}
}
//------------------------------------------
// Make your existing class call that class
public class HibernateUtil {
static void openSession() {
//open session with sessionFactory
HibernateUtility.getInstance( "one" ).openSession();
}
public static Session currentSession() {
//return the currently open session
HibernateUtility.getInstance( "one" ).currentSession();
}
static void closeSession() {
//close the currently open session
HibernateUtility.getInstance( "one" ).closeSession();
}
}
//------------------------------------------
// Make the new connection use that class too.
public class HibernateUtil {
static void openSession() {
//open session with sessionFactory
HibernateUtility.getInstance( "two" ).openSession();
}
public static Session currentSession() {
//return the currently open session
HibernateUtility.getInstance( "two" ).currentSession();
}
static void closeSession() {
//close the currently open session
HibernateUtility.getInstance( "two" ).closeSession();
}
}
This way the existing client code won't break.
Second option. Clean up the mess and modify the code accordaingly.
In this second option you create an object very similar to the HibernateUtility described before, but this time you don't add the methods openSession nor closeSession, which don't belong to that interface.
Using this non static method let's you create different instances that could connect to different configurations.
You just just use the currentSession that if the session doesn't exists creates a new one, and receive as parameter an identifier ( much like "one" and "two" in the previous sample )
In this second option you have to change your client code to use the rigth implementation.
Note: Somethings wrong with the code formatting in SO, it took me some time to format it the way it is, and it doesn't look any good :(