I'm using Hibernate 3.0 with MySql 4.1.
I have created one session-factory in the hibernate.cfg.xml to connect to my database and my code is working. How does my code need to change if I add another session-factory/database connection? I currently am using a class called HibernateUtil (as per the examples) to access the Session, the code is below. So, how do things work if I have several session-factories defined?
Code:
public class HibernateUtil
{
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.out.println("ERROR INITIALIZING!");
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession()
{
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
}