Skip to content

NH-3606 - A method to create a child stateless session #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/NHibernate.Test/Linq/StatelessSessionQueringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,77 @@ namespace NHibernate.Test.Linq
{
public class StatelessSessionQueringTest : LinqTestCase
{
[Test]
public void CanQueryChildStatelessSession()
{
//NH-3606
using (var session = this.OpenSession())
{
Assert.AreEqual(0, session.GetSessionImplementation().PersistenceContext.EntityEntries.Count);

using (var statelessSession = session.GetStatelessSession())
{
var results = statelessSession.Query<Customer>().ToList();

Assert.IsNotEmpty(results);
}

Assert.AreEqual(0, session.GetSessionImplementation().PersistenceContext.EntityEntries.Count);
}
}

[Test]
public void CanCreateStatelessSessions()
{
//NH-3606
using (var session = OpenSession())
using (session.BeginTransaction())
{
Assert.IsTrue(session.Transaction.IsActive);

using (var statelessSession = session.GetStatelessSession())
{
Assert.IsTrue(statelessSession.Transaction.IsActive);

statelessSession.Transaction.Rollback();
}

Assert.IsFalse(session.Transaction.IsActive);
}
}

[Test]
public void CanApplyAsStatelessExtensionMethod()
{
//NH-3606
using (var session = OpenSession())
{
Assert.AreEqual(0, session.GetSessionImplementation().PersistenceContext.EntityEntries.Count);

var results = session.Query<Customer>().AsStateless().ToList();

Assert.IsNotEmpty(results);

Assert.AreEqual(0, session.GetSessionImplementation().PersistenceContext.EntityEntries.Count);
}
}

[Test]
public void ExplicitlyFetchedLazyMembersAreNotCached()
{
//NH-3606
using (var session = OpenSession())
{
Assert.AreEqual(0, session.GetSessionImplementation().PersistenceContext.EntityEntries.Count);

var results = session.Query<Customer>().AsStateless().Fetch(x => x.Orders).ToList();

Assert.IsNotEmpty(results);

Assert.AreEqual(0, session.GetSessionImplementation().PersistenceContext.EntityEntries.Count);
}
}

[Test]
public void WhenQueryThroughStatelessSessionThenDoesNotThrows()
{
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/AdoNet/ConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface Callback
private bool ownConnection;

[NonSerialized]
private ITransaction transaction;
internal ITransaction transaction;

[NonSerialized]
private IBatcher batcher;
Expand Down
33 changes: 33 additions & 0 deletions src/NHibernate/Impl/SessionImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public sealed class SessionImpl : AbstractSessionImpl, IEventSource, ISerializab
[NonSerialized]
ISession _childSession;

//NH-3606
[NonSerialized]
private IStatelessSession childStatelessSession;

[NonSerialized]
private readonly bool flushBeforeCompletionEnabled;
[NonSerialized]
Expand Down Expand Up @@ -359,12 +363,20 @@ public DbConnection Close()
{
try
{
//NH-3606
if (childStatelessSession != null)
{
childStatelessSession.Close();
childStatelessSession = null;
}

if (_childSession != null)
{
if (_childSession.IsOpen)
{
_childSession.Close();
}
_childSession = null;
}
}
catch
Expand Down Expand Up @@ -2210,6 +2222,27 @@ public ISessionImplementor GetSessionImplementation()
return this;
}

//NH-3606
public IStatelessSession GetStatelessSession()
{
using (new SessionIdLoggingContext(SessionId))
{
if (rootSession != null)
{
return rootSession.GetStatelessSession();
}

if (childStatelessSession == null)
{
childStatelessSession = new StatelessSessionImpl(Connection, SessionFactory as SessionFactoryImpl);

(childStatelessSession as StatelessSessionImpl).ConnectionManager.transaction = Transaction;
}

return childStatelessSession;
}
}

public ISession GetChildSession()
{
using (new SessionIdLoggingContext(SessionId))
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Linq/DefaultQueryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public DefaultQueryProvider(ISessionImplementor session)
_session = new WeakReference(session, true);
}

protected virtual ISessionImplementor Session
protected internal virtual ISessionImplementor Session
{
get { return _session.Target as ISessionImplementor; }
}
Expand Down
8 changes: 8 additions & 0 deletions src/NHibernate/Linq/LinqExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ namespace NHibernate.Linq
{
public static class LinqExtensionMethods
{
public static IQueryable<T> AsStateless<T>(this IQueryable<T> query)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this extensions. It needs to be implemented as a separate feature.

{
var session = (query.Provider as DefaultQueryProvider).Session;
var statelessSession = (session as SessionImpl).GetStatelessSession();

return new DefaultQueryProvider(statelessSession.GetSessionImplementation()).CreateQuery<T>(query.Expression);
}

public static IQueryable<T> Query<T>(this ISession session)
{
return new NhQueryable<T>(session.GetSessionImplementation());
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/NHibernate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@
<Compile Include="Proxy\DynProxyTypeValidator.cs" />
<Compile Include="QueryException.cs" />
<Compile Include="ReplicationMode.cs" />
<Compile Include="SessionExtensions.cs" />
<Compile Include="SqlCommand\Alias.cs" />
<Compile Include="SqlCommand\ANSICaseFragment.cs" />
<Compile Include="SqlCommand\ANSIJoinFragment.cs" />
Expand Down
16 changes: 16 additions & 0 deletions src/NHibernate/SessionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Impl;

namespace NHibernate
{
public static class SessionExtensions
{
public static IStatelessSession GetStatelessSession(this ISession session)
{
return (session.GetSessionImplementation() as SessionImpl).GetStatelessSession();
}
}
}