在View和@Transactional中打开会话 [英] Open Session in View vs @Transactional

查看:57
本文介绍了在View和@Transactional中打开会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在服务层中使用 @Transactional 批注.但是要解决由于视图中的延迟加载而导致的错误,我不得不在视图过滤器中使用 Open Session .此后,无需使用@Transaction本身即可打开Session并开始事务.那么这是否意味着不需要@Transactions?然后如何在服务层中处理事务和回滚?

I had been using @Transactional annotations in my Service Layer. But to resolve an error due to Lazy Loading in View I had to use Open Session in View Filter. After this, without use of @Transaction itself a Session gets opened and transaction starts. So does that mean @Transactions are not required? How will transactions and roll-backs be handled, then in Service Layers?

推荐答案

此过滤器使Hibernate Sessions可通过当前线程使用,它将由事务管理器自动检测到.它适用于通过HibernateTransactionManager或JtaTransactionManager进行的服务层事务以及非事务执行(如果配置正确).

This filter makes Hibernate Sessions available via the current thread, which will be autodetected by transaction managers. It is suitable for service layer transactions via HibernateTransactionManager or JtaTransactionManager as well as for non-transactional execution (if configured appropriately).

注意:默认情况下,此过滤器将不刷新休眠会话,并且刷新模式设置为FlushMode.NEVER.它假定与护理刷新的服务层事务结合使用:活动事务管理器将在读写事务期间将刷新模式临时更改为FlushMode.AUTO,最后将刷新模式重置为FlushMode.NEVER.每笔交易.如果您打算在没有事务的情况下使用此过滤器,请考虑更改默认的刷新模式(通过"flushMode"属性).

NOTE: This filter will by default not flush the Hibernate Session, with the flush mode set to FlushMode.NEVER. It assumes to be used in combination with service layer transactions that care for the flushing: The active transaction manager will temporarily change the flush mode to FlushMode.AUTO during a read-write transaction, with the flush mode reset to FlushMode.NEVER at the end of each transaction. If you intend to use this filter without transactions, consider changing the default flush mode (through the "flushMode" property).

这篇关于在View和@Transactional中打开会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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