Skip to Main Content
  • Questions
  • pragma autonomous_transaction; and database links

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Chuck.

Asked: November 27, 2017 - 6:41 pm UTC

Last updated: December 06, 2017 - 1:08 pm UTC

Version: 12c

Viewed 1000+ times

You Asked

I have a package of functions that return data from a SqlServer database through a link.
Usually the results are just displayed in optional fields on a web page or client program.

They take the form of:
function get_info(ar_key number) 
return varchar2 as
   v_return varchar2(256);
begin
   select "info"
     into v_return
     from "table"@foreignsqldb
    where "key" = ar_key;
   return v_return;
exception
   when no data found
      return '';
end;


These functions used to return from an Oracle database.
But because it's now sql I worry about orphaned transactions locking things over there.
I know pragma autonomous_transaction has a bad rep, but would this be a valid use for it:

function get_info(ar_key number) 
return varchar2 as
   pragma autonomous_transaction;
   v_return varchar2(256);
begin
   select "info"
     into v_return
     from "table"@foreignsqldb
    where "key" = ar_key;
   rollback; 
   return v_return;
exception
   when no data found
      rollback;
      return '';
end;


TIA

and Connor said...

My understanding of read locks in SQL Server is that they'll be released as soon as your query completes.

So on the assumption that your queries are intended to be nice and quick (ie, where key = ... ) then you should not need to be commencing/terminating transactions.


Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thanks.

Chuck Jolley, December 01, 2017 - 1:49 pm UTC

Thanks Connor. The SQL database is a third party black box with no test instance. Not a comfortable thing to write new code against.
Connor McDonald
December 06, 2017 - 1:08 pm UTC

Indeed :-)

More to Explore

Administration

Need more information on Administration? Check out the Administrators guide for the Oracle Database