问题与春季交易管理? [英] issue with spring transaction management?

查看:107
本文介绍了问题与春季交易管理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring和hibernate。我正在使用spring进行事务管理。

  @Service 
@Transactional(readOnly = true)
public class Sample implements SampleInterface {

@Override
public List< Some> getData(){

//在此之后获取数据,它会更新一些内容

setStatus(someId);


$ b @Override
@Transactional
public void setStatus(Long someId){

// set status

}

}

如果我不保留@Transactional for getData()然后我得到下面的异常。

 引起:org.springframework.dao.InvalidDataAccessApiUsageException:写操作是不允许在只读模式(FlushMode.MANUAL):将你的Session变成FlushMode 

如果我保留@Transactional getData()然后它会正确保存。这里有什么问题?任何我有@Transactional为setStatus()。我还需要为getData()保留@Transactional,因为它调用一个公共方法来设置状态吗?



谢谢!


这个问题有点复杂,是由 setStatus() em> getData()。当你从外部调用 getData()时,你实际上正在调用Spring框架为你创建的Java代理。此代理应用事务行为(启动只读事务)并委托给您的实际服务类。这很好。



但是,当您调用 setStatus()时,您将绕过事务代理并调用您的直接服务。换句话说,对 setStatus()的请求不被拦截,并且 @Transactional 被忽略



有没有简单的方法来处理这个问题和 ejb 的问题具有相同的问题。



另请参阅



    $ b在调用公共方法时,您必须格外小心。 $ b
  • 8.6.1了解AOP代理 - 官方文档
  • 春季陷阱:代理 - 我的博客 $ b

    I am using spring and hibernate. i am using spring for transaction management. i have below class.

    @Service
    @Transactional(readOnly = true)
    public class Sample implements SampleInterface{
    
    @Override
    public List<Some> getData(){
    
    //gets data after that it updates something
    
    setStatus(someId);
    
    }
    
    @Override
    @Transactional
    public void setStatus(Long someId){
    
    //sets status
    
    }
    
    }
    

    If i dont keep @Transactional for getData() then i get below exception.

    Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode
    

    if i keep @Transactional for getData() then it will save properly. what is an issue here? any how i have @Transactional for setStatus(). Still do i need to keep @Transactional for getData() as it is calling a public method which will set the status?

    Thanks!

    解决方案

    The problem is a bit complex and is caused by calling setStatus() inside getData(). When you are calling getData() from outside, you are actually calling a Java proxy created for you by Spring framework. This proxy applies transaction behaviour (starts read-only transaction) and delegates to your actual service class. This works fine.

    However, when you call setStatus(), you are bypassing the transactional proxy and calling your service directly. In other words the request to setStatus() is not intercepted, and @Transactional is ignored.

    There is no easy way to deal with this problem and has the same issue. You just have to be extra careful when calling public methods inside the same class.

    See also

    这篇关于问题与春季交易管理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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