Home Database Oracle oracle view lock and session execution sql (summary sharing)

oracle view lock and session execution sql (summary sharing)

Jul 01, 2022 pm 12:23 PM
oracle

This article brings you relevant knowledge about Oracle, which mainly introduces the issues related to checking locks and sql in session execution. Let’s take a look at it together. I hope it will be helpful to everyone. .

oracle view lock and session execution sql (summary sharing)

Recommended tutorial: "Oracle Video Tutorial"

The database environment for the test data in this article: Oracle 11g

Why is it said that it is the sql during session execution? It seems that the sql execution record of a certain session cannot be obtained. I have also read a lot of blog posts. Many people on the Internet say that the sql_id is associated with the view v$active_session_history and v$sqlarea. You can query the sql execution record of a certain session. After practice, it is found that it does not work (tried through the table dba_hist_active_sess_history also does not work) , the sql_id of some sql is not recorded at all in v$active_session_history, I Try to modify the parameters: control_management_pack_access, and found that I do not have permission. I checked it and found that the parameter value is normal and the parameter database is open. Refer to the blog post: Oracle V$ACTIVE_SESSION_HISTORY Query No Data - wazz_s - Blog Park

I can query the execution record of SQL through the v$sqlarea view, but I cannot find the sessionid that executed the SQL. It would be great if I had this sessionid, so that I can find out who executed the SQL. .

If I want to query the sql that caused the table to be locked, most of the blog posts on the Internet teach this. Get the corresponding prev_sql_addr field value by querying the view v$session, which is recorded as Value A, and then use value A as the query condition value of the view v$sqlarea field address, and then the corresponding SQL record can be queried. As a practice test, you can find the SQL that finds the lock table, but in most cases you cannot get it in a normal production environment. Why? Please see the introduction below.

This article uses an exploratory approach to study. In order to ensure the accuracy of the data, I opened three database sessions, recorded as session1, session2, and session3 respectively. The specific steps are as follows:

1 Create a new test table and test data in session session1

--新建测试表
create table zxy_table(zxy_id int,zxy_name varchar2(20));
--插入数据
insert into zxy_table(zxy_id,zxy_name) values(1,'zxy1');
insert into zxy_table(zxy_id,zxy_name) values(2,'zxy2');
insert into zxy_table(zxy_id,zxy_name) values(3,'zxy3');
insert into zxy_table(zxy_id,zxy_name) values(4,'zxy4');
commit;
Copy after login

2 View the session ID of session1

 select userenv('sid') from dual;
Copy after login

You can see that the session ID is 2546

3 In session1, lock a row of the table zxy_table through select for update, as follows:

 select * from zxy_table where zxy_name='zxy1' for update;
Copy after login

4 In session2, query that the session ID is 2189:

Then update the row in the table zxy_table with the value zxy_name='zxy1' in session2, as follows:

update zxy_table set zxy_name='zxy1_modify' where zxy_name='zxy1';
Copy after login

Then we saw that the sql has been blocked, as shown below:

5 Then we came to session 3 to view the lock table situation

First check the table v$locked_object

select * from v$locked_object;
Copy after login

You can see that the session id that caused the lock table is 2546, which is the previous session1, and the object_id is 110154. Of course, In the generation environment, you will definitely see more than one record. You have to execute it several times. After executing it n times, you can still see the record, which proves that this record is the record of the lock table.

Pass object_id :110154 Query the dba4_objects table to query the detailed lock table information

select object_name as 被锁的表名称,obj.* from dba_objects obj where object_id='110154';
Copy after login

## Query the view v$session

select 
       s.prev_sql_addr,
       module as 客户端工具名称,
       s.user# as 数据库账号名,
       s.osuser as 连接数据库客户端对应的window账号名称,
       s.machine as 连接数据库客户端对应的计算机名称,
       s.* 
from v$session s where sid='2546';
Copy after login
Copy after login
# through sessionid: 2546

## Get the value of prev_sql_addr: 000000012E045E28, and then query the view v$sqlarea through the obtained value

select * from v$sqlarea where address='000000012E045E28';
Copy after login

As you can see from the picture above, the result There is a statement to lock the table, but many blog posts are finished at this step. Is this query really reliable? The answer is unreliable. You can go back to session1 and execute a sql at will, as follows:

 select * from zxy_table;
Copy after login

Then you can go to session3 to execute

select 
       s.prev_sql_addr,
       module as 客户端工具名称,
       s.user# as 数据库账号名,
       s.osuser as 连接数据库客户端对应的window账号名称,
       s.machine as 连接数据库客户端对应的计算机名称,
       s.* 
from v$session s where sid='2546';
Copy after login
Copy after login

 再看看prev_sql_addr是不是变了,从000000012E045E28变为了00000001FB03CEC0,再通过00000001FB03CEC0查询视图v$sqlarea

select * from v$sqlarea where address='00000001FB03CEC0';
Copy after login

得到的sql_text是select * from zxy_table,你敢说这条sql导致了锁表吗?所有只能说是session1当前执行的sql,而且你很难保证session1执行完锁表的sql: select * from zxy_table where zxy_name='zxy1' for update且在提交前不再执行别的sql,这就是前文提出的问题的答案。

推荐教程:《Oracle视频教程

The above is the detailed content of oracle view lock and session execution sql (summary sharing). For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

What to do if the oracle log is full What to do if the oracle log is full Apr 12, 2025 am 06:09 AM

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

How to export oracle view How to export oracle view Apr 12, 2025 am 06:15 AM

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

See all articles