济南网页设计|自助建站济南网站建设|聊城网站建设济南网站制作|济南网站优化 http://www.mzwkj.com www.tuanqv.com

孙未勤的HibernateUtil

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);
           }
         }


 

posted on 2007-08-19 12:56  路大侠  阅读(140)  评论(0)    收藏  举报

导航

济南户外拓展|企业户外拓展 http://www.tuanqv.com