1. MySQL默认自动提交(autocommit=1),如果要开启事务,先关闭自动提交(autocommit=0);
  2. InnoDB支持事务,MyISAM不支持;
  3. MySQL不支持事务嵌套;已经开启事务后,再开启事务(start transaction),会隐式的提交(commit)上一个事务。

MySQL中可以通过部分事务(savepoint)来实现嵌套

TRUNCATE nested_transaction;
BEGIN;
INSERT INTO `nested_transaction`(id) VALUES(1);
SAVEPOINT t1;
INSERT INTO `nested_transaction`(id) VALUES(2);
ROLLBACK TO SAVEPOINT t1;
COMMIT;

id
--------
1

PHP中Laravel中事务嵌套(ManagesTransactions.php)

<?php

namespace Illuminate\Database\Concerns;

use Closure;
use Exception;
use Throwable; trait ManagesTransactions
{
/**
* Execute a Closure within a transaction.
*
* @param \Closure $callback
* @param int $attempts
* @return mixed
*
* @throws \Exception|\Throwable
*/
public function transaction(Closure $callback, $attempts = 1)
{
for ($currentAttempt = 1; $currentAttempt <= $attempts; $currentAttempt++) {
$this->beginTransaction(); // We'll simply execute the given callback within a try / catch block and if we
// catch any exception we can rollback this transaction so that none of this
// gets actually persisted to a database or stored in a permanent fashion.
try {
return tap($callback($this), function ($result) {
$this->commit();
});
} // If we catch an exception we'll rollback this transaction and try again if we
// are not out of attempts. If we are out of attempts we will just throw the
// exception back out and let the developer handle an uncaught exceptions.
catch (Exception $e) {
$this->handleTransactionException(
$e, $currentAttempt, $attempts
);
} catch (Throwable $e) {
$this->rollBack(); throw $e;
}
}
} /**
* Handle an exception encountered when running a transacted statement.
*
* @param \Exception $e
* @param int $currentAttempt
* @param int $maxAttempts
* @return void
*
* @throws \Exception
*/
protected function handleTransactionException($e, $currentAttempt, $maxAttempts)
{
// On a deadlock, MySQL rolls back the entire transaction so we can't just
// retry the query. We have to throw this exception all the way out and
// let the developer handle it in another way. We will decrement too.
if ($this->causedByDeadlock($e) &&
$this->transactions > 1) {
--$this->transactions; throw $e;
} // If there was an exception we will rollback this transaction and then we
// can check if we have exceeded the maximum attempt count for this and
// if we haven't we will return and try this query again in our loop.
$this->rollBack(); if ($this->causedByDeadlock($e) &&
$currentAttempt < $maxAttempts) {
return;
} throw $e;
} /**
* Start a new database transaction.
*
* @return void
* @throws \Exception
*/
public function beginTransaction()
{
$this->createTransaction(); ++$this->transactions; $this->fireConnectionEvent('beganTransaction');
} /**
* Create a transaction within the database.
*
* @return void
*/
protected function createTransaction()
{
if ($this->transactions == 0) {
try {
$this->getPdo()->beginTransaction();
} catch (Exception $e) {
$this->handleBeginTransactionException($e);
}
} elseif ($this->transactions >= 1 && $this->queryGrammar->supportsSavepoints()) {
$this->createSavepoint();
}
} /**
* Create a save point within the database.
*
* @return void
*/
protected function createSavepoint()
{
$this->getPdo()->exec(
$this->queryGrammar->compileSavepoint('trans'.($this->transactions + 1))
);
} /**
* Handle an exception from a transaction beginning.
*
* @param \Exception $e
* @return void
*
* @throws \Exception
*/
protected function handleBeginTransactionException($e)
{
if ($this->causedByLostConnection($e)) {
$this->reconnect(); $this->pdo->beginTransaction();
} else {
throw $e;
}
} /**
* Commit the active database transaction.
*
* @return void
*/
public function commit()
{
if ($this->transactions == 1) {
$this->getPdo()->commit();
} $this->transactions = max(0, $this->transactions - 1); $this->fireConnectionEvent('committed');
} /**
* Rollback the active database transaction.
*
* @param int|null $toLevel
* @return void
*/
public function rollBack($toLevel = null)
{
// We allow developers to rollback to a certain transaction level. We will verify
// that this given transaction level is valid before attempting to rollback to
// that level. If it's not we will just return out and not attempt anything.
$toLevel = is_null($toLevel)
? $this->transactions - 1
: $toLevel; if ($toLevel < 0 || $toLevel >= $this->transactions) {
return;
} // Next, we will actually perform this rollback within this database and fire the
// rollback event. We will also set the current transaction level to the given
// level that was passed into this method so it will be right from here out.
$this->performRollBack($toLevel); $this->transactions = $toLevel; $this->fireConnectionEvent('rollingBack');
} /**
* Perform a rollback within the database.
*
* @param int $toLevel
* @return void
*/
protected function performRollBack($toLevel)
{
if ($toLevel == 0) {
$this->getPdo()->rollBack();
} elseif ($this->queryGrammar->supportsSavepoints()) {
$this->getPdo()->exec(
$this->queryGrammar->compileSavepointRollBack('trans'.($toLevel + 1))
);
}
} /**
* Get the number of active transactions.
*
* @return int
*/
public function transactionLevel()
{
return $this->transactions;
}
}

也可以只开启一个事物,判断如果是嵌套事务,则不做操作直接返回。

MySQL 事务嵌套的更多相关文章

  1. MySql中的事务嵌套

    1.Mysql中的事务必须是InnoDB.Berkeley DB引擎,myisam不支持. 2.Mysql是不支持嵌套事务的,开启了一个事务的情况下,再开启一个事务,会隐式的提交上一个事务. 3.My ...

  2. Mysql事务及行级锁的理解

    在最近的开发中,碰到一个需求签到,每个用户每天只能签到一次,那么怎么去判断某个用户当天是否签到呢?因为当属表设计的时候,每个用户签到一次,即向表中插入一条记录,根据记录的数量和时间来判断用户当天是否签 ...

  3. 第五章 MySQL事务,视图,索引,备份和恢复

    第五章 MySQL事务,视图,索引,备份和恢复 一.事务 1.什么是事务 事务是一种机制,一个操作序列,它包含了一组数据库操作命令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求.要么都执行 ...

  4. Mysql事务及行级锁

    事务隔离级别 数据库事务隔离级别,只是针对一个事务能不能读取其它事务的中间结果. Read Uncommitted (读取未提交内容) 在该隔离级别,所有事务都可以看到其他未提交事务的执行结果.本隔离 ...

  5. 10.mysql事务管理及python操作

    在用户操作MySQL过程中,对于一般简单的业务逻辑或中小型程序而言,无需考虑应用MySQL事务.但在比较复杂的情况下,往往用户在执行某些数据操作过程中,需要通过一组SQL语句执行多项并行业务逻辑或程序 ...

  6. mysql事务的开启

    mysql事务的开启 对于一个MYSQL数据库(InnoDB),事务的开启与提交模式无非下面这两种情况: 1>若参数autocommit=0,事务则在用户本次对数据进行操作时自动开启,在用户执行 ...

  7. Mysql事务探索及其在Django中的实践(二)

    继上一篇<Mysql事务探索及其在Django中的实践(一)>交代完问题的背景和Mysql事务基础后,这一篇主要想介绍一下事务在Django中的使用以及实际应用给我们带来的效率提升. 首先 ...

  8. MySQL实现嵌套集合模型

    MySQL实现嵌套集合模型 译文主要是介绍如何用MySQL来存储嵌套集合数据.在其中会增加一些自己的理解,也会删除掉一些自认为无用的废话. 这篇文章主要讲的是嵌套集合模型,所以邻接表不是本文的重点,简 ...

  9. MySQL 事务

    MySQL 事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数据库操作语句就构成 ...

随机推荐

  1. java学习笔记(2)——函数

    int a = 1; a = a++; 1,取出a的值1作为a++表达式的值(a++表达式等于1) 2,a增加1变为2 3,执行赋值运算,a++表达式的值再赋给a,a又成为了1. ---------- ...

  2. mod_timer函数及其他定时器函数

    当一个定时器已经被插入到内核动态定时器链表中后,我们还能够改动该定时器的expires值.函数mod_timer()实现这一点 改动注冊入计时器列表的handler的起动时间 int mod_time ...

  3. BLAS 与 Intel MKL 数学库

    0. BLAS BLAS(Basic Linear Algebra Subprograms)描述和定义线性代数运算的规范(specification),而不是一种具体实现,对其的实现包括: AMD C ...

  4. python 教程 第五章、 函数

    第五章. 函数 定义语句后面要加冒号 1)    定义函数 def sayHello(): print 'Hello World!' sayHello() 2)    变量作用域 LEGB原则 L本地 ...

  5. leetcode先刷_Pascal&#39;s Triangle II

    三角相对简答题.第一个问题是太简单.我不沾了,我一定会写.其实没什么的第二个问题,与这个问题计算路径有点像一个三角形,假定输入是n,然后从第一行计数到第一n行,保存在数据线上的时间到,由于只有相关的事 ...

  6. MySQL更改表的存储引擎

    MySQL它提供了多种数据库存储引擎,存储引擎负责MySQL存储和检索数据的数据库.不同的存储引擎具有不同的特性,能须要将一个已经存在的表的存储引擎转换成另外的一个存储引擎.有非常多方法能够完毕这样的 ...

  7. uboot初体验-----趣谈nand设备发起的浅显理解

    1 选择Uboot版本号 2 移植uboot至console正常work 3 制造uImage和使用uboot指南 4 写NFC驱动器 5 uboot从nand启动引导系统 1 选择Uboot版本号 ...

  8. XF 表视图添加和删除行

    using System;using Xamarin.Forms;using Xamarin.Forms.Xaml; [assembly: XamlCompilation (XamlCompilati ...

  9. PySide——Python图形化界面入门教程(四)

    PySide——Python图形化界面入门教程(四) ——创建自己的信号槽 ——Creating Your Own Signals and Slots 翻译自:http://pythoncentral ...

  10. 图像滤镜艺术---(Lightleaks Filter)漏光滤镜

    原文:图像滤镜艺术---(Lightleaks Filter)漏光滤镜 (Lightleaks Filter)漏光滤镜 漏光拍摄其实就是一种摄影手法,最初是因为强烈光照导致相片交卷的过分曝光,最终在成 ...