New method of Oracle insert data and deadlock solution

During the development process, a string of code was discovered, as follows:

<insert id="insertAccInvoicePrintListBatch" parameterClass="list">
		INSERT INTO acc_invoice_print_list (instance_id, invoice_id, charge_name,currency,unit_price,unit_num,amount,invoice_amount,unit_name,specifications)
		SELECT TO_CHAR(SYSDATE,'YYYYMMDD') || seq_charge_id.nextval instance_id, invoice_id, charge_name,currency,unit_price,unit_num,amount,invoice_amount,unit_name,specifications
        FROM (
		<iterate conjunction=" UNION ALL ">
			SELECT #insertList[].invoice_id# invoice_id,
					#insertList[].charge_name# charge_name,
					#insertList[].currency# currency,
					#insertList[].unit_price# unit_price,
					#insertList[].unit_num# unit_num,
					#insertList[].amount# amount,
					#insertList[].invoice_amount# invoice_amount,
					#insertList[].unit_name# unit_name,
					#insertList[].specifications# specifications
			FROM dual
		</iterate>)
</insert>

After research, I found that there are no values ​​after the insert statement in this string of code. Instead, after writing the corresponding field name, a select statement is added at the end, and this select statement returns a result set. If the code can Successful execution means that insert into table_name1(column1, column2, column3) select column1, column2, column3 from table_name2; this form can insert multiple pieces of data, so I verified it myself.

First create a table;

create table test111(
       job_id number(15) not null primary key,
       job_no varchar2(15),
       sale_corp_id number(10)
)

Then use the above format to write the insert statement;

insert into test111
  (job_id, job_no, sale_corp_id)
  select h.job_id, h.job_no, h.sale_corp_id
    from job_head h
   where h.create_date > to_date('2019/09/21', 'yyyy/MM/dd')

Finally inserted successfully.

This operation may be convenient when copying table data, and does not require the use of loops and other operations.

Replenish:

After inserting data, failure to submit in time resulted in a process deadlock. The following is a method to solve the deadlock.

1. Find whether there is a process deadlock;

select username, lockwait, status, machine, program
  from v$session
 where sid in (select session_id from v$locked_object)

Among them, status may have two values, active and inactive. Among them, active refers to being executed (always in the execution state), and inactive refers to waiting for execution.

For example:

--SQL1
delete from test111;
--SQL2
delete from test111;

Execute SQL1 first without submitting, then execute SQL2, and execute the deadlock query SQL. You will find that there are two records, one active and one inactive. Both active and inactive processes are abnormal and should be cleared manually.

2. You can query the specific statement that caused the deadlock through the following statement.

select sql_text
  from v$sql
 where hash_value in
       (select sql_hash_value
          from v$session
         where sid in (select session_id from v$locked_object))

3. Then query the relevant information of the deadlock process through the following SQL

select s.username,
       l.object_id,
       l.session_id,
       s.serial#,
       l.oracle_username,
       l.os_user_name,
       l.process
  from v$locked_object l, v$session s
 where l.session_id = s.sid;

4. Finally kill the process

  --session_id, serial#是上面查询到的结果中的值
 alter system kill session 'session_id,serial#'; 

Process deadlock refers to the original text

Guess you like

Origin blog.csdn.net/qq_42740899/article/details/101453139