---
title: "Oracle自治事务的介绍(Autonomous Transactions)PRAGMA AUTONOMOUS_TRANSACTION"
author: "libin123"
date: "2010-05-26T11:11:11Z"
tags: []
categories: ["数据库"]
slug: "oracle-autonomous-transactions-pragma-autonomous-transaction"
draft: false
lastmod: "2010-05-26T20:36:32Z"
id: 675717
url: "https://dbua.iteye.com/blog/675717"
---

在你的一个事务（外层事务）中可以定义一个或几个自治事务。自治事务可以独立commit,不对外层事务产生影响，同样外层事务的 rollback 也对自治事务没有影响。通常可以考虑将自治事务定义成一个过程，在外层的事务中调用。  
现在举个例子吧：  
 create  or replace procedure p\_record(p\_sin in varchar2) as   
    pragma autonomous\_transaction;  
    begin  
    insert into t\_log values (p\_sin,sysdate);  
    commit;  
    end;  
因为这个procedure的作用是写log，无论操作成功与否（即无名块是否执行rollback），log当然是都应该记录的啦。所以要把它写成自治事务，让这个procedure的提交独立于调用它的无名块，在rollback时也不会影响到log的记录。