组合休眠@Transactional &ehcache @Cacheable 总是创建数据库事务 [英] Combination hibernate @Transactional & ehcache @Cacheable always creates DB transaction

查看:29
本文介绍了组合休眠@Transactional &ehcache @Cacheable 总是创建数据库事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对我们的应用程序进行负载测试时,我注意到如果您使用 @Transactional 和 @Cacheable 注释,休眠总是会创建一个数据库事务.有没有简单的方法来防止这种情况?在 spring 中解决这个问题的一种更优雅的方法是使用以下类/接口

While doing loadtesting on our application I noticed that if you use @Transactional and @Cacheable annotions that hibernate always creates a database transaction. Is there an easy way to prevent this? A more eleberate way to solve this within spring is to have following class/interfaces

  • 服务层接口
  • 可缓存的带注释的类,它只是一个代理/转发到
  • 事务注解实现类

发生了什么

呼叫 1:

  1. 交易已创建
  2. 类方法被调用
  3. 结果缓存&返回

呼叫 2:

  1. 交易已创建
  2. 返回缓存结果

首选结果应该是:

呼叫 1:

  1. 交易已创建
  2. 类方法被调用
  3. 结果缓存&返回

呼叫 2:

  1. 返回缓存结果

推荐答案

您需要更改 @Transactional@Cacheable 方面的相对顺序.

You need to change relative order of @Transactional and @Cacheable aspects.

可以使用order属性进行配置.参见 8.2.4.7 建议订购 表示订单值的含义.

It can be configured using order attribute of <tx:annotation-driven> and <cache:annotation-driven>. See 8.2.4.7 Advice ordering for the meaning of order values.

这篇关于组合休眠@Transactional &amp;ehcache @Cacheable 总是创建数据库事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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