简体   繁体   English

跨多个Web服务的交易

[英]Transaction across multiple web services

I have quite a simple problem. 我有一个简单的问题。 I am rewriting very old app which is using direct access to database through DAO objects. 我正在重写非常老的应用程序,该应用程序通过DAO对象直接访问数据库。 There is no business layer (the code is not mine and is quite anti-code), so connection.setAutoCommit(false) is used for starting the transactions everywhere in the code. 没有业务层(该代码不是我的代码,并且完全是反代码),因此使用connection.setAutoCommit(false)在代码中的任何地方启动事务。 I had to rewrite the project because of security reasons, so it does not use database connection but webservices and hibernate/jpa on the J2EE server side (before it was standalone app, now app+j2ee). 由于安全原因,我不得不重写该项目,因此它不使用数据库连接,而是在J2EE服务器端使用webservices和hibernate / jpa(以前是独立应用程序,现在是app + j2ee)。 Simple - I just moved the DAO/VO objects to the webservice server and rewrote sql to hql and DAO in client replaced with webservice client. 简单-我只是将DAO / VO对象移到了Web服务服务器,并将sql重新写到了hql,而在用Webservice client替换的客户端中的DAO。

But what to do with transaction code? 但是如何处理交易代码? Normally one transaction one webservice call. 通常,一次交易一个Web服务调用。 So I need some mechanism (parameter in webservices?) that could help me to reference to the same hibernate transaction across multiple webservice calls. 因此,我需要某种机制(Web服务中的参数?)来帮助我在多个Web服务调用之间引用同一休眠事务。 Is it completely bad approach and should I just move the transactions in server code? 这是完全不好的方法吗?我应该只在服务器代码中移动事务吗?

I think you should use SessionBeans expose as JAX-RS services, and let them control the transactions. 我认为您应该将SessionBeans公开用作JAX-RS服务,并让它们控制事务。 If you need to have a transaction accross multiple webservice calls, just define a new webservice, also a EJB SessionBean that acts as a facade for the other calls. 如果您需要跨多个Web服务调用进行事务处理,则只需定义一个新的Web服务,还定义一个EJB SessionBean作为其他调用的基础。 I think is a bad practice to implement what you suggest (with referecing the same hibernate transaction), and I think it might not even be possible. 我认为实现您的建议(引用相同的休眠事务)是一种不好的做法,而且我认为甚至不可能。 Each WS call is a separate thread, at different moment in times, mixing transactions across threads is not a good practice. 每个WS调用都是一个单独的线程,在不同的时间点,跨线程混合事务不是一个好习惯。

I also think this is bad practice, because you usually build webservice methods that are coarse grained. 我也认为这是不好的做法,因为您通常会构建粗粒度的Web服务方法。 So usually you are fine with one request per transaction. 因此通常情况下,每笔交易只有一个请求就可以了。

I can understand your need but think about the downsides: 我可以理解您的需求,但请考虑以下方面:

  • How will you do a rollback about several transactions? 您将如何回滚几笔交易? This will introduce data inconsistencies, if not possible. 如果可能的话,这将导致数据不一致。
  • If this is possible, your webservice won't be stateless anymore, which is commonly considered bad practice. 如果可能的话,您的Web服务将不再是无状态的,这通常被认为是不良做法。
  • This means, your API requests will be dependent on each other, so you have prerequisites for executing any of your request. 这意味着,您的API请求将相互依赖,因此您具有执行任何请求的先决条件。

Have you tried to put your transaction within one request? 您是否尝试过将交易放入一个请求中? This might help to re-structure and possibly enhance the code of your app. 这可能有助于重构结构,并可能增强应用程序的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM