This is the piece of code that we are using for accessing Hibernate: Please have a look at it and let me know if they way we are obtaining and killing the session is right:
package com.ge.erc.uww.actuarial.data.hibernate;
import com.ge.erc.uww.actuarial.exception.ActuarialException;
import com.ge.erc.uww.actuarial.util.ActuarialConstants;
import com.ge.erc.uww.actuarial.util.DBConnection;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import org.apache.log4j.Logger;
import java.sql.SQLException;
/**
*
* Hibernate Implementation class
*
*/
public class HibernateSession {
private static String olaSchema = ActuarialConstants.OLA_SCHEMA_OWNER;
private static String gusSchema = ActuarialConstants.GUS_SCHEMA_OWNER;
private static String uwwSchema = ActuarialConstants.UWW_SCHEMA_OWNER;
private static SessionFactory sessionFactory;
public static final ThreadLocal OLA_SESSION = new ThreadLocal();
public static final ThreadLocal UWW_SESSION = new ThreadLocal();
public static final ThreadLocal GUS_SESSION = new ThreadLocal();
private static Logger logger =
Logger.getLogger (HibernateSession.class.getName ());
/**
* get current OLA session
*/
public static Session currentOLASession ()
throws ActuarialException {
return currentSession (olaSchema);
}
/**
* Get current UWW session
*/
public static Session currentUWWSession ()
throws ActuarialException {
return currentSession (uwwSchema);
}
/**
* Fetches the current session for the specified schema. Used internally by
* other wrapper methods
* Initializes the session-factory and sessions if not initialized
* @param schemaName
* @return session
* @exception ActuarialException
* <b>Preconditions:</b><br>
* <ol>
* <li>schemaName should not be empty</li>
* </ol>
*
*/
private static Session currentSession (String schemaName)
throws ActuarialException {
Session session = null;
try {
logger.debug ("currentSession>");
session = getSession (schemaName);
logger.debug ("session : " + session);
if (
(session == null) ||
((session != null) && !session.isConnected ())) {
// Don't get from JNDI, use a static SessionFactory
if (sessionFactory == null) {
// Use default hibernate.cfg.xml
sessionFactory =
new Configuration().configure ().buildSessionFactory ();
logger.debug ("sessionFactory was null");
}
logger.debug ("sessionfactory:" + sessionFactory);
if (olaSchema.equalsIgnoreCase (schemaName)) {
session =
sessionFactory.openSession (
DBConnection.getOLAConnection ());
logger.debug ("Opened session with OLAConnection");
} else {
session =
sessionFactory.openSession (
DBConnection.getConnection ());
logger.debug ("Opened session with UWW Connection");
}
logger.debug ("session opened:" + session);
setSession (session, schemaName);
logger.debug ("session set to factory");
}
} catch (HibernateException he) {
logger.error ("HibernateException:" + he.getMessage (), he);
throw new ActuarialException("error.hibernate.currentSession", he);
}
logger.debug ("currentSession<");
return session;
}
/**
* This method closes all the sessions.
* @exception ActuarialException
*/
public static void closeSession ()
throws ActuarialException {
logger.debug ("closeSession>");
try {
closeOLASession ();
closeUWWSession ();
} catch (HibernateException he) {
logger.error ("HibernateException:" + he.getMessage (), he);
throw new ActuarialException("error.hibernate.closingSession", he);
} catch (SQLException se) {
logger.error ("SQLException:" + se.getMessage (), se);
throw new ActuarialException("error.hibernate.closingSession", se);
}
logger.debug ("closeSession<");
}
/**
* This method closes the UWW session.
* @exception ActuarialException
* @exception SQLException
*/
public static void closeUWWSession ()
throws HibernateException, SQLException {
logger.debug ("closeUWWSession>");
Session session = ( Session ) UWW_SESSION.get ();
logger.debug ("s:" + session);
if ((session != null) && (session.connection () != null)) {
logger.debug (
"s.connection().isClosed():" +
session.connection ().isClosed ());
}
if (
(session != null) &&
(session.connection () != null) &&
!(session.connection ().isClosed ())) {
session.connection ().close ();
logger.debug ("connection closed");
}
UWW_SESSION.set (null);
if (session != null) {
session.close ();
}
logger.debug ("closeUWWSession<");
}
/**
* This method closes the OLA session.
* @exception ActuarialException
* @exception SQLException
*/
public static void closeOLASession ()
throws HibernateException, SQLException {
logger.debug ("closeOLASession>");
Session session = ( Session ) OLA_SESSION.get ();
logger.debug ("session:" + session);
if ((session != null) && (session.connection () != null)) {
logger.debug (
"session.connection().isClosed():" +
session.connection ().isClosed ());
}
if (
(session != null) &&
(session.connection () != null) &&
!(session.connection ().isClosed ())) {
session.connection ().close ();
logger.debug ("connection closed");
}
OLA_SESSION.set (null);
if (session != null) {
session.close ();
}
logger.debug ("closeOLASession<");
}
/**
* This method fetches the session for the respective schema name.
* @param schemaName
* @return Session
* <b>Preconditions:</b><br>
* <ol>
* <li>schemaName should not be empty/null</li>
* </ol>
*
*/
private static Session getSession (String schemaName) {
logger.debug ("getSession>");
Session session = null;
if (olaSchema.equalsIgnoreCase (schemaName)) {
session = ( Session ) OLA_SESSION.get ();
} else if (gusSchema.equalsIgnoreCase (schemaName)) {
session = ( Session ) GUS_SESSION.get ();
} else {
session = ( Session ) UWW_SESSION.get ();
}
logger.debug ("getSession<");
return session;
}
/**
* This method is used to set the session for the respective schema name.
* @param schemaName
* @param session
* @return Session
* <b>Preconditions:</b><br>
* <ol>
* <li>session should not be null</li>
* <li>schemaName should not be empty/null</li>
* </ol>
*
*/
private static void setSession (Session session, String schemaName) {
logger.debug ("setSession>");
if (olaSchema.equalsIgnoreCase (schemaName)) {
OLA_SESSION.set (session);
} else if (gusSchema.equalsIgnoreCase (schemaName)) {
GUS_SESSION.set (session);
} else {
UWW_SESSION.set (session);
}
logger.debug ("setSession<");
}
}
|