-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Choose the configuration file to use
PostPosted: Tue Apr 06, 2004 9:48 am 
Regular
Regular

Joined: Thu Feb 05, 2004 6:51 am
Posts: 50
Hi all,

I have serching in this forum a way to use the hibernate.cfg.xml file I want but nothing works (this ismy fault I'm sure).

The application will be packaged in a jar a the hibernate.cfg.xml file will be in the WEB-INF/classes folder. The class calling the configure mathod is located down in the hierarchy.

Please could someone indicate my what is the right code to use.

Thanks in advance.

Vlad


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 06, 2004 10:09 am 
Regular
Regular

Joined: Thu Feb 05, 2004 6:51 am
Posts: 50
I want to use it in this code context :

public class ThreadLocalSession
{
private static Logger LOG = Logger.getLogger(ThreadLocalSession.class);

private static final SessionFactory sessionFactory;
private Session session;

private static final ThreadLocal sessionContext = new ThreadLocal();

static {
try {
Configuration conf = new Configuration();
sessionFactory = conf.configure(Thread.currentThread().getClass().getResourceAsStream("hibernate.cfg.db1.xml")).buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex);
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 06, 2004 10:25 am 
Regular
Regular

Joined: Thu Feb 05, 2004 6:51 am
Posts: 50
Could the people having already worked using the possibily to choose a specific hibernate conf file with multiple database as instance post the code they use.

This is bloody important issue for me and I can't figure out this.

Vlad


Top
 Profile  
 
 Post subject: Here's how I do it.
PostPosted: Tue Apr 06, 2004 12:05 pm 
Beginner
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;
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 06, 2004 3:24 pm 
Regular
Regular

Joined: Thu Feb 05, 2004 6:51 am
Posts: 50
Ok, thanks it looks fine.

But it is working to get a configuration file located in the same jar that the web app ?

Vlad


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.