春季休眠事务 - 变化不反映? [英] spring-hibernate transaction- changes not reflecting?

查看:104
本文介绍了春季休眠事务 - 变化不反映?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring和Hibernate。我有一个用 @Transactional 注释的方法。此方法有两个数据库调用。一次调用将更新表中的数据,另一次调用将根据第一次调用的更新数据从同一表中检索一些数据。
问题是第一次调用的数据库更改没有立即反映出来。在用 @Transactional 注释的方法流出后,更改会被反映出来。我仍然尝试调用 session.flush()但没用。请推荐我。

I am using Spring and Hibernate. I have a method annotated with @Transactional. This method has two database calls. One call would update the data in the table and the other call would retrieve some data from the same table based on the first call's updated data. The problem is first call's database change is not reflected immediately. Change is reflected after the flow comes out of the method which is annotated with @Transactional. I still tried calling session.flush() but no use. Please suggest me.

@Transactional
public void method1(){
dao.updateM1();
dao.getData();
}


推荐答案

?例如:您为 updateM1 getData 方法声明了什么传播级别?

Could you elaborate on your scenario? For example: what propagation level did you declare for updateM1 and getData methods?

如果您没有声明, t描述 updateM1 getData 方法的Transactional,问题是两个方法都在同一个事务中。因此,直到事务提交之前,Hibernate才会更新数据。要解决这个问题,您只需描述 updateM1 的交易如下:

In case, you didn't describe Transactional for updateM1, and getData methods, the problem is both methods are in one same transaction. As a result, Hibernate won't update the data until the transaction is committed. To solve that problem, you just describe the transaction for updateM1 as below:

@Transactional (propagation = Propagation.REQUIRE_NEW)
public E updateM1() {}

每次调用 updateM1 时都会创建事务。 updateM1完成后,新的事务将被提交,所有更改将保留在数据库中。

With that, a new transaction will be created every time updateM1 is called. When the updateM1 is finished, that new transaction is committed and all changes will be persisted in the database.

关于session.flush不工作,以下是一个明确的答案:< hibernate =https://stackoverflow.com/questions/3274958/question-about-hibernate-session-flush>有关Hibernate session.flush()的问题

About session.flush not working, here is one clear answer: Question about Hibernate session.flush()

这篇关于春季休眠事务 - 变化不反映?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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