赞
踩
事务是应用程序中一些列严密的操作,所有操作要么全部成功执行,要么全部执行失败。
--开启事务
START TRANSACTION;
--执行sql的语句
INSERT INTO records(rid,bid) VALUES(1,2);
UPDATE books SET count=count-1 WHERE bid=2;
--rollback; //手动回滚
--提交事务
COMMIT; //代表一个事务的结束
注意点:
1.事务开启后,一旦执行SQL语句出现错误,事务中所有的操作都将回滚到数据操作前;
2.数据没有提交前,对数据库中的所有操作都不会写进磁盘,一旦发生某些错误,数据将恢复到操作前的状态;
3.数据库使用事务时,提交事务后,如果发生某些SQL的执行错误,系统将自动回滚;
package com.qk.utils; import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; public class DbUtils { private static final DataSource DATA_SOURCE; static { MysqlDataSource db=new MysqlDataSource(); String url="jdbc:mysql://localhost:3306/beta?useSSL=false&characterEncodiing=utf-8&severTimezone=Asia/Shanghai"; db.setUrl(url); db.setUser("root"); db.setPassword("787426"); DATA_SOURCE =db; } public static Connection getConnection() throws SQLException { return DATA_SOURCE.getConnection(); } }
2.测试事务提交
package com.qk; import com.qk.utils.DbUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class Demo1 { public static void main(String[] args) throws SQLException { String sql1="insert into records(rid,bid) values(1,2)"; String sql2="update books set count=count-1 where bid =2"; //同一个事务中,执行sql1和sql2,意味着必须在同一个Connection中完成 try(Connection connection = DbUtils.getConnection()) { //connection中有一个autocommit属性,默认情况下是开启(true) //开启状态下,意味着每一条sql都被视作一个事务 //要让sql1和sql2看作一个整体,就需关闭自动提交,手动提交事务 connection.setAutoCommit(false); try(PreparedStatement ps=connection.prepareStatement(sql1)) { ps.executeUpdate(); } try(PreparedStatement ps=connection.prepareStatement(sql2)) { ps.executeUpdate(); } //手动提交事务,以上数据操作才算真正执行,数据写入磁盘 connection.commit(); } } }
3.JDBC事务使用的四个场景
package com.qk; import com.qk.utils.DbUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class Demo2 { public static void main(String[] args) throws SQLException { String sql1="insert into records(rid,bid) values(1,2)"; String sql2="update books set count=count-1 where bid =2"; try(Connection connection = DbUtils.getConnection()) { try(PreparedStatement ps=connection.prepareStatement(sql1)) { ps.executeUpdate(); } //执行完第一天sql1后,第二天sql2执行失败 try(PreparedStatement ps=connection.prepareStatement(sql2)) { ps.executeUpdate(); } } } }
package com.qk; import com.qk.utils.DbUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class Demo3 { public static void main(String[] args) throws SQLException { String sql1="insert into records(rid,bid) values(1,2)"; String sql2="update books set count=count-1 where bid =2"; try(Connection connection = DbUtils.getConnection()) { //开启事务 connection.setAutoCommit(false); try(PreparedStatement ps=connection.prepareStatement(sql1)) { ps.executeUpdate(); } //执行完第一天sql1后,第二天sql2执行失败 try(PreparedStatement ps=connection.prepareStatement(sql2)) { ps.executeUpdate(); } connection.commit(); } } }
package com.qk; import com.qk.utils.DbUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class Demo4 { public static void main(String[] args) throws SQLException { String sql1="insert into records(rid,bid) values(1,2)"; String sql2="update books set count=count-1 where bid =2"; try(Connection connection = DbUtils.getConnection()) { //开启事务 connection.setAutoCommit(false); try(PreparedStatement ps=connection.prepareStatement(sql1)) { ps.executeUpdate(); } try(PreparedStatement ps=connection.prepareStatement(sql2)) { ps.executeUpdate(); } connection.rollback(); //主动回滚 } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。