Home Database Mysql Tutorial sql 嵌套事务学习笔记

sql 嵌套事务学习笔记

Jun 07, 2016 pm 05:37 PM
affairs study Nested notes

以下内容根据此官方文档修改:(v=sql.105).aspx 嵌套事务的使用场景或者说目的主要是为了调用包含了事务的存储过程。不然没必要使用嵌套事务。 下列示例显示了嵌套事务的用途。在 TransProc 存储过程中包含事务,在另外的代码中分别启动事务调用TransProc和

以下内容根据此官方文档修改:(v=sql.105).aspx

嵌套事务的使用场景或者说目的主要是为了调用包含了事务的存储过程。不然没必要使用嵌套事务。

下列示例显示了嵌套事务的用途。在TransProc 存储过程中包含事务,在另外的代码中分别启动事务调用TransProc和不启动事务调用TransProc。

 

SET QUOTED_IDENTIFIER OFF; GO SET NOCOUNT OFF; GO USE AdventureWorks2008R2; TestTrans(Cola , Colb CHAR(3) NOT NULL); TransProc ( InProc INSERT INTO TestTrans VALUES (@PriKey, @CharCol) , @CharCol) COMMIT TRANSACTION InProc; OutOfProc; ; GO /* Roll back the outer transaction, this will roll back TransProc's nested transaction. OutOfProc; ; GO /* The following SELECT statement shows only rows 3 and 4 are still in the table. This indicates that the commit of the inner transaction from the first EXECUTE statement of TransProc was overridden by the subsequent rollback. TestTrans; GO

嵌套事务有以下特点:

1、SQL Server 数据库引擎将忽略内部事务的提交,除了将@@TRANCOUNT 减 1。内部事务的真正提交或者回滚是依靠最外部事务结束时进行的提交或者回滚。如果提交外部事务,也将提交内部嵌套事务。如果回滚外部事务,也将回滚所有内部事务,不管是否单独提交过内部事务。

2、但针对第1条,假如内部事务进行了提交动作(COMMIT TRANSACTION 或 COMMIT WORK),COMMIT只对当前所处的TRANSACTION起作用,也就是说即使COMMIT TRANSACTION transaction_name中的transaction_name是外部事务的名称,也不会提交该外部事务。这一条的重点在于内部事务无法提交外部事务

3、@@TRANCOUNT 函数记录当前事务的嵌套级别,@@TRANCOUNT=0表示不在事务中,等于1表示在一个事务中,大于1表示处在嵌套事务中。每次 BEGIN TRANSACTION 语句使 @@TRANCOUNT 增加 1,每次 COMMIT TRANSACTION 或 COMMIT WORK 语句使 @@TRANCOUNT 减去 1,而只要有一个ROLLBACK就会使@@TRANCOUNT等于0.

4、ROLLBACK TRANSACTION 语句的 transaction_name只能是最外部事务的事务名称,使用内部事务名称是非法的。带最外部事务名称的ROLLBACK或者不带任何名称的ROLLBACK语句都将回滚所有嵌套事务,,包括最外部事务。此时@@TRANCOUNT等于0

5、针对第4条,这里特别需要说明的是:虽然这里说“ROLLBACK语句都将回滚所有嵌套事务,包括最外部事务”,但这里的前提是在最外层进行ROLLBACK,经过本人亲自实验,如果在内部事务中执行带最外部事务名称的ROLLBACK或者不带任何名称的ROLLBACK,则只回滚当前内部事务和已执行过的外部事务语句,此内部事务后续的外层事务将继续执行,并能成功修改数据,但后续外部事务中的所有rollback和commit都将不起作用,并提示错误信息,因为@@TRANCOUNT早已经等于0,数据库引擎找不到对应的BEGIN TRANSACTION。可以自己通过下面代码示例中的bad code示例进行验证。

 

以下最佳代码示范说明参考以下博文:

NORTHWIND; (N,N) TESTTRAN; CREATE TABLE TESTTRAN ( COLA , COLB CHAR(3)); ; ; OUTERTRAN; BEGIN TRANSACTION INNER1; BEGIN TRANSACTION INNER2; );(, 16, 1) INNER2; ; ); INNER1; ; ); OuterTran; ; TESTTRAN (NOLOCK); SELECT @@Trancount;

下面看一下怎么处理内层事务的错误(何时Rollback, Commit及错误的传递)

NORTHWIND; (N,N) TESTTRAN; CREATE TABLE TESTTRAN ( COLA , COLB CHAR(3)); ; ; OUTERTRAN; BEGIN TRANSACTION INNER1; BEGIN TRANSACTION INNER2; ); INNER2; INNER2; INNER2; ); INNER1; INNER1; INNER1; );(,16,1) OUTERTRAN; ; OUTERTRAN; TESTTRAN (NOLOCK)

考虑到SP的调用,我们开发SP时应该在最后把@@ERROR返回供调用者检查。另外测试注意检查一下@@Trancount,有时结果看似正确,但是如果@@Trancount不等于0,说明我们的代码出了问题。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? Mar 21, 2024 pm 09:30 PM

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of "What to do if the notes published by Xiaohongshu are missing" and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click "Me" → "Publish" → "All Publications" to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu Mar 12, 2024 am 10:40 AM

How to add product links in notes in Xiaohongshu? In the Xiaohongshu app, users can not only browse various contents but also shop, so there is a lot of content about shopping recommendations and good product sharing in this app. If If you are an expert on this app, you can also share some shopping experiences, find merchants for cooperation, add links in notes, etc. Many people are willing to use this app for shopping, because it is not only convenient, but also has many Experts will make some recommendations. You can browse interesting content and see if there are any clothing products that suit you. Let’s take a look at how to add product links to notes! How to add product links to Xiaohongshu Notes Open the app on the desktop of your mobile phone. Click on the app homepage

Can generic functions in Go be nested within each other? Can generic functions in Go be nested within each other? Apr 16, 2024 pm 12:09 PM

Nested Generic Functions Generic functions in Go 1.18 allow the creation of functions that apply to multiple types, and nested generic functions can create reusable code hierarchies: Generic functions can be nested within each other, creating a nested code reuse structure. By composing filters and mapping functions into a pipeline, you can create reusable type-safe pipelines. Nested generic functions provide a powerful tool for creating reusable, type-safe code, making your code more efficient and maintainable.

Revealing the appeal of C language: Uncovering the potential of programmers Revealing the appeal of C language: Uncovering the potential of programmers Feb 24, 2024 pm 11:21 PM

The Charm of Learning C Language: Unlocking the Potential of Programmers With the continuous development of technology, computer programming has become a field that has attracted much attention. Among many programming languages, C language has always been loved by programmers. Its simplicity, efficiency and wide application make learning C language the first step for many people to enter the field of programming. This article will discuss the charm of learning C language and how to unlock the potential of programmers by learning C language. First of all, the charm of learning C language lies in its simplicity. Compared with other programming languages, C language

MySQL transaction processing: the difference between automatic submission and manual submission MySQL transaction processing: the difference between automatic submission and manual submission Mar 16, 2024 am 11:33 AM

MySQL transaction processing: the difference between automatic submission and manual submission. In the MySQL database, a transaction is a set of SQL statements. Either all executions are successful or all executions fail, ensuring the consistency and integrity of the data. In MySQL, transactions can be divided into automatic submission and manual submission. The difference lies in the timing of transaction submission and the scope of control over the transaction. The following will introduce the difference between automatic submission and manual submission in detail, and give specific code examples to illustrate. 1. Automatically submit in MySQL, if it is not displayed

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

Getting Started with Pygame: Comprehensive Installation and Configuration Tutorial Getting Started with Pygame: Comprehensive Installation and Configuration Tutorial Feb 19, 2024 pm 10:10 PM

Learn Pygame from scratch: complete installation and configuration tutorial, specific code examples required Introduction: Pygame is an open source game development library developed using the Python programming language. It provides a wealth of functions and tools, allowing developers to easily create a variety of type of game. This article will help you learn Pygame from scratch, and provide a complete installation and configuration tutorial, as well as specific code examples to get you started quickly. Part One: Installing Python and Pygame First, make sure you have

See all articles