Hibernate会话刷新行为[和Spring @Transactional] [英] Hibernate Session flush behaviour [ and Spring @Transactional ]

查看:142
本文介绍了Hibernate会话刷新行为[和Spring @Transactional]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Web应用程序中使用Spring和Hibernate,将SessionFactory注入到DAO bean中,然后通过webservicecontext将此DAO用于Servlet。

/ p>

DAO方法是事务性的,在我使用的方法之一中... getCurrentSession()。save(myObject);



更新似乎不会立即刷新,大约需要5秒钟查看数据库中的更改。调用DAO更新方法的servlet方法需要几秒钟的时间才能完成。

在执行完DAO的@Transactional方法后,不会发生?这似乎不是一个规则 [我已经看到它]。然后问题是这样的:在每个DAO方法之后如何强制会话刷新?
这可能不是一件好事,但谈论服务层时,一些方法必须以立即刷新结束,并且Hibernate Session行为不可预测。



那么怎么办才能保证我的@Transactional方法在该方法代码的最后一行之后保留所有更改?

  getCurrentSession()。flush()是唯一的解决方案? 

p.s。我在某处读到@Transactional与DB事务相关联。方法返回,事务必须提交。
我没有看到发生这种情况。

解决方案

一旦完成了顶级@Transactional方法(即从您的servlet调用的方法)被提交,并且默认的Hibernate行为是在提交上刷新。



这听起来像是奇怪的事情发生了,你应该做一些调查。



调查您的日志,特别是我将日志级别设置为事务管理器上的DEBUG,以确切了解事务管理器正在做什么。



另外,为hibernate设置日志记录(将show_sql设置为true):当发生刷新时这将输出到System.out,这可能会给你一些线索。



如果您发现任何有趣的事情,请回报。


I use Spring and Hibernate in a web-app,

SessionFactory is injected into a DAO bean, and then this DAO is used in a Servlet through webservicecontext.

DAO methods are transactional, inside one of the methods I use ... getCurrentSession().save(myObject);

One servlet calls this method with an object passed.

The update seems to not be flushed at once, it takes about 5 seconds to see the changes in the database. The servlet's method in which that DAO's update method is called, takes a fraction of second to complete.

After the @Transactional method of DAO is completed, flushing may NOT happen ? It does not seem to be a rule [ I already see it ].

Then the question is this: what to do to force the session to flush after every DAO method? It may not be a good thing to do, but talking about a Service layer, some methods must end with immediate flush, and Hibernate Session behavior is not predictable.

So what to do to guarantee that my @Transactional method persists all the changes after the last line of that method code?

 getCurrentSession().flush() is the only solution?

p.s. I read somewhere that @Transactional IS ASSOCIATED with a DB Transaction. Method returns, transaction must be committed. I do not see this happens.

解决方案

Once the "top level" @Transactional method has completed (ie the method called from your servlet), then the transaction should be committed, and the default Hibernate behaviour is to flush on the commit.

It sounds like something odd is happening and you should do a bit of investigating.

Investigate your logs, in particular I would set the logging level to DEBUG on your Transaction Manager to see exactly what the transaction manager is doing.

Also, set the logging on for hibernate (set show_sql to true): this will output to System.out when flushing is happening and this might give you some clues.

Do report back if you find anything interesting.

这篇关于Hibernate会话刷新行为[和Spring @Transactional]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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