Linq Queryable and sessions

15 views
Skip to first unread message

Dan Jasek

unread,
Mar 25, 2011, 10:41:32 PM3/25/11
to Castle Project Users
Looking at the code, AnctiveRecordLinqBase.Queryable never releases
the ISession it creates. This is contrary to Create, Find, etc. which
all release their ISession after they are done with their work.

Does this mean that a Queryable should always be used within a
SessionScope?
If you use it outside of a scope, will you leak ISessions?

Thanks.

Henry Conceição

unread,
Mar 28, 2011, 11:10:38 PM3/28/11
to Castle Project Users
Afaik, we can't have the same behavior (create & release session
inside the method boundaries) of Create, Find, etc because we don't
know when the query will be actually executed. So, the answer for your
question is yes.

Now that you bring a light on this, I think that is better to replace
the CreateSession by a lookup on the ThreadScopeInfo for a
RegisteredScope. What do you think about it?

Dan Jasek

unread,
Mar 29, 2011, 1:06:01 PM3/29/11
to Castle Project Users
I think that would be much safer. This would throw an error if
attempted outside of a scope, right? I would have rather gotten an
exception the first time I tried to use queryable outside of a scope,
instead of stumbling on the issue a few months later.

I went ahead and wrote a slightly customized query provider that
creates/releases the session on execution. Of course it requires
changes to the visibility of some methods in NH. Once I am sure it
works, and I get the changes into NH, and that code filters down to
AR, I'll submit a patch.

Also, BTW - I got my required changes into NH for the ByteCode. So,
next time the NH binaries are updated in AR, I will be able to add
support for lazy loading properties and collections outside of
SessionScopes.

-Dan
> > Thanks.- Hide quoted text -
>
> - Show quoted text -

Nicholas Kilian

unread,
Mar 29, 2011, 3:11:10 PM3/29/11
to castle-pro...@googlegroups.com
> Also, BTW - I got my required changes into NH for the ByteCode. So, next
time the NH binaries are updated in AR, I will be able to add support for
lazy loading properties and collections outside of SessionScopes.

You may have just become my favourite person.

Which NH version are they out in? AR 3.0 RC is NH 3.1.0 ga. Possible we
could get that support out in final AR 3.0?

- Nick

-Dan

--
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to
castle-project-u...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en.

Dan Jasek

unread,
Mar 29, 2011, 3:40:25 PM3/29/11
to Castle Project Users
They are doing quite a bit of updates over there, I don't think it
would be a good idea to update AR midway through their release cycle.

If you are feeling adventurous, and want to compile your own NH and
AR, I would be happy to post a patch.
Grab a version of NHibernate after 3/26, the "Apply NH-2600"
revision. Let me know if you do this, and I will put something
together.

-Dan

On Mar 29, 1:11 pm, Nicholas Kilian <nicholaskil...@googlemail.com>
wrote:
> For more options, visit this group athttp://groups.google.com/group/castle-project-users?hl=en.- Hide quoted text -

Henry Conceição

unread,
Mar 29, 2011, 4:56:38 PM3/29/11
to Castle Project Users
You should get a github account and have your mods published man.
Also, it would ease the review and merge of your changes ;-).
> > For more options, visit this group athttp://groups.google.com/group/castle-project-users?hl=en.-Hide quoted text -

Dan Jasek

unread,
Mar 30, 2011, 2:58:04 PM3/30/11
to Castle Project Users
Good idea.
My mods can be found here: https://github.com/oillio/Castle.ActiveRecord

It uses a slightly modified NHibernate library based off of the 3.1.0
release. The only changes are minor refactorings to make some
properties/methods more visible to external code.

This fork provides two advancements:
The complete implementation of the ActiveRecord ByteCode. This will
provide session management for lazy proxies, collections, and
properties. I believe it handles all lazy scenarios, please let me
know if I missed any.

A custom Linq Query Provider. The provider will manage the session
when a query is executed. Therefore, you can create and execute
queries outside of a Scope and move queries between Scopes without
issue. The query will use whatever session is available when it is
actually executed.

Nick,
For more information on configuring the ByteCode, please see the wiki:
http://docs.castleproject.org/Active%20Record.The-ByteCode.ashx

In addition to setting the ProxyFactory, as described in the wiki, you
will also need to set the CollectionTypeFactory. In your
configuration file, add the following line:
<add key="collectiontype.factory_class"
value="Castle.ActiveRecord.ByteCode.CollectionTypeFactory,
Castle.ActiveRecord"/>

An example config section would look something like this:
<config>
<add key="connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"/>
<add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<add key="connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="connection.connection_string" value="Server=(local)
\sqlexpress;initial catalog=test;Integrated Security=SSPI" />
<add key="proxyfactory.factory_class"
value="Castle.ActiveRecord.ByteCode.ProxyFactoryFactory,
Castle.ActiveRecord"/>
<add key="collectiontype.factory_class"
value="Castle.ActiveRecord.ByteCode.CollectionTypeFactory,
Castle.ActiveRecord"/>
<add key="query.factory_class"
value="NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory, NHibernate"/
>
</config>
> > > For more options, visit this group athttp://groups.google.com/group/castle-project-users?hl=en.-Hidequoted text -
>
> > > - Show quoted text -- Hide quoted text -

Nicholas Kilian

unread,
Mar 31, 2011, 1:11:30 AM3/31/11
to castle-pro...@googlegroups.com
Dan, looks good.

I'm preparing for a demo over the next few days, but at the weekend I'll
spend some time removing the session management on branches of a few of our
applications and trying your changes out.

I must admit I'm pretty excited about the possibilities here. Not having to
worry about session management, passing entities between sessions/forms or
handling the inevitable session failure is very appealing.

Daniel Soto

unread,
Mar 31, 2011, 7:32:05 AM3/31/11
to castle-pro...@googlegroups.com

La

Daniel Soto - Ingeniero de software

El mar 31, 2011 2:14 a.m., "Nicholas Kilian" <nichola...@googlemail.com> escribió:

Dan, looks good.

I'm preparing for a demo over the next few days, but at the weekend I'll
spend some time removing the session management on branches of a few of our
applications and trying your changes out.

I must admit I'm pretty excited about the possibilities here. Not having to
worry about session management, passing entities between sessions/forms or
handling the inevitable session failure is very appealing.


Nick

-----Original Message-----
From: castle-pro...@googlegroups.com
[mailto:castle-project...

Sent: 30 March 2011 08:58 PM
To: Castle Project Users
Subject: Re: Linq Queryable and sessions

Good...

> > > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

--

You received this messag...

Reply all
Reply to author
Forward
0 new messages