Quartz定时器中打开与关闭hibernateSession,这样对hibernate2级缓存进行了优化,在打开的session中可以维护lazy加载,这样提升了hibernate在Quartz中的使用效率。废话不多说了,上代码:

  1. package com.net263.boss.charge.action;  
  2.  
  3. import org.apache.log4j.Logger;  
  4. import org.hibernate.HibernateException;  
  5. import org.hibernate.Session;  
  6. import org.hibernate.SessionFactory;  
  7. import org.quartz.JobExecutionContext;  
  8. import org.quartz.JobExecutionException;  
  9. import org.springframework.orm.hibernate3.SessionFactoryUtils;  
  10. import org.springframework.orm.hibernate3.SessionHolder;  
  11. import org.springframework.scheduling.quartz.QuartzJobBean;  
  12. import org.springframework.transaction.support.TransactionSynchronizationManager;  
  13.  
  14. public abstract class TransactionalQuartzTask extends QuartzJobBean {  
  15.  
  16.     private static Logger logger = Logger.getLogger(TransactionalQuartzTask.class);  
  17.     private SessionFactory sessionFactory;    
  18.       
  19.     public SessionFactory getSessionFactory() {  
  20.         return sessionFactory;  
  21.     }  
  22.  
  23.     public void setSessionFactory(SessionFactory sessionFactory) {  
  24.         this.sessionFactory = sessionFactory;  
  25.     }  
  26.  
  27.     /**   
  28.      * Most of this method is copied from the HibernateInterceptor.   
  29.      */    
  30.     protected final void executeInternal(JobExecutionContext ctx)    
  31.             throws JobExecutionException {    
  32.         Session session = SessionFactoryUtils.getSession(sessionFactory, true);    
  33.         boolean existingTransaction = SessionFactoryUtils    
  34.                 .isSessionTransactional(session, getSessionFactory());    
  35.         if (existingTransaction) {    
  36.             logger.info("Found thread-bound Session for TransactionalQuartzTask");    
  37.         } else {    
  38.             TransactionSynchronizationManager.bindResource(getSessionFactory(),    
  39.                     new SessionHolder(session));    
  40.         }    
  41.     
  42.         try {    
  43.             executeTransactional(ctx);    
  44.         } catch (HibernateException ex) {    
  45.             ex.printStackTrace();    
  46.             throw ex;    
  47.         } finally {    
  48.             if (existingTransaction) {    
  49.                 logger.debug("Not closing pre-bound Hibernate Session after TransactionalQuartzTask");    
  50.             } else {    
  51.                 TransactionSynchronizationManager    
  52.                         .unbindResource(getSessionFactory());    
  53.                 SessionFactoryUtils    
  54.                         .releaseSession(session, getSessionFactory());    
  55.             }    
  56.         }    
  57.     }    
  58.     
  59.     /**   
  60.      * Implementing classes, implement this method.   
  61.      */    
  62.     protected abstract void executeTransactional(JobExecutionContext ctx)    
  63.             throws JobExecutionException;  

所有Quartz类都继承TransactionalQuartzTask类实现executeTransactional虚方法。