calling Session.CreateQuery multiple times is causing a memory leak.

Is there a way to manually displse of the query created by the call to Session.CreateQuery after I have finished with it?

Thanks in advance

asked May 31 '11 at 06:00

Tedder77's gravatar image


edited May 31 '11 at 06:48

Alex%20Ustinov's gravatar image

Alex Ustinov

This is in DataObjects.Net 3.x ?

(May 31 '11 at 06:07) Peter Ĺ ulek Peter%20%C5%A0ulek's gravatar image

this is version

(May 31 '11 at 06:13) Tedder77 Tedder77's gravatar image

One Answer:

Yes it is.

Query type is inherited from QueryBase which in turn implements IDisposable interface. I guess you were confused because it is explicit implementation and as a consequence Dispose method is not publicly visible.

So you have two options

1) (Preferable) You can use using keyword:

using (var query = Session.CreateQuery("Select ...")) {
  // Your code here

2) Or you can cast query instance to IDisposable interface to be able to call Dispose then:


answered May 31 '11 at 06:45

Alex%20Ustinov's gravatar image

Alex Ustinov

edited May 31 '11 at 06:54

Dmitri%20Maximov's gravatar image

Dmitri Maximov

Thanks - I'll give that a try!

(May 31 '11 at 07:10) Tedder77 Tedder77's gravatar image

When looking at the code the IDbCommand instance is disposed, what is the site-effect of not disposing? open connections?

(May 31 '11 at 08:33) Marco Marco's gravatar image

No, not connections. Connections are managed by Session instances. So be sure you disposing Sessions as well.

Side effects of not disposing IDbCommand instances are highly depend on certain db provider. In some cases these effects are harmful in other ones are not (SqlCommand is safe in this respect). But in all cases disposing brings positive performance and memory consumption impact because of skipping finalization (See this discussion for more details).

(May 31 '11 at 14:54) Alex Ustinov Alex%20Ustinov's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA