parallelStream transaction invalidation, null, data duplication

text

transaction failure

Today, I encountered a transaction failure problem. The transaction scenario is Transactional transaction annotation, and the update operation is performed in the list.parallelStream loop. This may cause the transaction to fail, and no exception has been thrown or rolled back. The way I wrote it at the time was to update two tables in a loop. The pseudo code is as follows:

method(list){
    
    
	listObj.parallelStream.for(obj->{
    
    
		tableA.update();
		tableB.update();
	});
}

When looking at the database, sometimes the data update is incomplete. For example, two records should be updated in table A, but one record is updated.

I didn’t look at the source code to get to the bottom of it. When guessing that there should be multiple threads, it should be that multiple clients are enabled for the database, so it is a different transaction! !

The resulting data is abnormal

ParallelStream Pit 2: To solve the problem today, the data collection still throws a null exception even if the data collection satisfies the non-empty condition. Finally, it is concluded that the collection safety problem is caused by the parallelStream parallel flow. Therefore, thread safety should be used in the loop collection class or avoid using

The data is empty and
insert image description here
the data is repeated
insert image description here

A small hole, remember it

Guess you like

Origin blog.csdn.net/qq_16607641/article/details/127558177