Beginner |
|
Joined: Tue Sep 02, 2003 12:15 pm Posts: 33
|
This is basically how I do it. I use approximately 5 databases to work with our data. I can't guarantee this is exactly how it should be done, but it seems to work for me.
private static final int NUM_SESSIONS = 5;
private static final SessionFactory[] sessionFactory = new SessionFactory[NUM_SESSIONS];
private static final TabLogger log = new TabLogger("net.sf.hibernate.cfg.Environment");
private static ThreadLocal session = new ThreadLocal();
public static TransactionAwareSession currentSession()
{
if (log.isDebugEnabled())
{
TransactionAwareSession s = (TransactionAwareSession) session.get();
if (s != null)
{
log.debug("Getting session " + s.getSession().hashCode());
}
}
return (TransactionAwareSession) session.get();
}
public static boolean loadSession(final int id) throws HibernateException
{
if (NUM_SESSIONS > id)
{
if (null == sessionFactory[id])
{
configureDatasource(id);
}
TransactionAwareSession s = (TransactionAwareSession) session.get();
// Open a new Session, if this Thread has none yet
if (s == null)
{
s = new TransactionAwareSession(sessionFactory[id].openSession());
log.debug("Beginning transaction ");
s.beginTransaction();
session.set(s);
}
}
else
{
return false;
}
return true;
}
private static void configureDatasource(int ii) throws HibernateException
{
StringBuffer fileName = new StringBuffer(pathToFiles);
switch (ii)
{
case Services.IFX_DATASOURCE:
fileName.append("informixdb.hibernate.xml");
log.debug("Loading Informix Property File " + fileName.toString());
sessionFactory[ii] = new Configuration().
configure(fileName.toString()).
buildSessionFactory();
break;
case Services.IFX_REPORT_DATASOURCE:
fileName.append("report.informixdb.hibernate.xml");
log.debug("Loading Informix Report Property File " + fileName.toString());
sessionFactory[ii] = new Configuration().
configure(fileName.toString()).
buildSessionFactory();
break;
case Services.FACTOR_ONLINE_DATASOURCE:
fileName.append("factoronline.hibernate.xml");
log.debug("Loading Factor Property File " + fileName.toString());
sessionFactory[ii] = new Configuration().
configure(fileName.toString()).
buildSessionFactory();
break;
case Services.CHECK_IMAGING_DATASOURCE:
fileName.append("checkimaging.hibernate.xml");
log.debug("Loading Check Imaging Property File " + fileName.toString());
sessionFactory[ii] = new Configuration().
configure(fileName.toString()).
buildSessionFactory();
break;
case Services.FACTORSOFT_DATASOURCE:
fileName.append("factorsoft.hibernate.xml");
log.debug("Loading Factor Property File " + fileName.toString());
sessionFactory[ii] = new Configuration().
configure(fileName.toString()).
buildSessionFactory();
break;
default :
throw new UncheckedMessageException("Invalid datasource configuration requested.");
}
}
// -------------------------- INNER CLASSES --------------------------
public static final class Services
{
// ------------------------------ FIELDS ------------------------------
public static final int IFX_DATASOURCE = 0;
public static final int FACTOR_ONLINE_DATASOURCE = 1;
public static final int CHECK_IMAGING_DATASOURCE = 2;
public static final int IFX_REPORT_DATASOURCE = 3;
public static final int FACTORSOFT_DATASOURCE = 4;
}
|
|