简体   繁体   English

ODP.net在连接关闭时是否关闭引用游标?

[英]Does ODP.net close a ref cursor when the connection closes?

I haven't been able to find this explicitly stated anywhere yet, but a bunch of examples I've found online follow what I've been doing. 我还没有在任何地方找到明确说明的内容,但我在网上找到的一些例子都遵循我一直在做的事情。

I have a C# class which uses ODP.net to connect to an Oracle DB and run a procedure that's in a package. 我有一个C#类,它使用ODP.net连接到Oracle DB并运行一个包中的过程。

My package has stored procedures which take a ref cursor output parameter. 我的包有存储过程,它接受ref游标输出参数。 All the procedure does is open up the cursor for a particular select statement. 所有过程都打开了特定select语句的游标。

If I execute this procedure directly on the oracle db, then eventually I'll hit a max number of open cursors error. 如果我直接在oracle db上执行此过程,那么最终我将达到最大数量的打开游标错误。

So I was wondering if ODP.net does indeed close this cursor that was opened in my procedure? 所以我想知道ODP.net是否确实关闭了我在我的程序中打开的光标?

I'm using the OracleDataApaper.Fill(DataSet) method. 我正在使用OracleDataApaper.Fill(DataSet)方法。

eg. 例如。

DataSet ds = new DataSet();
OracleConnection conn = new OracleConnection(this.connStr);
OracleCommand com = new OracleCommand("MYPKG.MYQUERY", conn);
OracleDataAdapter adapter = new OracleDataAdapter(com);
conn.Open();
com.Parameters.Add("searchParam", OracleDbType.Varchar2).Value = "myName";
com.Parameters.Add("outCursor", OracleDbType.RefCursor, ParameterDirection.Output);
com.CommandType = CommandType.StoredProcedure;

adapter.Fill(ds);
conn.Close();




PROCEDURE GETALLEMAILS(searchParamIN VARCHAR2, outCursor OUT sys_refcursor) AS
  BEGIN
    open outCursor
      select 
        EAEMAL as Email
      from 
        EmailTable
      where 
        EmailName = searchParam;  
  END GETALLEMAILS;

I'm just afraid of leaving open cursors behind on the DB is all. 我只是害怕在数据库上留下开放的游标。 If anyone can provide links to official documentation, that'd be great! 如果有人可以提供官方文档的链接,那就太好了!


Updates: 更新:

Thanks for the input. 感谢您的投入。 I was calling 我在打电话

com.Dispose();
conn.Close();
conn.Dispose();

but left them out of my example. 但他们离开了我的榜样。

I found this forum post, which states that the OracleDataAdapter.Fill(Dataset) method does release the ref cursor after the Fill() method has been executed. 我找到了这篇论坛帖子,其中说明OracleDataAdapter.Fill(Dataset)方法在执行Fill()方法后释放了引用游标。
http://www.frontoracle.com/oracle-archive/140/386140-close-ref-cursor.html http://www.frontoracle.com/oracle-archive/140/386140-close-ref-cursor.html

I wish the Oracle documentation was more explicit in describing this process though. 我希望Oracle文档在描述此过程时更加明确。

ODP.NET requires you to clean up things. ODP.NET要求你清理东西。 So you: 那么你:

  • have to dispose OracleParameter instances, as they contain unmanaged resources (!) and Odp.net doesn't do this 必须处置OracleParameter实例,因为它们包含非托管资源(!)而Odp.net不会这样做
  • have to dispose OracleCommand objects, as they too contain unmanaged resources and closing a connection doesn't close these 必须处置OracleCommand对象,因为它们也包含非托管资源,关闭连接不会关闭它们
  • open cursors can't live without an open connection, though in odp.net nothing gets cleaned up after a connection closes (or gets disposed), so you have to clean up these too (and before the connection closes of course). 打开游标无法在没有打开连接的情况下生存,但在odp.net中,在连接关闭(或被处理)后没有任何东西被清除,所以你也必须清理它们(当然连接关闭之前)。

Iow: clean up what you create. 喵:清理你创造的东西。

It can be the OracleDataAdapter already does this for you, but that's unclear (and the odp.net docs don't say this, so you've to check the (unreadable) code with reflector to make sure. Though rule of thumb with odp.net: to avoid memory leaks, always call dispose, on everything in the order: parameter, cursor, command, transaction, connection. 可能是OracleDataAdapter已经为你做了这个,但是目前还不清楚(并且odp.net文档没有这么说,所以你要用反射器检查(不可读)代码以确保。虽然经验法则有odp .net:为了避免内存泄漏,总是在命令的所有内容上调用dispose:参数,游标,命令,事务,连接。

I'm not sure if you've stumbled onto this article, and it doesn't apply directly to your question, but it illustrates something I learned when working with ODP.Net: when in doubt, always close (connections) and dispose. 我不确定你是否偶然发现了这篇文章,它并不直接适用于你的问题,但它说明了我在使用ODP.Net时学到的东西:当有疑问时,总是关闭(连接)和处理。 Every method I write that uses an instance of ODP connections, commands, and/or cursors has a finally clause disposing everything. 我编写的每个使用ODP连接,命令和/或游标实例的方法都有一个finally子句来处理所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM