简体   繁体   English

如何在春季以编程方式设置事务的回滚点?

[英]How can I programatically set the roll back point for a transaction in spring?

How can I specify the rollback point for a transaction in Spring? 如何在Spring中指定事务的回滚点?

Assuming the following scenario, I have to perform a really long insert into the db which takes quite some times (several minutes). 假设出现以下情况,我必须在数据库中执行很长的插入操作,这需要花费很多时间(几分钟)。 This insert operation is wrapped in a transaction which ensures that if a problem occurs, the transaction is aborted and the database is restored to the status preceding the beginning of the transaction. 此插入操作被包装在事务中,该事务可确保在发生问题时中止事务,并将数据库还原到事务开始之前的状态。

However, this solution affects the performance of the application since other transactions cannot access the db while the long transaction is being executed. 但是,此解决方案影响应用程序的性能,因为在执行长事务时其他事务无法访问该数据库。 I solved this issue by splitting the large transaction in several smaller transactions that perform the same operation. 我通过将大型事务拆分为几个执行相同操作的较小事务来解决了此问题。 However, if one of these small transactions fails, the database rolls back to the status preceding this last transaction. 但是,如果这些小事务之一失败,则数据库将回滚到该最后一个事务之前的状态。 Unfortunately, this would leave the database in an incorrect status. 不幸的是,这会使数据库处于错误状态。 I want that if an errors occurs in any of these smaller transactions, the database rolls back to the status before the first small transaction ( ie exactly the same status, it would roll back if this operation is performed by a singular transaction). 我希望如果这些较小的事务中的任何一个发生错误,则数据库会回滚到第一个小事务之前的状态(即,完全相同的状态,如果此操作由单个事务执行,它将回滚)。

Do you have any suggestion how I can achieve this using Spring transactions? 您对我如何使用Spring交易实现这一点有什么建议吗?

You should look to http://docs.spring.io/spring/docs/4.0.3.RELEASE/javadoc-api/org/springframework/transaction/TransactionStatus.html . 您应该查看http://docs.spring.io/spring/docs/4.0.3.RELEASE/javadoc-api/org/springframework/transaction/TransactionStatus.html It has required functionality: - create savepoint - release savepoint - rollback to savepoint 它具有所需的功能:-创建保存点-释放保存点-回滚到保存点

Of course, your transaction manager (and underlying JDBC driver, and DB) should support the functionality. 当然,您的事务管理器(以及基础的JDBC驱动程序和数据库)应支持该功能。

if you can use same primary key sequence for staging tables and production tables then you shall batch moving data from stg to prod. 如果可以对登台表和生产表使用相同的主键序列,则应将数据从stg批量转移到prod。 when a small transaction fails you can use the keys in staging table to delete from production table. 当小事务失败时,可以使用登台表中的键从生产表中删除。 that way you can restore production table to its original state 这样您就可以将生产表恢复到原始状态

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

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