java commit inside for loop

what are the advantage/disadvantage of using commit inside for loop in JAVA ?

for(int cnt = 0; cnt < pdbResponseVOList.size(); cnt++) {
    //UPDATE STATEMENT
    stmt = vSqlConnection.prepareStatement(vUpdateProvisioningTable);
    int vUpdateSucess = stmt.executeUpdate();
    vSqlConnection.commit();
} //end for loop
//stmt.close()
// vS

qlConnection.close()

Answers


Well, you'll commit each iteration instead of the whole loop and thus you can't easily roll back already committed iterations when a following iteration fails. This can be an advantage or disadvantage, depending on your requirements.


Depends on your task. If your statements are logically associated, then you need to commit after the loop. If your statements are logically independent from each-other, then you can commit inside your loop.

Example1:

You intend to transfer money. First you subtract the sum from the sender, then you add the sum to the receiver. In this case you need to put your commit outside the loop, because you do not intend to subtract the money from the sender but not adding it to the receiver. If something fails, you do not want to commit.

Example2:

You need to update the statuses of your users. If a statement fails, then the other updates should still be executed. In this case you might want to put your commit inside the loop.


Need Your Help

Why aren't my NSViews in my NSWindow resizing when the window is resized?

objective-c macos nswindow nsviewcontroller autoresizingmask

I'm pulling my hair out trying to figure why my views aren't resizing with my NSWindow on drag.

Readfile and print only specific contant

php html

$fh = fopen('http:xxxxxxx.com/rates.php','r') or die($php_errormsg);