如何配置Hibernate以立即应用所有保存,更新和删除? [英] How can I configure Hibernate to immediately apply all saves, updates, and deletes?

查看:199
本文介绍了如何配置Hibernate以立即应用所有保存,更新和删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何配置 Hibernate ,在数据库服务器之后立即应用所有保存,更新和删除操作会话执行每个操作?默认情况下,Hibernate将所有保存,更新和删除操作排队,只有在执行 flush()操作,提交事务或关闭



立即刷新数据库写入操作的一个好处是,程序可以捕获和处理任何数据库异常(例如 ConstraintViolationException )它们发生。对于晚期或自动刷新,这些异常可能在导致SQL操作的相应Hibernate操作后很久发生。



更新: >

根据Hibernate API文档的会话,在会话结束之前捕获和处理数据库异常的好处可能根本没有好处:如果会话抛出异常,事务必须回滚,并且session ignored。在异常发生后,Session的内部状态可能与数据库不一致。



然后,围绕立即Hibernate使用try-catch块的会话写操作是在异常发生时捕获并记录异常。是否立即刷新这些操作有其他好处?

解决方案


如何配置Hibernate在会话执行每个操作后立即将所有保存,更新和删除到数据库服务器?


据我所知,Hibernate不会提供任何设施。但是,它看起来像Spring,你可以有一些数据访问操作 FLUSH_EAGER ,只需将其 HibernateTemplate HibernateInterceptor 到冲洗模式源< a>)。



但我强烈建议您仔细阅读javadoc (我会在这里回来)。


默认情况下,Hibernate将所有保存,更新和删除操作排入队列,并且只有在flush()操作,提交事务或关闭


关闭会话不会冲洗。


立即刷新数据库写入操作的一个好处是,程序可以捕获和处理发生它们的代码块中的任何数据库异常(例如ConstraintViolationException)。对于迟到或自动刷新,这些异常可能在导致SQL操作的相应Hibernate操作之后很久发生。首先,DBMS的变化如下:




<判断插入(或更新)或后续提交(这称为立即或延迟约束)上的约束违反。所以,没有保证,你的DBA甚至可能不需要立即约束(这应该是默认行为)。



其次,我个人看到更多的缺点,立即冲洗而不是好处,如在 FLUSH_EAGER


急速冲洗导致立即
与数据库同步,
即使在事务中。这导致
不一致性立即显示并抛出
相应的异常,并且
JDBC访问代码参与
同一事务将
更改为数据库已经是
意识到他们了。但缺点
是:




  • 与数据库的额外通信往返,而不是单个
    批处理在事务提交

  • 如果Hibernate
    事务回滚,则需要实际的数据库回滚(由于已经
    提交了SQL语句) 。


相信我,增加数据库往返和放弃语句批处理可能会导致主要性能下降



还要记住,一旦你得到一个异常,除了抛弃你的会话之外,你不能做太多。



总之,我很高兴Hibernate将各种动作排队,我一定不会使用 EAGER_FLUSH flushMode 作为一般设置(但也许仅用于实际需要eager的特定操作,如果有)。


How can I configure Hibernate to apply all saves, updates, and deletes to the database server immediately after the session executes each operation? By default, Hibernate enqueues all save, update, and delete operations and submits them to the database server only after a flush() operation, committing the transaction, or the closing of the session in which these operations occur.

One benefit of immediately flushing database "write" operations is that a program can catch and handle any database exceptions (such as a ConstraintViolationException) in the code block in which they occur. With late or auto-flushing, these exceptions may occur long after the corresponding Hibernate operation that caused the SQL operation.

Update:

According to the Hibernate API documentation for interface Session, the benefit of catching and handling a database exception before the session ends may be of no benefit at all: "If the Session throws an exception, the transaction must be rolled back and the session discarded. The internal state of the Session might not be consistent with the database after the exception occurs."

Perhaps, then, the benefit of surrounding an "immediate" Hibernate session write operation with a try-catch block is to catch and log the exception as soon as it occurs. Does immediate flushing of these operations have any other benefits?

解决方案

How can I configure Hibernate to apply all saves, updates, and deletes to the database server immediately after the session executes each operation?

To my knowledge, Hibernate doesn't offer any facility for that. However, it looks like Spring does and you can have some data access operations FLUSH_EAGER by turning their HibernateTemplate respectively HibernateInterceptor to that flush mode (source).

But I warmly suggest to read the javadoc carefully (I'll come back on this).

By default, Hibernate enqueues all save, update, and delete operations and submits them to the database server only after a flush() operation, committing the transaction, or the closing of the session in which these operations occur.

Closing the session doesn't flush.

One benefit of immediately flushing database "write" operations is that a program can catch and handle any database exceptions (such as a ConstraintViolationException) in the code block in which they occur. With late or auto-flushing, these exceptions may occur long after the corresponding Hibernate operation that caused the SQL operation

First, DBMSs vary as to whether a constraint violation comes back on the insert (or update ) or on the subsequent commit (this is known as immediate or deferred constraints). So there is no guarantee and your DBA might even not want immediate constraints (which should be the default behavior though).

Second, I personally see more drawbacks with immediate flushing than benefits, as explained black in white in the javadoc of FLUSH_EAGER:

Eager flushing leads to immediate synchronization with the database, even if in a transaction. This causes inconsistencies to show up and throw a respective exception immediately, and JDBC access code that participates in the same transaction will see the changes as the database is already aware of them then. But the drawbacks are:

  • additional communication roundtrips with the database, instead of a single batch at transaction commit;
  • the fact that an actual database rollback is needed if the Hibernate transaction rolls back (due to already submitted SQL statements).

And believe me, increasing the database roundtrips and loosing the batching of statements can cause major performance degradation.

Also keep in mind that once you get an exception, there is not much you can do apart from throwing your session away.

To sum up, I'm very happy that Hibernate enqueues the various actions and I would certainly not use this EAGER_FLUSH flushMode as a general setting (but maybe only for the specific operations that actually require eager, if any).

这篇关于如何配置Hibernate以立即应用所有保存,更新和删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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