pragma autonomous_transaction : restrict_references(<fuctionname>,wnds) and serially_resumable

本文通过创建并使用 Oracle PL/SQL 包演示了如何实现串行可重用性和自治事务特性。首先定义了一个串行可重用包 pkg1 来管理变量状态,并展示其在不同会话中的行为。接着创建了一个名为 pkg2 的包,该包包含一个自治事务函数 fn_auto,用于在查询中执行 DML 操作。

pragma : serially_reusable


SQL> CREATE PACKAGE pkg1 IS

   PRAGMA SERIALLY_REUSABLE;
   num NUMBER := 0;
   PROCEDURE init_pkg_state(n NUMBER);
   PROCEDURE print_pkg_state;
END pkg1;
/  2    3    4    5    6    7


Package created.


SQL> CREATE PACKAGE BODY pkg1 IS
   PRAGMA SERIALLY_REUSABLE;
   PROCEDURE init_pkg_state (n NUMBER) IS
   BEGIN
      pkg1.num := n;
   END;
   PROCEDURE print_pkg_state IS
   BEGIN
      dbms_output.put_line('Num: ' || pkg1.num);
   END;
END pkg1;
/  2    3    4    5    6    7    8    9   10   11   12


Package body created.


SQL>
SQL>
SQL> exec pkg1.init_pkg_state(10);


PL/SQL procedure successfully completed.

SQL> exec pkg1.print_pkg_state;
Num: 0


PL/SQL procedure successfully completed.


pragma :autonomous_transaction

SQL> create table t(ab date);


Table created.


SQL> create or replace package pkg2 is
             function fn_auto return number;
           end pkg2;
/  2    3    4


Package created.


SQL> create or replace package body pkg2 is
            function fn_auto return number
             is
            begin
                insert into t values(sysdate);
                commit;
                return 1;
            end;
         end pkg2;
 /  2    3    4    5    6    7    8    9   10


Package body created.


SQL>  select pkg2.fn_auto() test from dual;
 select pkg2.fn_auto() test from dual
        *
ERROR at line 1:
ORA-14551: cannot perform a DML operation inside a query
ORA-06512: at "QDL.PKG2", line 5


SQL> create or replace package body pkg2 is
function fn_auto return number
is
pragma autonomous_transaction;
begin
insert into t values(systimestamp);
commit;
 return 1;
end;
end pkg2;
/  2    3    4    5    6    7    8    9   10   11


Package body created.


SQL>  select pkg2.fn_auto() test from dual;


      TEST
----------
         1


pragma :restrict_references(fn_auto,wnds)

make the compiler compile the function fn_auto according to WNDS (write no database ) , so after the package boday created, you see the compile error directly.


C:/Users/Administrator/Documents/untitled1.cpp 7 0 [警告] ignoring '#pragma comment ' [-Wunknown-pragmas] C:/Users/Administrator/Documents/untitled1.cpp 0 -1 In function 'void HandleClient(SOCKET)': C:/Program Files/RedPanda-Cpp/mingw64/lib/gcc/x86_64-w64-mingw32/11.5.0/include/c++/bits/locale_facets.h 48 0 In file included from C:/Program Files/RedPanda-Cpp/mingw64/lib/gcc/x86_64-w64-mingw32/11.5.0/include/c++/bits/locale_facets.h C:/Users/Administrator/Documents/untitled1.cpp 35 21 [错误] no matching function for call to 'find(std::vector<long long unsigned int>::iterator, std::vector<long long unsigned int>::iterator, SOCKET&)' C:/Program Files/RedPanda-Cpp/mingw64/lib/gcc/x86_64-w64-mingw32/11.5.0/include/c++/bits/streambuf_iterator.h 421 5 [说明] candidate: 'template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT, std::char_traits<_CharT> > >::__type std::find(std::istreambuf_iterator<_CharT, std::char_traits<_CharT> >, std::istreambuf_iterator<_CharT, std::char_traits<_CharT> >, const _CharT2&)' C:/Program Files/RedPanda-Cpp/mingw64/lib/gcc/x86_64-w64-mingw32/11.5.0/include/c++/bits/streambuf_iterator.h 421 5 [说明] template argument deduction/substitution failed: C:/Users/Administrator/Documents/untitled1.cpp 35 21 [说明] '__gnu_cxx::__normal_iterator<long long unsigned int*, std::vector<long long unsigned int> >' is not derived from 'std::istreambuf_iterator<_CharT, std::char_traits<_CharT> >' C:/Users/Administrator/Documents/untitled1.cpp 7 0 [警告] ignoring '#pragma comment ' [-Wunknown-pragmas] C:/Users/Administrator/Documents/untitled1.cpp 0 -1 In function 'void HandleClient(SOCKET)': C:/Users/Administrator/Documents/untitled1.cpp 35 28 [错误] no matching function for call to 'find(std::vector<long long unsigned int>::iterator, std::vector<long long unsigned int>::iterator, SOCKET&)' C:/Program Files/RedPanda-Cpp/mingw64/lib/gcc/x86_64-w64-mingw32/11.5.0/include/c++/bits/locale_facets.h 48 0 In file included from C:/Program Files/RedPanda-Cpp/mingw64/lib/gcc/x86_64-w64-mingw32/11.5.0/include/c++/bits/locale_facets.h C:/Program Files/RedPanda-Cpp/mingw64/lib/gcc/x86_64-w64-mingw32/11.5.0/include/c++/bits/streambuf_iterator.h 421 5 [说明] candidate: 'template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT, std::char_traits<_CharT> > >::__type std::find(std::istreambuf_iterator<_CharT, std::char_traits<_CharT> >, std::istreambuf_iterator<_CharT, std::char_traits<_CharT> >, const _CharT2&)' C:/Program Files/RedPanda-Cpp/mingw64/lib/gcc/x86_64-w64-mingw32/11.5.0/include/c++/bits/streambuf_iterator.h 421 5 [说明] template argument deduction/substitution failed: C:/Users/Administrator/Documents/untitled1.cpp 35 28 [说明] '__gnu_cxx::__normal_iterator<long long unsigned int*, std::vector<long long unsigned int> >' is not derived from 'std::istreambuf_iterator<_CharT, std::char_traits<_CharT> >'
最新发布
08-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值