Spring事务边界和数据库连接保持 [英] Spring transaction boundary and DB connection holding

查看:317
本文介绍了Spring事务边界和数据库连接保持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring boot并通过tomcat连接池在jpa上休眠.您能帮我理解Spring在事务期间如何使用数据库连接.例如,考虑以下情形:

I am using spring boot and hibernate over jpa with tomcat connection pooling. Can you please help me understanding how spring uses DB connections during transactions. For example consider following scenario:

  1. 我们有2个连接的数据库连接池.
  2. Spring启动事务,即使用@Transactional注释修饰的调用方法.
  3. 此方法执行数据库更新
  4. 呼叫外部服务
  5. 收到来自外部服务的响应后,它将更新数据库并返回.
  6. Spring提交交易

假设外部服务(第4步)需要1分钟才能完成,那么数据库池中将有多少个数据库连接可用?假设spring保持DB连接直到事务完成,在这段时间内对于任何收到的请求只有1个DB连接可用,如果我们收到1个以上的请求,他们将不得不等待DB连接.

Assuming the external service(step 4) takes 1 minute to complete, how many DB connections will be available in the DB pool?. Assuming, spring keeps hold of DB connection until the transaction completes, there will be only 1 DB connection available for any request received during this time and if we received more than 1 requests, they will have to wait for DB connection.

请确认我的理解,如果正确,请提出在高交易量系统中如何处理这种情况的建议.

Please confirm my understanding and if it is correct, suggest how I can handle this situation in a high transaction volume system.

谢谢

推荐答案

首先,您的理解是正确的.请参阅有关

First your understanding is correct. See the spring documentation about declarative transaction management.

我猜您在事务内进行了外部服务调用,因为您希望在发生异常的情况下回滚数据库更改.换句话说,您希望数据库更新能够反映外部服务调用的状态.

I guess you do the external service call within the transaction, because you want the database changes to be rollbacked in case of an exception. Or in other words you want the db updates to reflect the state of the external service call.

如果是这样,您将无法将其移出交易边界.在这种情况下,您应该增加连接池的大小,或者也许可以将长期运行的事务委派给处理它们的专用服务器节点.这将保持例如一个主"服务器节点,用于处理长时间运行的事务中的用户请求.

If so you can't move it out the transaction boundary. In this case you should either increase your connection pool size or maybe you can delegate long running transactions to a dedicated server node that handles them. This would keep e.g. a "main" server node that handles user requests away from long running transactions.

您应该考虑数据一致性.确实有必要将数据库更新与外部服务调用同步吗?可以将外部服务呼叫移出事务边界吗?

And you should think about the data consistency. Is it really necessary that the db update must be synchronized with the external service call? Can the external service call be moved out of the transaction boundary?

这篇关于Spring事务边界和数据库连接保持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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