What is a python transaction? Four attributes to help you understand the business

Source: Internet
Author: User
Tags rollback
In this article, let's take a look at Pythonmysql Transactions, for a friend who has just come into contact with Python's programming language, it may python TransactionsThere is little understanding of control related aspects, and I do not understand what a Python transaction is, so in this article we will talk about the knowledge of pythonmysql transaction control.

What is a transaction

A transaction (Transaction) is a unit of concurrency control and is a user-defined sequence of operations. These operations are either done or not, and are an inseparable unit of work. With transactions, SQL Server can bind a logically related set of operations so that the server maintains the integrity of the data. A broad example of transactional applications is the deposit and withdrawal business in banks.

Transaction properties

Transaction mechanisms ensure data consistency.

A transaction should have 4 properties : atomicity, consistency, isolation, persistence. These four properties are often called acid properties.

1. atomicity (atomicity). A transaction is an inseparable unit of work, and the operations included in the transaction are either done or not.

2. Consistency (consistency). The transaction must be to change the database from one consistency state to another. Consistency is closely related to atomicity.

3. Isolation (Isolation). Execution of one transaction cannot be disturbed by other transactions. That is, the operations inside a transaction and the data used are isolated from other transactions that are concurrently executing, and cannot interfere with each other concurrently.

4. Persistence (durability). Persistence, also known as permanence (permanence), refers to the fact that once a transaction is committed, its changes to the data in the database should be permanent. The next operation or failure should not have any effect on it.

The transaction for Python DB API 2.0 provides two methods of commit or rollback.

Instance parsing

# SQL Delete Record statement sql = "Delete from EMPLOYEE WHERE age > '%d '" "%" try:   # Execute SQL statement   cursor.execute (SQL)   # submit to Database C4/>db.commit () except:   # rollback   db.rollback () when an error occurs

For a database that supports transactions, in Python database programming, when the cursor is established, an invisible database transaction is automatically started.

Commit () method all the update operations of the cursor, the rollback () method rolls back all operations of the current cursor. Each method starts a new transaction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.