JDBC 事务(二)回滚到保存点
public class SavePointTest {
/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
test();
}
static void test() throws SQLException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
Savepoint sp = null;
try {
conn = JdbcUtils.getConnection();
conn.setAutoCommit(false);
st = conn.createStatement();
String sql = "update user set money=money-10 where id=1";
st.executeUpdate(sql);
sp = conn.setSavepoint();
sql = "update user set money=money-10 where id=3";
st.executeUpdate(sql);
sql = "select money from user where id=2";
rs = st.executeQuery(sql);
float money = 0.0f;
if (rs.next()) {
money = rs.getFloat("money");
}
if (money > 300)
throw new RuntimeException("已经超过最大值!");
sql = "update user set money=money+10 where id=2";
st.executeUpdate(sql);
conn.commit();
} catch (RuntimeException e) {
if (conn != null && sp != null) {
conn.rollback(sp);
conn.commit();
}
throw e;
} catch (SQLException e) {
if (conn != null)
conn.rollback();
throw e;
} finally {
JdbcUtils.free(rs, st, conn);
}
}
}
相关文章
- MySQL事务隔离级别,锁(转)
- oracle事务内部原理
- redis事务
- 一文读懂分布式事务一致性
- Java -- JDBC 学习--事务
- C# 用delegate实现AOP事务[C# | AOP | delegate]
- Java JDBC篇3——JDBC事务
- SAP ABAP SICF事务码和SAP Hybris的链式过滤器filter chain
- Atitit db access req数据库访问规范jdo jdbc jpa pdo sql 目录 1. 常见特性1 1.1. 元数据 API1 1.2. 分布式事务 vs事务中使用 Sav
- 数据库的事务
- STM 软件事务内存——本质是为提高并发,通过事务来管理内存的读写访问以避免锁的使用
- springboot整合多数据源解决分布式事务
- Spring Cloud Alibaba微服务第21章之分布式事务
- MySQL学习10:视图&事务&索引