Home Database Mysql Tutorial 关于自治事务和锁PRAGMAAUTONOMOUS_TRANSACTION&LOCK

关于自治事务和锁PRAGMAAUTONOMOUS_TRANSACTION&LOCK

Jun 07, 2016 pm 04:10 PM
affairs about

之前遇到的一个问题, 可以稳定重现. Oracle 的 INV 提供了一个接口 inv_lot_api_pub.auto_gen_lot() , 用来自动为 item 产生 lot number, 调用一次这个接口, lot number 就会自动 + 1; 看里头的代码, 是先从 MTL_SYSTEM_ITEMS 这个表里面取出一个字段 start_

之前遇到的一个问题, 可以稳定重现.

Oracle 的 INV 提供了一个接口 inv_lot_api_pub.auto_gen_lot() , 用来自动为 item 产生 lot number, 调用一次这个接口, lot number 就会自动 + 1; 看里头的代码, 是先从 MTL_SYSTEM_ITEMS 这个表里面取出一个字段 start_auto_lot_number, 比方说取出来的数值是1000, 那么就会把 1000 当做当前的 lot number, 接着会调用一个 procedure, 去更新这个字段, 变成 1001. 这个procedure 名字叫做 update_msi(). 最后去验证 1000 这个 lot number 是不是已经存在了. 如果存在, 那么会重新取出 1001 当做当前 lot number, 并且验证是不是存在, 直到找到没有重复的lot number, 就把它返回给调用 api 的地方.

那么这段代码会有什么问题呢? 我们假设一个场景, 有两个 session 同时调用这个 api, 那么在从 MSI 取数据的阶段, 有可能两个 session 会取出来相同的lot number. 嗯, 这个是有可能的. 如果这两个 session 是在为同一个 item 产生 lot number 就还好, 但是如果是不同的item, 就会为不同的两个 item 产生相同的 lot number. 这样数据就错了.

解决的办法就是通过锁来锁住被选择的 lot number, 并且在更新成功之后释放锁.

锁住选择的数据的方法是使用 for update 语句

 

     SELECT auto_lot_alpha_prefix,
            start_auto_lot_number
       INTO l_lot_prefix,
            l_lot_suffix
       FROM mtl_system_items
      WHERE organization_id = p_org_id
        AND inventory_item_id = p_inventory_item_id for update
Copy after login

一旦有数据被选中, 那么同样使用这个 SQL 的session 将不能获得这些数据, 这就保证了无法产生相同的lot number. 当成功更新 lot number 之后, 就可以用 commit 释放锁了.

 

那么问题来了, 如果我们使用 commit 提交这个事务, 将会把所有的事务处理都提交, 那么如果之后出现错误回滚了, 将会有一部分数据提交, 另一部分数据回滚了. 解决的办法是使用自治事务. 从 select for update 语句开始, 到update MSI 并 commit 为止, 我们定义一个 procedure 并声明这是一个 PRAGMA AUTONOMOUS_TRANSACTION. 这样提交事务的时候就只会提交这个自治事务所做的更改, 并且释放锁给其他语句使用. 在自治事务之外的数据更新操作都不会受到影响.

这样通过锁和声明自治事务, 就可以保证不会在两个 session 里面获取到相同的 lot number 了;

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)

Lock wait timeout exceeded; try restarting transaction - How to solve MySQL error: transaction wait timeout Lock wait timeout exceeded; try restarting transaction - How to solve MySQL error: transaction wait timeout Oct 05, 2023 am 08:46 AM

Lockwaittimeoutexceeded;tryrestartingtransaction - How to solve the MySQL error: transaction wait timeout. When using the MySQL database, you may sometimes encounter a common error: Lockwaittimeoutexceeded;tryrestartingtransaction. This error indicates that the transaction wait timeout. This error usually occurs when

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

PHP PDO Tutorial: An Advanced Guide from Basics to Mastery PHP PDO Tutorial: An Advanced Guide from Basics to Mastery Feb 19, 2024 pm 06:30 PM

1. Introduction to PDO PDO is an extension library of PHP, which provides an object-oriented way to operate the database. PDO supports a variety of databases, including Mysql, postgresql, oracle, SQLServer, etc. PDO enables developers to use a unified API to operate different databases, which allows developers to easily switch between different databases. 2. PDO connects to the database. To use PDO to connect to the database, you first need to create a PDO object. The constructor of the PDO object receives three parameters: database type, host name, database username and password. For example, the following code creates an object that connects to a mysql database: $dsn="mysq

The principles and application scenarios of MySQL transactions The principles and application scenarios of MySQL transactions Mar 02, 2024 am 09:51 AM

The principle and application scenarios of MySQL transactions In the database system, a transaction is a set of SQL operations. These operations are either all executed successfully or all fail and are rolled back. As a commonly used relational database management system, MySQL supports transaction characteristics and can ensure that the data in the database is consistent, isolated, durable and atomic. This article will start with the basic principles of MySQL transactions, introduce its application scenarios, and provide specific code examples for readers' reference. The principle of MySQL transactions: My

Analysis of solutions to transaction management problems encountered in MongoDB technology development Analysis of solutions to transaction management problems encountered in MongoDB technology development Oct 08, 2023 am 08:15 AM

Analysis of solutions to transaction management problems encountered in MongoDB technology development As modern applications become more and more complex and large, the transaction processing requirements for data are also getting higher and higher. As a popular NoSQL database, MongoDB has excellent performance and scalability in data management. However, MongoDB is relatively weak in data consistency and transaction management, posing challenges to developers. In this article, we will explore the transaction management issues encountered in MongoDB development and propose some solutions.

How does Java database connection handle transactions and concurrency? How does Java database connection handle transactions and concurrency? Apr 16, 2024 am 11:42 AM

Transactions ensure database data integrity, including atomicity, consistency, isolation, and durability. JDBC uses the Connection interface to provide transaction control (setAutoCommit, commit, rollback). Concurrency control mechanisms coordinate concurrent operations, using locks or optimistic/pessimistic concurrency control to achieve transaction isolation to prevent data inconsistencies.

Master the power of PHP PDO: advanced queries and updates Master the power of PHP PDO: advanced queries and updates Feb 20, 2024 am 08:24 AM

The PHP Data Objects (PDO) extension provides efficient and object-oriented interaction with database servers. Its advanced query and update capabilities enable developers to perform complex database operations, improving performance and code maintainability. This article will delve into the advanced query and update functions of PDO and guide you to master its powerful functions. Advanced queries: Using placeholders and bound parameters Placeholders and bound parameters are important tools for improving query performance and security. Placeholders use question marks (?) to represent replaceable parameters in the query, while bind parameters allow you to specify the data type and value of each parameter. By using these methods, you can avoid SQL injection attacks and improve performance because the database engine can optimize queries ahead of time. //Use placeholder $stmt=$

Detailed explanation of Java EJB architecture to build a stable and scalable system Detailed explanation of Java EJB architecture to build a stable and scalable system Feb 21, 2024 pm 01:13 PM

What is EJB? EJB is a Java Platform, Enterprise Edition (JavaEE) specification that defines a set of components for building server-side enterprise-class Java applications. EJB components encapsulate business logic and provide a set of services for handling transactions, concurrency, security, and other enterprise-level concerns. EJB Architecture EJB architecture includes the following major components: Enterprise Bean: This is the basic building block of EJB components, which encapsulates business logic and related data. EnterpriseBeans can be stateless (also called session beans) or stateful (also called entity beans). Session context: The session context provides information about the current client interaction, such as session ID and client

See all articles