2011-10-19 51 views
1

我有一个基本的方法,我正在写,以便不重复相同的hibernate会话/事务逻辑一遍又一遍。这很简单,但有一个具体问题,我不确定可以用这种方法解决。休眠模式的事务重用

想象一下,您有一个用户实体和一个权限实体。如果请求保存用户及其匹配的权限,那么我认为在单个事务中执行这两个操作是有意义的,因为只能保存其中一个实体可能会被视为数据损坏。例如,未能保存用户的权限将保证先前插入的用户数据回滚。

我做了下面的方法来允许通用的hibernate操作,如果有必要的话,它可以与当前事务一起工作,尽管我现在认为在它当前的形式下它不会工作,因为调用session.beginTransaction();可能会返回一个新的事务,即使之前没有被提交(是这种情况?)。假设我改变了它,以便它返回当前会话和事务,如果它指定了当前事务会有更多的操作,你认为它会起作用吗?做这样的事情会是明智的吗?还是你会推荐改变方法?由于

protected <T> void baseOperation(Class<T> entityClass, List<T> instances, BaseHibernateDAO.Operations operation, boolean isLastOperation) throws Exception 
{ 
    Session session = null; 
    Transaction transaction = null; 
    boolean caughtException = false; 

    //get session from factory 
    session = HibernateSessionFactory.getSession(); 

    try 
    { 
     //get current transaction 
     transaction = session.beginTransaction(); 

     for (Object instance : instances) //perform operation on all instances 
     { 
      log.debug(String.format("Will perform %s operation on %s instance.", operation.name(), entityClass.getName())); 

      switch (operation) //perform requested operation 
      { 
       case SAVE: 
        session.save(instance); 
        break; 
       case UPDATE: 
        session.update(instance); 
        break; 
       case SAVEORUPDATE: 
        session.saveOrUpdate(instance); 
        break; 
       case DELETE: 
        session.saveOrUpdate(instance); 
        break; 
      } 

      log.debug(String.format("%s operation on %s instance was succesful.", operation.name(), entityClass.getName())); 
     } 

     session.flush(); //synchronize 

     if (isLastOperation) //if this is the last operation of the transaction 
     { 
      transaction.commit(); 
      log.debug("Transaction commited succesfully."); 
     } 
    } 
    catch (Exception e) //error occurred 
    { 
     caughtException = true; 

     //roll-back if transaction exists 
     if (transaction != null) 
     { 
      transaction.rollback(); 
     } 

     //log and re-throw 
     log.error("An error occurred during transaction operation.", e); 
     throw e; 
    } 
    finally //cleanup tasks 
    { 
     if (isLastOperation || caughtException) //close session if there are no more pending operations or if an error occurred 
     { 
      HibernateSessionFactory.closeSession(); 
     } 
    } 
} 

回答

7

“宜先”将停止试图重写这已经被写入,调试代码,通过泥浆,调试更拖累,并部署了数千次。也就是说,您遇到的问题和考虑事项之前已经遇到和克服,并且解决方案已经得到验证。此外,经过广泛使用和改进,与使用自定义解决方案相比,它们需要的功能更少。检出Spring's Hibernate support,特别是"Implementing DAOs based on plain Hibernate 3 API"和“Declarative transaction demarcation”。为进一步阅读,整个chapter on transaction management

我有一个sample project on github,你可以看到一个非常简单的例子,使用Spring来管理Web应用程序(使用Spring MVC)上下文中的Hibernate会话和事务。

更新:对于那些谁一起去以后,所以他们没有通过的意见挖:

有使用Spring的事务处理三个基本方面:声明defining which methods are transactional with XML,声明annotating methods as @Transactional,或者编程using TransactionTemplate

+1

好点。另外不要忘记EJB3和Seam 3的其他基于标准的选项(基于JSR-299)。 –

+0

这并不是说我很乐意尝试重写它,我只是不知道它存在。问题是我目前没有时间去学习另一个框架。我看着你的例子,它看起来很简单,但我知道,一旦我尝试将它应用于更复杂的对象,情况就不会如此。我还没有找到一个在单个事务中运行两个操作的例子,在春天或其他时候(我发现的最多的是三个点......暗示会有更多的代码)。如果我不能看到它我无法理解它,因此我宁愿自己做,即使它没有被尝试和真实 – JayPea

+0

但无论如何,感谢指引我朝那个方向努力,当我得到时间时我会尝试学习Spring,但现在在开发时间学习这样的东西太冒险:) – JayPea