简体   繁体   English

如何在@Transactional带注释的方法中将状态提交给DB?

[英]How to commit status to DB within @Transactional annotated method?

I am using JPA through Hibernate in Spring. 我在春季通过Hibernate使用JPA。 I need to process a lot of data in a @Transactional method. 我需要在@Transactional方法中处理大量数据。 The process may take quite some time, so I want to query and display the progress on my front-end. 该过程可能会花费一些时间,因此我想查询并在前端显示进度。 "50 of 1000 items have been processed", etc. “已处理1000个项目中的50个”,等等。

But how can I commit the process status into DB during data is being processed within a @Transactional annotated method? 但是,在使用@Transactional带注释的方法处理数据期间,如何将过程状态提交到DB中?

It may work by making a remote call, any other better ways? 可以通过拨打远程电话或其他更好的方法来工作吗?

Thanks in advance. 提前致谢。

You don't need a query for the update status. 您不需要查询更新状态。 You can use JMS to push progress notifications so the UI can consume them while the transaction is still running (AJAX progress bar + a REST service which can consume the JMS updates). 您可以使用JMS推送进度通知,以便UI可以在事务仍在运行时使用它们(AJAX进度栏+可以使用JMS更新的REST服务)。

If you don't want JMS you can use any other messaging technology for this task. 如果您不想使用JMS,则可以使用任何其他消息传递技术来完成此任务。 You cam even use a Cache for setting the update progress and read it in some other thread. 您甚至可以使用缓存来设置更新进度,并在其他线程中读取它。

You can even write your own mechanism backed by a java.util.concurrent blocking queue. 您甚至可以编写由java.util.concurrent阻塞队列支持的自己的机制。

Bottom line, you don't need the db for monitoring the progress of that batch processing job. 最重要的是,您不需要数据库来监视该批处理作业的进度。

The @Transactional will commit once it finish all records it processes. @Transactional一旦完成处理的所有记录,便会提交。 So if you want to commit batch by batch, you have to call your db calling method as batch by batch. 因此,如果要逐批提交,则必须逐批调用数据库调用方法。

I have found something from Here. 我从这里找到了一些东西

get TransactionStatus using TransactionAspectSupport.currentTransactionStatus() inject transaction manager to your bean (assuming you are using hibernate) try to invoke doCommit(DefaultTransactionStatus status) in transaction manager. 使用TransactionAspectSupport.currentTransactionStatus()获取TransactionStatus将事务管理器注入到您的bean中(假设您使用的是hibernate),尝试在事务管理器中调用doCommit(DefaultTransactionStatus status)

try this out not sure it will work or not because as per spring doc Spring Doc 试试这个不确定它是否可以正常工作,因为根据Spring Doc Spring Doc

You are strongly encouraged to use the declarative approach to rollback if at all possible. 强烈建议您尽可能使用声明性方法进行回滚。 Programmatic rollback is available should you absolutely need it, but its usage flies in the face of achieving a clean POJO-based architecture. 如果您绝对需要它,则可以使用程序化回滚,但是面对实现干净的基于POJO的体系结构时,它的用法就不那么理想了。

暂无
暂无

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

相关问题 回滚 @Transactional 注释的方法 - Rollback a @Transactional annotated method 如果在内部@Spring注释了方法,则Spring @Transactional注释方法是否会覆盖@Transactional(readOnly = true)方法? - Does a Spring @Transactional annotated method overwrite an @Transactional(readOnly=true) method if called within it? 从另一个@Transactional注释方法调用@Transactional注释方法 - Call @Transactional annotated method from another @Transactional annotated method 与方法注解者@Transactional的通信 - Communication to caller of method annotated @Transactional Spring 为在 @Transactional 注释方法中调用的每个 JpaRepository 方法打开一个新事务 - Spring opens a new transaction for each JpaRepository method that is called within an @Transactional annotated method 从服务类中调用时,Spring @Transactional不适用于带注释的方法 - Spring @Transactional doesn't work for an annotated method when called from within service class Spring AOP:具有@Transactional注释方法的方法的注释切入点? - Spring AOP : Annotated pointcuts for a method with @Transactional annotated method? 当在类和方法中都添加了带注释的Transactional时,如何使事务仅在方法中生效? - How to make transaction only effect in a method when a annotated Transactional is added in both class and method? @Transactional 注释方法是否等待 Spring 中的成功提交? - Do @Transactional annotated methods wait for a successful commit in Spring? Spring乐观锁定:如何重试事务方法直到提交成功 - Spring Optimistic Locking:How to retry transactional method till commit is successful
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM