控制事务

SELECT * FROM PRODUCTS_TMP;
+---------+-------------------------+-------+
| PROD_ID | PROD_DESC               | COST  |
+---------+-------------------------+-------+
| 11235   | WITCH COSTUME           | 29.99 |
| 119     | ASSORTED MASKS          |  4.95 |
| 13      | FALSE PARAFFIN TEETH    |  1.10 |
| 15      | ASSORTED COSTUMES       | 10.00 |
| 222     | PLASTIC PUMPKIN 18 INCH |  7.75 |
| 6       | PUMPKIN CANDY           |  1.45 |
| 87      | PLASTIC SPIDERS         |  1.05 |
| 9       | CANDY CORN              |  1.35 |
| 90      | LIGHTED LANTERNS        | 14.50 |
+---------+-------------------------+-------+

START TRANSACTION;
DELETE FROM PRODUCTS_TMP WHERE COST < 14;
COMMIT;

SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 29.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
+---------+------------------+-------+
START TRANSACTION;

UPDATE PRODUCTS_TMP
SET COST = 39.99
WHERE PROD_ID = ‘11235‘;

SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 39.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
+---------+------------------+-------+

ROLLBACK;

SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 29.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
+---------+------------------+-------+
START TRANSACTION;

SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 29.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
| 2345    | OAK BOOKSHELF    | 59.99 |
+---------+------------------+-------+

SAVEPOINT sp1;

DELETE FROM PRODUCTS_TMP WHERE PROD_ID = ‘11235‘;

SAVEPOINT sp2;

DELETE FROM PRODUCTS_TMP WHERE PROD_ID = ‘90‘;

SAVEPOINT sp3;

DELETE FROM PRODUCTS_TMP WHERE PROD_ID = ‘2345‘;

SELECT * FROM PRODUCTS_TMP;
Empty set (0.00 sec)

ROLLBACK TO sp2;
SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 90      | LIGHTED LANTERNS | 14.50 |
| 2345    | OAK BOOKSHELF    | 59.99 |
+---------+------------------+-------+

-- 回滚到SAVEPOINT sp2后,sp3失效(=RELEASE SAVEPOINT sp3;)
ROLLBACK TO sp3;
ERROR 1305 (42000): SAVEPOINT sp3 does not exist

-- ROLLBACK回滚到上一个COMMIT或ROLLBACK语句()。
-- 由于之前没有执行过COMMIT,所以这时执行ROLLBACK会撤销全部删除命令
ROLLBACK;
SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 29.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
| 2345    | OAK BOOKSHELF    | 59.99 |
+---------+------------------+-------+

COMMIT;

START TRANSACTION;

SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 29.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
| 2345    | OAK BOOKSHELF    | 59.99 |
+---------+------------------+-------+

SAVEPOINT sp1;
DELETE FROM PRODUCTS_TMP WHERE PROD_ID = ‘11235‘;
SAVEPOINT sp2;
DELETE FROM PRODUCTS_TMP WHERE PROD_ID = ‘90‘;
SAVEPOINT sp3;
DELETE FROM PRODUCTS_TMP WHERE PROD_ID = ‘2345‘;

ROLLBACK TO sp3;
SELECT * FROM PRODUCTS_TMP;
+---------+---------------+-------+
| PROD_ID | PROD_DESC     | COST  |
+---------+---------------+-------+
| 2345    | OAK BOOKSHELF | 59.99 |
+---------+---------------+-------+

ROLLBACK;
SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 29.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
| 2345    | OAK BOOKSHELF    | 59.99 |
+---------+------------------+-------+

COMMIT;
START TRANSACTION;

SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 29.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
| 2345    | OAK BOOKSHELF    | 59.99 |
+---------+------------------+-------+

SAVEPOINT sp1;

DELETE FROM PRODUCTS_TMP WHERE PROD_ID = ‘11235‘;

SAVEPOINT sp2;

DELETE FROM PRODUCTS_TMP WHERE PROD_ID = ‘90‘;

SAVEPOINT sp3;

DELETE FROM PRODUCTS_TMP WHERE PROD_ID = ‘2345‘;

ROLLBACK WORK TO SAVEPOINT sp3;

SELECT * FROM PRODUCTS_TMP;
+---------+---------------+-------+
| PROD_ID | PROD_DESC     | COST  |
+---------+---------------+-------+
| 2345    | OAK BOOKSHELF | 59.99 |
+---------+---------------+-------+

ROLLBACK WORK TO SAVEPOINT sp2;

SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 90      | LIGHTED LANTERNS | 14.50 |
| 2345    | OAK BOOKSHELF    | 59.99 |
+---------+------------------+-------+

DELETE FROM PRODUCTS_TMP WHERE PROD_ID = ‘2345‘;
-- 回滚到指定SAVEPOINT之后,该SAVEPOINT失效(= RELEASE SAVEPOINT sp3;)
ROLLBACK TO sp3;
ERROR 1305 (42000): SAVEPOINT sp3 does not exist

SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 90      | LIGHTED LANTERNS | 14.50 |
+---------+------------------+-------+

-- 回滚到第一个SAVEPOINT,恢复包括sp1/sp2/sp3在内的所有数据
ROLLBACK TO sp1;
SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 29.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
| 2345    | OAK BOOKSHELF    | 59.99 |
+---------+------------------+-------+
时间: 2024-11-05 00:29:56

控制事务的相关文章

MySql命令行控制事务

新建表t1 CREATE TABLE `t1` (   `a` int(11) NOT NULL,   `b` int(11) DEFAULT NULL,   PRIMARY KEY (`a`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 查看mysql系统级别的事务隔离级别: mysql> SELECT @@global.tx_isolation; +-----------------------+ | @@global.tx_isolation | +--

面试之spring是如何控制事务

1.spring的核心是ioc和aop,其中ioc是将控制权交由spring容器进行管理,aop是面向切面编程,内部实现使用的是动态代理,二动态代理内部实现用的是反射.spring的事务是通过aop来实现的,从cglib.jar包就可以看出,是和动态代理相关的. 2.spring的事务的使用:声明式和编程式(可以介绍一下自己喜欢的使用方式). 3.再简单介绍下spring的事务隔离和传播性.(事务最重要的两个特性,是事务的传播级别和数据隔离级别.传播级别定义的是事务的控制范围,事务隔离级别定义的

spring如何控制事务

Spring 的事务,可以说是 Spring AOP 的一种实现. AOP面向切面编程,即在不修改源代码的情况下,对原有功能进行扩展,通过代理类来对具体类进行操作. spring是一个容器,通过spring这个容器来对对象进行管理,根据配置文件来实现spring对对象的管理. spring的事务声明有两种方式,编程式和声明式.spring主要是通过"声明式事务"的方式对事务进行管理,即在配置文件中进行声明,通过AOP将事务切面切入程序,最大的好处是大大减少了代码量.

spring 控制事务

<!-- 对数据源进行事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <aop:config> <aop:pointcut id="transactionPo

在多线程或并发中控制事务的解决方案

1 前言 控制多线程同步的方法很多,比如加同步锁机制,但是这会带来一定的性能消耗,因为要排队机制,前段时间看了别人的代码有个类(ThreadLocal<T>  泛型类)引起了我的注意. 2. 概念ThreadLocal 查看MSDN解释:提供数据的线程本地存储,其实通俗易懂的讲就是一个变量在多个线程中实现多个拷贝(副本),因为多线程的机制,各个线程的变量是互不影响的,所以不用担心变量引起了污染,各用各的不用担心.在数据库连接操作或是ORM上下文操作的时候可以实现多个线程的共用变量. 可以参考引

Oracle——控制事务

一.回滚到保留点 使用 SAVEPOINT 语句在当前事务中创建保存点. 使用 ROLLBACK TO SAVEPOINT 语句回滚到创建的保存点. 二.提交或回滚前的数据状态 改变前的数据状态是可以恢复的 其他用户不能看到当前用户所做的改变,直到当前用户结束事务. DML语句所涉及到的行被锁定, 其他用户不能操作. 三.提交后的数据状态 数据的改变已经被保存到数据库中. 改变前的数据已经丢失. 所有用户可以看到结果. 锁被释放,其他用户可以操作涉及到的数据. 所有保存点被释放. 四.事务实例

JDBC 控制事务(MySQL为例)

事务 一个包含多个步骤的业务操作.如果这个业务操作被事务管理,则这多个步骤要么同时成功,要么同时失败. 对事务的操作 开启事务 提交事务 回滚事务 使用Connection对象来管理事务 java.sql.Connection接口是一个数据库连接对象.它与特定数据库的连接(会话). 执行SQL语句并在连接的上下文中返回结果. 开启事务 setAutoCommit(boolean autoCommit) // 调用该方法设置参数为false,即开启事务 提交事务 commit() // 当所有sq

Spring 的官方的事务控制

Spring 官方版的事务控制 什么是事务? 事务是数据库(RDB)保证业务操作完整性的一种机制(强一致性) 事务在那里控制 业务层控制(Service 或Biz 或Business) 如何控制事务(不同的底层不同的控制) 如果是JDBC:----Connnection Connection.setAutoCommit(false) Connection.commit() Connection.rollback() 如果是Hibernate ------Session ---SessionFac

【转】JDBC事务控制管理

转载声明:本文转载自csdn博客,URL=http://blog.csdn.net/caomiao2006/article/details/22412755 1.事务 (1)事务的概念 事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功. 例如:A——B转帐,对应于如下两条sql语句 update account set money=money-100 where name=‘a’; update account set money=money+100 where na