Solved

C# Oracle provider ODP.NET : close connection in finaly

Posted on 2009-03-30
4
1,458 Views
Last Modified: 2013-12-17
I write a unique class fro my C# web application to open the connection to a oracle 10 database. I want to use Oracle provider ODP.NET for this.
With ADO.NET It was better to close the connection in the finally. It's still right with ODP.NET and Visual studio 2008 (framework 3.5)? By default, connection pooling is used, I must  close the connection in the finaly?

Thanks for your help.

Angie
0
Comment
Question by:AngieWill
  • 2
  • 2
4 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 24022121
Yes. Regardless of the provider you are using you need to close the connection when you are finished with it. The safest place to do this is in a finally block as it will then get executed regardless of whether or not an exception occurs.
0
 

Author Comment

by:AngieWill
ID: 24026009
I read a note in msdn site:
 Do not call Close or Dispose on a Connection, a DataReader, or any other managed object in the Finalize method of your class. In a finalizer, only release unmanaged resources that your class owns directly. If your class does not own any unmanaged resources, do not include a Finalize method in your class definition.

conn.close() must be called after the block try...catch?
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 24026526
That MSDN note is talking about something slightly different. A finalizer is a destrcutor on an object that is called when the object is cleaned up by the GC. You have no control over when the GC will clear up your object, so therefore no idea when the Finalizer wil be called. So if you have a connection that you try to close in the Finalizer then your connection will be left open until the object is destroyed.

So, as I said earlier, you need to close the connection when you are finished using it, usually in teh "finally" block of a try catch statement.
0
 

Author Closing Comment

by:AngieWill
ID: 31564356
I think that we always must open and close the connection.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Salesforce.com is a cloud-based customer relationship management (CRM) system. In this article, you will learn how to add and map custom lead and contact fields to your Salesforce instance.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question