views:

54

answers:

1

I'm using this template:

try {
    connection.setAutoCommit(false);

    try {
        // ... do something with that connection ...
        connection.commit();
    catch (SQLException exception) {
        connection.rollback();
        throw exception;
    } finally {
        connection.setAutoCommit(true);
    }
} catch (SQLException exception) {
    // log error
}

Is this the right way? How can this template be improved?

+1  A: 

Your code should work fine. Do you get any errors or anything else?

Here's an example on using JDBC Transaction anyway

http://www.java2s.com/Code/Java/Database-SQL-JDBC/JDBCTransaction.htm

P.S. Specify your problem and I'll try to help.

hgulyan
I don't have any particular problems with this code, I'm trying to avoid them in future :)
Shooshpanchick
Then just check link in my answer for an example. You won't have problems, your code is correct. Good Luck.
hgulyan