用于事务重用的Hibernate模式

我有一个基本方法,我写的是为了不反复重复相同的hibernate会话/事务逻辑。 这很简单,但有一个特定的问题,我不确定这种方法可以解决。

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

我提出了以下方法来允许通用的hibernate操作,如果有必要可以使用当前事务,尽管我现在认为,在调用session.beginTransaction()时它的当前forms不会起作用。 即使前一个没有被提交,也可能会返回一个新的事务(这是这种情况吗?)。 假设我更改了它以使其返回当前会话和事务,如果指定当前事务将有更多操作,您认为它会起作用吗? 做这样的事情是否可取,或者你会建议改变方法吗? 谢谢

protected  void baseOperation(Class entityClass, List 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(); } } } 

“Advisable”将停止尝试重写已经编写,调试,拖拽,调试更多,并部署数千次的代码。 即,您遇到的问题和考虑因素之前已经遇到并克服,并且解决方案已得到证实。 此外,经过广泛使用和改进后,与使用自定义解决方案相比,它们所需的工作量要少得多。 查看Spring的Hibernate支持 ,特别是“基于普通Hibernate 3 API实现DAO”和“ 声明式事务划分 ”。 有关进一步阅读,有一整章关于交易管理 。

我在github上有一个示例项目 ,您可以在其中看到一个使用Spring在Web应用程序上下文中管理Hibernate会话和事务的简单示例(使用Spring MVC)。

更新:对于那些以后来的人,所以他们不必深入评论:

使用Spring的事务处理有三种常规方法:声明性地定义哪些方法是XML事务处理,声明性地将方法 注释为@Transactional ,或者使用TransactionTemplate以编程方式。