table 是 innodb 非 myisam
package com.gyarmy.transcation; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import org.junit.Test; import com.gyarmy.utils.JdbcUtils; public class TestTranscation { @Test public void test1(){ Connection conn=null; Statement stmt=null; try { conn = JdbcUtils.getConnection(); conn.setAutoCommit(false); stmt = conn.createStatement(); stmt.executeUpdate("update account set money = money-100 where name = 'aa'"); int i = 1/0; stmt.executeUpdate("update account set money = money+100 where name = 'bb'"); conn.commit(); } catch (Exception e) { // TODO Auto-generated catch block try { conn.rollback(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } e.printStackTrace(); } finally{ JdbcUtils.releaseRes(null, stmt, conn); } } }
0则评论给“jdbc事物处理测试”