Hibernate、Spring Web Service事务问题
我有两个应用程序。一个是 OrderService,另一个是 TradeService。 在贸易服务中,有一种交易方法“购买”。
当交易服务使用 Web 服务调用订单服务的 SubmitOrder 方法时,会出现问题。
问题描述:
Buy 方法中的所有步骤都在一笔交易下。
当我们调用 buy 方法时,它会创建一个 Order 对象。
当此方法使用 Web 服务调用 Submit Order 方法时,它会传递订单 Id。
提交订单方法使用此 ID 从数据库加载订单对象。
但是在 Load 方法中它给出了延迟初始化异常:会话已关闭。 (两个应用程序共享相同的数据库)
可能的解决方案是使 Submit Order 方法具有事务性。
这会导致另一个死锁问题。
- 僵局的原因, A. Buy 方法锁定 ID 为“xyz”的订单对象。并发送相同的 ID 到提交订单方法。 B. 现在Buy方法正在等待提交订单的回复。 C. Submit Order 方法无法加载 order 对象,因为它已被 buy 方法锁定。 D. 所以 Submit Order 方法正在等待获取锁。因此这是死锁状态。
注意:这两个应用程序都使用 Spring MVC、Spring Web Service 和 Hibernate。
请帮助我摆脱这种情况。提前致谢。(如果需要,我可以发布部分代码)
I have two applications. One is OrderService and another is TradeService.
In trade service there is a method 'Buy' which is transactional.
Issue arises when Trade Service calls SubmitOrder method of Order service using Web Service.
Description of Issue:
All steps in Buy method are under one transaction.
When we call buy method it creates one Order Object.
When this method calls Submit Order method using web service, It passes order Id.
Submit Order method use this Id to load Order Object from DB.
But in Load method It gives Lazy Initialization Exception: Session is closed.
(Both Applications share same Database)Probable solution to this is make Submit Order method transactional.
It leads to another Deadlock issue.
- Reason for deadlock,
A. Buy method Locks Order Object with Id say 'xyz'. and sends same Id to submit order method.
B. Now Buy method is waiting for reply from submit order.
C. Submit Order method can not load order Object as it is already locked by buy method.
D. So Submit Order method is waiting to acquire lock. Hence it is dead lock condition.
- Reason for deadlock,
Note: Both Applications use Spring MVC, Spring Web Service and Hibernate.
Please Help me out of this situation. Thanks In advance.(I can post part of the code If required)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,不可能在 Web 服务调用的两侧使用相同的数据库事务。 -- 至少根本不可能通过 Web 服务传输连接。
因此,您必须接受这样一个事实:您拥有/需要两个数据库事务。两边各一个。 -- 除此之外(这会导致您的延迟初始化异常),如果 Web 服务 - 服务器端接收一个实体,它不会附加到任何会话,因此如果您尝试访问尚未加载(layz 加载)的属性然后得到这个layz加载异常。
您需要做的是,打开一个交易,然后首先将实体附加到该交易中。
In general it is not possible to use the same Database Transaction on both sides of an web service invocation. -- It is at least simply impossible to transfer the connection over the web service.
So you have to live with the fact, that you have/need two Database transactions. One one each side. -- And in addition to that (that causes your Lazy Initialization Exception), if the web service - server side recive a entity, it is not attached to any session, so if you try to access a not yet loaded (layz loaded) property then get this layz loading exception.
What you need to do is, open a transaction, and attach the entity to this transaction first.
TradeService 为什么要创建订单?我希望这是 OrderService 的责任。这样就不会存在对同一资源的争用。
是的,这两项服务都应该是事务性的。您收到 LazyInitializationException 是因为您的加载方法可能进行 2 个单独的数据库调用来构建 Order 实例,并且会话在第一次数据库调用后关闭。
Why is the TradeService creating the order? I would expect that to be the responsibility of the OrderService. That way there is no contention for the same resource as well.
And yes, both services should be transactional. You are getting LazyInitializationException because your load method is probably making 2 separate database calls to build the Order instance and the session's closed after the first database call.