package netstore.service;
import netstore.framework.exceptions.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import org.apache.commons.logging.*;
import netstore.businessobjects.*;
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static Configuration configuration;
private static SessionFactory sessionFactory;
// Create the initial SessionFactory from the default configuration files
static {
try {
configuration = new Configuration();
configuration.addClass(Category.class)
.addClass(Customer.class)
.addClass(Item.class)
.addClass(Order.class);
sessionFactory = configuration.buildSessionFactory();
// We could also let Hibernate bind it to JNDI:
// configuration.configure().buildSessionFactory()
} catch (Throwable ex) {
// We have to catch Throwable, otherwise we will miss
// NoClassDefFoundError and other subclasses of Error
log.error(ex.getMessage());
throw new ExceptionInInitializerError(ex);
}
}
/**
* Returns the SessionFactory used for this static class.
*
* @return SessionFactory
*/
public static SessionFactory getSessionFactory() {
/* Instead of a static variable, use JNDI:
SessionFactory sessions = null;
try {
Context ctx = new InitialContext();
String jndiName = "java:hibernate/HibernateFactory";
sessions = (SessionFactory)ctx.lookup(jndiName);
} catch (NamingException ex) {
throw new InfrastructureException(ex);
}
return sessions;
*/
return sessionFactory;
}
/**
* Returns the original Hibernate configuration.
*
* @return Configuration
*/
public static Configuration getConfiguration() {
return configuration;
}
/**
* Rebuild the SessionFactory with the static Configuration.
*
*/
public static void rebuildSessionFactory()
throws DatastoreException {
synchronized(sessionFactory) {
try {
sessionFactory = getConfiguration().buildSessionFactory();
} catch (Exception ex) {
log.error(ex.getMessage());
throw DatastoreException.datastoreError(ex);
}
}
}
/**
* Rebuild the SessionFactory with the given Hibernate Configuration.
*
* @param cfg
*/
public static void rebuildSessionFactory(Configuration cfg)
throws DatastoreException {
synchronized(sessionFactory) {
try {
sessionFactory = cfg.buildSessionFactory();
configuration = cfg;
} catch (Exception ex) {
log.error(ex.getMessage());
throw DatastoreException.datastoreError(ex);
}
}
}
public static Session getSession()throws DatastoreException{
try {
return sessionFactory.openSession();
}catch(Exception ex){
log.error(ex.getMessage());
throw DatastoreException.datastoreError(ex);
}
}
public static void close(){
try {
sessionFactory.close();
}catch(Exception ex){
log.error(ex.getMessage());
}
}
public static void closeSession(Session session)throws DatastoreException{
try {
session.close();
}catch(Exception ex){
log.error(ex.getMessage());
throw DatastoreException.datastoreError(ex);
}
}
public static void rollbackTransaction(Transaction transaction)throws DatastoreException{
try {
if(transaction!=null)
transaction.rollback();
}catch(Exception ex){
log.error(ex.getMessage());
throw DatastoreException.datastoreError(ex);
}
}
}