java事务不应该回滚所有记录

标签 java sql database spring transactions

在我的应用程序中,我正在执行以下操作

1. Getting 25 records from the DB.
2. Update every 10 records
   Here I have a loop to update every 10 records.so there will be 3 loops.
   For each loop I need to have a new transaction.(so that only  the 10 record will rollback and not all the records will rollback)   
//line 1  
    getRecords();//25 records from DB  //line 2  
    For(Records r: loop) {  //line 3  
    add 10 records;  //line 4  
    call update method(object with 10 records)   //line 5  
}
  //line 6  
updateRecords()//this method updates the 10 records\\line 7  

这里我想我需要在第 6 行使用@Transaction(REQUIRED)。 这样只有10条记录会回滚,而不是所有记录都会回滚。

有人可以确认这是否是正确的方法吗?

最佳答案

确保在公共(public)方法上设置了@Transactional注释。参见

http://forum.spring.io/forum/spring-projects/data/49494-transactional-on-private-methods

Does Spring @Transactional attribute work on a private method?

如果 updateRecords() 是私有(private)方法,则注释将不起作用。

关于java事务不应该回滚所有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24052273/

相关文章:

java - 使用 FTP 连接时出现 NullPointer

java - 关于从另一个对象访问的 protected 方法访问级别是什么

mysql - 关于确定mysql中一条记录的level id值(用户自定义字段["level_id"])

sql - 如何在 SQL Server Management Studio 中将列值设置为 NULL?

java - 使用流在已知索引处操作字符串

java - Jackson - 将 JSON 反序列化为类

mysql - 从具有相同 id 的两个查询中获取分组记录

database - 如何查询 Sharepoint 数据库?

sql - dbvisualizer:在选择查询中设置最大行数

mysql - INNER JOIN 查询三张表