Hibernate模式用于事务重用 [英] Hibernate pattern for transaction reuse

查看:93
本文介绍了Hibernate模式用于事务重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基础方法,我正在编写这个方法,以避免一遍又一遍地重复同一个hibernate会话/事务逻辑。这很简单,但有一个具体问题,我不确定可以用这种方法解决。

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



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

 受保护的< T> void baseOperation(Class< T> entityClass,List< T>实例,BaseHibernateDAO.Operations操作,boolean isLastOperation)throws Exception 
{
Session session = null;
交易交易=空;
boolean caughtException = false;

//从工厂获取会话
session = HibernateSessionFactory.getSession();

尝试
{
//获取当前事务
transaction = session.beginTransaction();

(对象实例:实例)//对所有实例执行操作
{
log.debug(String.format(将在%s实例上执行%s操作。 ,operation.name(),entityClass.getName()));

switch(operation)//执行请求的操作
{
case SAVE:
session.save(instance);
休息;
case更新:
session.update(instance);
休息;
case SAVEORUPDATE:
session.saveOrUpdate(instance);
休息;
案例DELETE:
session.saveOrUpdate(instance);
休息;

$ b $ log.debug(String.format(%s实例上的%s操作成功。,operation.name(),entityClass.getName()));
}

session.flush(); //同步

if(isLastOperation)//如果这是交易的最后一个操作
{
transaction.commit();
log.debug(Transaction commitedfully succesfully。);

$ b $ catch(Exception e)//发生错误
{
caughtException = true;

//如果交易存在,则回滚
if(transaction!= null)
{
transaction.rollback();
}

//记录并重新抛出
log.error(在事务操作期间发生错误。,e);
throw e;
}
finally //清理任务
{
if(isLastOperation || caughtException)//如果没有更多待处理操作或发生错误,则关闭会话
{
HibernateSessionFactory.closeSession();




解决方案

建议是停止尝试重写已经写入,调试过的代码,拖拽泥泞,调试更多,并部署数千次。也就是说,您遇到的问题和考虑事项之前已经遇到并被克服,并且解决方案已经得到验证。此外,经过广泛使用和改进,与使用自定义解决方案相比,它们需要的功能更少。查看 Spring的Hibernate支持,特别是实现基于纯Hibernate 3 API的DAO声明式事务划分。如需进一步阅读,请参阅完整的交易管理

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



更新:对于那些稍后再来的人来说,他们不必深入挖掘评论:

使用Spring的事务处理有三种常规方法:声明式定义哪些方法与XML交易,声明式注解方法为@Transactional ,或者通过编程方式使用TransactionTemplate

I have a base method that I'm writing in order to not repeat the same hibernate session/transaction logic over and over. It's fairly simple, but there's a specific issue that I'm not sure can be solved with this approach.

Imagine that you have a User entity and a Permission entity. If a request is made to save a user along with its matching permissions, then I think that it would make sense to perform both operations in a single transaction, since being able to save only one of those entities could be considered data corruption. For example, failing to save the user's permissions would warrant a rollback on previously inserted user data.

I made the following method to allow generic hibernate operations that could work with the current transaction if it were necessary, although I now think that in its current form it won't work since calling session.beginTransaction(); will probably return a new transaction even if the previous hasn't been commited (is this the case?). Suppose that I changed it in order to have it return the current session and transaction if it was specified that there would be more operations for the current transaction, do you think it would work? Would it be advisable to do something like this, or would you recommend a change of approach? Thanks

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

解决方案

"Advisable" would be to stop trying to rewrite code that's already been written, debugged, dragged through the mud, debugged more, and deployed thousands of times. I.e, the issues and considerations you're encountering have been encountered and overcome before, and the solutions are proven. Further, having been extensively used and improved, they require much less effort to use than what you're putting into your custom solution. Check out Spring's Hibernate support, especially "Implementing DAOs based on plain Hibernate 3 API" and "Declarative transaction demarcation". For further reading, there's a whole chapter on transaction management.

I have a sample project on github where you can see a very simple example of using Spring to manage Hibernate Sessions and transactions in the context of a webapp (using Spring MVC).

Update: For those who come along later, so they don't have to dig through the comments:

There are three general ways to use Spring's transaction handling: declaratively defining which methods are transactional with XML, declaratively annotating methods as @Transactional, or programmatically using TransactionTemplate.

这篇关于Hibernate模式用于事务重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆