I have some code that uses a database link that needs to be closed when it’s done with.
I call DBMS_SESSION.Close_database_link but it gives me the error, ORA-02080, even though the link is no longer in use.
When I tried it in SQL*Plus, it worked great and closed after I ran COMMIT. But in .Net, even if I run commit it doesn’t close.
using (var con = new Oracle.DataAccess.Client.OracleConnection(@"Data Source=firefly8;User Id=USER;Password=PWD;Pooling=false;"))
{
con.Open();
var c = con.CreateCommand();
c.CommandText = "select a from tnoam@link_2";
using (var r = c.ExecuteReader())
{
r.Read();
}
c.Dispose();
var c2 = con.CreateCommand();
c2.CommandText = "begin commit; dbms_session.close_database_link ('link_2');end;";
c2.ExecuteNonQuery();
}
I’m pretty stuck with this one. Please Help
Based on the comment from @Ben I tried the following, it didn’t work as well:
using (var con = new Oracle.DataAccess.Client.OracleConnection(@"Data Source=firefly8;User Id=MILK_NEW;Password=MILK_NEW;Pooling=false;"))
{
con.Open();
var t = con.BeginTransaction();
var c = con.CreateCommand();
c.Transaction = t;
c.CommandText = "select a from tnoam@link_2";
using (var r = c.ExecuteReader())
{
r.Read();
}
t.Commit();
c.Dispose();
var c2 = con.CreateCommand();
c2.CommandText = "begin commit; dbms_session.close_database_link ('link_2');end;";
c2.ExecuteNonQuery();
}
Thanks to @Luke, I got on my way and found the solution.
It seems that by default Oracle caches cursors and keeps them open. And if the cursor is open, you can’t close the database link.
The flag in the connection string that controls the cursor cache is:
You can manually clear the cache by calling the
PurgeStatementCachecache method of the OracleConnection class