开发者

What is the right way to use JDBC transactions in Java?

开发者 https://www.devze.com 2023-01-06 06:14 出处:网络
I\'m using this template: try { connection.setAutoCommit(false); try { // ... do something with that connection ...

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(tr开发者_JAVA百科ue);
    }
} catch (SQLException exception) {
    // log error
}

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消