Question

I have a requirement to call a function on a remote database (defined by a public database link) within a query. Everything works as expected except that when selecting "Disconnect" from the local database connection in SQL Developer it brings up the dialog:

Connection "localDB" has uncommited changes....

even though the remote function only reads from tables, and the invoking query isn't an update / insert / delete. I'm concerned that using this architecture may cause problems in the future where uncompleted transactions on the localDB diminish resources.

Have tried using the "PRAGMA AUTONOMOUS_TRANSACTION;" instruction in the remote db but to no avail. Any suggestions?

The following demonstrates the behaviour I'm experiencing:

RemoteDB function:

create or replace 
FUNCTION FN_test (inempid in number) RETURN varchar2 is PRAGMA AUTONOMOUS_TRANSACTION;
  sReturn varchar2(20);
BEGIN

sReturn:=to_char(sysdate,'dd-Mon-yyyy HH24:mi');

return sReturn;

END FN_test;

Invoke from localDB:

select  fn_test@RemoteDB.World(123)  from Dual;

Thanks!

Was it helpful?

Solution

Any remote call opens transaction - even this executes just query. Oracle can't predict what exactly you do on remote side and tries to prevent uncommitted distributed transaction. In example below I mere created local database link connecting to native host and just requested data from DUAL table using db link. Oracle has opened the local transaction in respond:

SQL> create database link locallink connect to scott identified by tiger
  2  using '127.0.0.1:1521/test';

Database link created.

SQL> select dbms_transaction.local_transaction_id from dual;

LOCAL_TRANSACTION_ID                                                            
--------------------------------------------------------------------------------


SQL> select * from dual@locallink;

D                                                                               
-                                                                               
X                                                                               

SQL> select dbms_transaction.local_transaction_id from dual;

LOCAL_TRANSACTION_ID                                                            
--------------------------------------------------------------------------------
17.13.10520    

Autonomous transaction is useless because it opens and closes the transaction what is differ from the main one. So you need to commit changes in main transaction if you use remote calls.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top