首页 > 解决方案 > 如何在 Spring Boot 应用程序的事务类中显式提交?

问题描述

我在我的 Spring Boot 应用程序中的一个类上使用事务注释。在此类方法之一中,我们正在从远程位置下载内容并更新数据库。此下载正在循环运行。我只想在此方法中下载 50 次后明确提交。请建议。

标签: javasqlspring-boot

解决方案


有两种方法可以实现这一目标..

  1. 要么在类中维护计数器局部变量状态,并在达到 50 时执行所需的操作。

  2. 尝试在该方法中打开两个事务并通过第一个事务更新数据库中的计数器,第二个事务将处理其余的事情。

Transaction transaction;

transaction = session.beginTransaction();

... (operations in the context of transaction)

transaction.commit();

... (other commands outside of any transaction) transaction = session.beginTransaction();

... (and so on and so forth ....

推荐阅读