单个hibernate会话中的多个事务(使用Spring) [英] Multiple transactions in a single hibernate session (with Spring)

查看:121
本文介绍了单个hibernate会话中的多个事务(使用Spring)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  • 打开会话

  • 可以使用Hibernate + Spring对以下模型进行建模:开始交易

  • 做一些工作

  • 提交

  • 开始交易 li>更多工作

  • 提交

  • 关闭会话



  • 我使用Spring TransactionTemplate来完成session +事务生命周期范围的确定。



    原因是有时我在业务流程中有几个阶段,我想在每个阶段完成后提交。不过,我想继续使用相同的持久对象。如果每笔交易都有一个单独的会话,那么我会收到临时/分离异常,因为原来的会话已关闭。



    这可能吗?


    <是的,Hibernate的会话可以开始并提交多个事务。你需要做的是在某个地方存储打开的会话,然后重用它。请注意,Session不是一个线程安全的对象,但是如果您确定它不会遇到并发问题,那么您需要的仅仅是使用 TransactionSynchronizationUtils 来绑定一个会话到线程资源,然后在需要的时候解除绑定,你可以找到一个例子 here 或者您可以看看OSIV及其标准实现。



    这是一件非常复杂的事情,因为它可能会带来麻烦,所以它更容易,因此希望您马上关闭会话并且不要重复使用它。




    • 缓存中的对象不会自动清除,因此您的会话将会变大,直到OutOfMemory。 会话内部的对象不会刷新除非它们很脏,因此另一个用户改变对象的机会越来越大。确保只有一个用户要更改可写对象。

    • 如果在某个步骤中发生某种异常,则必须确保关闭会话。在Session内发生异常之后,此对象不可重用。
    • 如果事务回滚,则会话由Spring清除,因此所有对象都将分离。如果至少有一个交易已回滚,请确保您放弃所有交易。


    Is it possible to model the following using Hibernate + Spring.

    • Open session
    • Begin transaction
    • Do some work
    • Commit
    • Begin transaction
    • More work
    • Commit
    • Close session

    I use the Spring TransactionTemplate which does both session + transaction lifetime scoping.

    The reason is that sometimes I have a few stages in a business process and I would like to commit after each stage completes. However I would like to continue using the same persistent objects. If I have a separate session per transaction then I get transient/detached exceptions because the original session was closed.

    Is this possible?

    解决方案

    Yes, Hibernate's Sessions can begin and commit several transactions. What you need to do is to store open session somewhere, then reuse it. Note, that Session is not a thread-safe object, but if you're sure it won't have problems with concurrency, what you need is just to use TransactionSynchronizationUtils to bind a session to the thread resources and then unbind it when desired, you can find an example here or you can take a look at OSIV and its standard implementations.

    This is a very complicated thing, it's much easier and thus desirable that you close your session right away and don't reuse it, because it may bring troubles:

    • The objects inside of cache are not automatically evicted, thus your Session will grow in size until OutOfMemory.
    • The objects inside of session are not flushed unless they are dirty, thus the chance that object was changed by another user is larger and larger. Ensure that only a single user is going to change writable objects.
    • If some exception happens during one of steps, you have to ensure you close the session. After exception occurred inside of Session, this object is not reusable.
    • If transaction was rolled back, the session is cleared by Spring, thus all your objects become detached. Make sure your discard everything if at least one of transactions was rolled back.

    这篇关于单个hibernate会话中的多个事务(使用Spring)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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