Hibernate version: 3.0.3
Name and version of the database you are using: Oracle 9i
Alrighty, to start off, I have read the FAQ, I have read the documentation, and I have searched the forums, and while the information I have found has been helpful, none of which I have found is addressing my problem. This being said, here's the skinny:
I have hibernate connections configured using hibernate.cfg.xml using a "connection.datastore" from a context.xml that defines the attributes needed when making a connection to the database (url, username, password, etc). I use a HibernateUtil class to instantiate the static SessionFactory as such:
Code:
public class HibernateUtil {
private static final Logger LOGGER = Logger.getLogger(HibernateUtil.class);
private static final LogHelper LOG_HELPER = LogHelper.getInstance();
private static Configuration configuration;
private static SessionFactory sessionFactory;
private static final ThreadLocal threadSession = new ThreadLocal();
private static final ThreadLocal threadTransaction = new ThreadLocal();
private static final ThreadLocal threadInterceptor = new ThreadLocal();
// Create the initial SessionFactory from the default configuration files
static {
try {
configuration = new Configuration();
sessionFactory = configuration.configure().buildSessionFactory();
} catch (Throwable ex) {
LOGGER.error("Building SessionFactory failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Configuration getConfiguration() {
return configuration;
}
public static Session getSession()
throws InfrastructureException {
Session s = (Session) threadSession.get();
try {
if (s == null) {
LOGGER.debug("Opening new Session for this thread.");
if (getInterceptor() != null) {
LOGGER.debug("Using interceptor: " + getInterceptor().getClass());
s = getSessionFactory().openSession(getInterceptor());
} else {
s = getSessionFactory().openSession();
}
threadSession.set(s);
}
} catch (HibernateException ex) {
LOG_HELPER.errorStackTrace(ex, LOGGER) ;
throw new InfrastructureException(ex);
}
return s;
}
...
}
What I'm looking to do here, is based on a specific "first action" a user makes, I need to switch how I connect to the database. I'm using the same database, but I need to connect as a different user, and thus access different DB schema within Oracle.
Thus far, the only way I have seen to do this using my current configuration would be to create a separate xxx.cfg.xml and separate HibernateUtilxxx class that uses hard coded names of the individual cfg.xml files in the
Code:
sessionFactory = configuration.configure("xxx.cfg.xml").buildSessionFactory();
statement for each and every "first action" so that the user would be in the proper context to access the proper schema based data. While this would seemingly work, this seems a bit overkill and bulky, especially if I have 10 different actions that required 10 different schema, not to mention quite tricky in determining what type of HibernateUtilityxxx class to use for the connection. (I thought about trying to parameterize the cfg.xml part of the statement, but how would I get a parameter passed to a static block?)
So, what I'm looking for is either confirmation that my assessment is correct, or that there is a simpler way of accomplishing what I'm looking for.
Thanks in advance for your help and patience with a first-timer.