Is there a good way to execute MySQL statements atomically via JDBC?
Posted
by javanix
on Stack Overflow
See other posts from Stack Overflow
or by javanix
Published on 2010-05-10T22:17:14Z
Indexed on
2010/05/10
22:24 UTC
Read the original article
Hit count: 299
Suppose I have a table that contains valid data. I would like to modify this data in some way, but I'd like to make sure that if any errors occur with the modification, the table isn't changed and the method returns something to that effect.
For instance, (this is kind of a dumb example, but it illustrates the point so bear with me) suppose I want to edit all the entries in a "name" column so that they are properly capitalized. For some reason, I want either ALL of the names to have proper capitalization, or NONE of them to have proper capitalization (and the starting state of the table is that NONE of them do).
Is there an already-implemented way to run a batch update on the table and be assured that, if any one of the updates fails, all changes are rolled back and the table remains unchanged?
I can think of a few ways to do this by hand (though suggestions are welcomed), but it'd be nice if there was some method I could use that would function this way. I looked at the java.sql.statement.executeBatch()
command, but I'm not convinced by the documentation that my table wouldn't be changed if it failed in some manner.
© Stack Overflow or respective owner