Skip to content
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

NH-3402 - Refactor ThreadLocalSessionContext #292

Closed
wants to merge 2 commits into from
Closed

NH-3402 - Refactor ThreadLocalSessionContext #292

wants to merge 2 commits into from

Conversation

rjperes
Copy link
Member

@rjperes rjperes commented Aug 13, 2014

NH-3402

@rjperes
Copy link
Member Author

rjperes commented Aug 14, 2014

Something weird is happening, because the code compiles perfectly on my machine

@hazzik
Copy link
Member

hazzik commented Aug 15, 2014

try to compile with ShowBuildMenu and choose "TeamCity" there

@oskarb
Copy link
Member

oskarb commented Aug 17, 2014

d:\BuildAgent-03\work\f20cfe335cc08c35\src\NHibernate.Test\ConnectionTest\ThreadLocalCurrentSessionTest.cs(82,11): error CS0103: The name 'context' does not exist in the current context

There seems to be a test that inherits from ThreadLocalSessionContext which needs to be rewritten.

@rjperes
Copy link
Member Author

rjperes commented Aug 17, 2014

Oskar: you are right, but this test is marked to be ignored...?
I can provide a proper implementation.

@oskarb
Copy link
Member

oskarb commented Aug 18, 2014

Don't know why it was marked ignored. Probably the rewritten test will be simpler. The old ThreadLocalSessionContext seems to worry about creating sessions and then being responsible for closing them, which none of the other ICurrentSessionContext implementations cared about.

I wonder what the impact of this change will be... Could there be code out there that with this change would require changes in many places (everywhere a new thread or threadpool work item is created) to add session open and close?

Since it will not be in 4.0 anymore, a safe and easy way would be to provide the new implementation under a different name and mark the old as deprecated. If we have it in 4.1, this would better follow the semantic versioning principle. I think I prefer this approach - thoughts?

@rjperes
Copy link
Member Author

rjperes commented Aug 18, 2014

Oskar,
I understand you. We can call it something else, it's just that I can't find a more suitable name than ThreadLocalSessionContext, because it's exactly what it is! :-)

@hazzik
Copy link
Member

hazzik commented Nov 18, 2014

@rjperes tests still fail

@hazzik hazzik added this to the 4.1.0 milestone Nov 18, 2014
@rjperes
Copy link
Member Author

rjperes commented Nov 18, 2014

I'm looking at it.

Removed dependency on SharpTestEx.
@hazzik hazzik changed the title NH-3402 NH-3402 - Refactor ThreadLocalSessionContext Dec 2, 2014
@hazzik hazzik modified the milestones: 4.2.0, 4.1.0 Aug 12, 2016
@hazzik hazzik modified the milestones: 4.2.0, 5.0.0 Dec 1, 2016
Copy link
Member

@fredericDelaporte fredericDelaporte left a comment

Choose a reason for hiding this comment

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

The xml doc is obsolete with your change and need an update.

We have to agree on changing the ThreadLocalSessionContext semantic to that point. It was heavily deviating from CurrentSessionContext on one crucial point: trying to get CurrentSession while none are current was causing thread local context to build a session, while the current context throws an exception instead.
Additionally, some "fool" protections were in place for releasing previous session if the user was binding a new one without unbinding the previous one.

Users who were relying on this "on the fly session building" behavior will have their applications fail after upgrade.
Since 5.0 is for doing all breaking changes we deem necessary, why not. It will realign this context with the behavior of all the others, which is a good thing in my opinion.

Otherwise, we would have to leave the previous implementation "as is", preferably marked obsolete, and add a ThreadLocalSessionContext2 (sorry, no idea for a better name).

@@ -20,164 +19,26 @@ namespace NHibernate.Context
/// has been called. If <tt>Close()</tt> is called on a session managed by
/// this class, it will be automatically unbound.
/// <p/>
/// Additionally, the static <see cref="Bind"/> and <see cref="Unbind"/> methods are
/// Additionally, the static <see cref="CurrentSessionContext.Bind"/> and <see cref="CurrentSessionContext.Unbind"/> methods are
/// provided to allow application code to explicitly control opening and
/// closing of these sessions. This, with some from of interception,
/// is the preferred approach. It also allows easy framework integration
/// and one possible approach for implementing long-sessions.
/// <p/>
/// </summary>
Copy link
Member

@fredericDelaporte fredericDelaporte Apr 4, 2017

Choose a reason for hiding this comment

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

With this change, most of this xml comment is obsolete. The previous implementation was generating a session if there was no current session, which this new implementation does not.
The comment should be updated accordingly.

Worst, this comment looks obsolete from the start. The mechanism for auto-unbinding a closed session seems to have never been implemented though documented in this xml doc. Same for the mechanism which was supposed to render the session unusable till a transaction is started.
The Ignore on his test class confirms it: Ignore("Not yet supported. Need AutoClosed feature.(TransactionContext)"). This ThreadLocalCurrentSessionTest test class looks quite crappy: it is like it assumes OpenSession on factory use the session context, it overrides a method pointlessly (called neither by base implementation nor by the test class), ... It should be deleted I think.

using NHibernate;
using NHibernate.Engine;

namespace NHibernate.Context
{
//TODO: refactoring on this class. Maybe using MapBasedSessionContext.
Copy link
Member

Choose a reason for hiding this comment

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

Good move to have ignored and removed this comment, since the MapBasedSessionContext implementation is flawed (thread safety issues, going to Jira that), and unneeded with your implementation.

@fredericDelaporte
Copy link
Member

Another changes may be missing:

  • SessionFactoryImpl.BuildCurrentSessionContext handles short configuration names for built-in session contextes. It would probably be nice to give a short name to the re-implemented ThreadLocalSessionContext.
  • doc/reference/modules/architecture.xml documents build-in session contextes. Now that the ThreadLocalSessionContext would behave as the others, it should be documented here too I think.

@fredericDelaporte
Copy link
Member

Completely forgotten all that when doing #644. If #644 is ok, I think this partially done port from Hibernate should be either completely removed, or finished with its original intended semantic. If we agree on that alternative, then this would mean this #292 PR is to be closed.

@fredericDelaporte
Copy link
Member

With #644, this PR in its current state would just change ThreadLocalSessionContext to a duplicate of ThreadStaticSessionContext with more code. Closing.

@hazzik hazzik removed this from the 5.0 milestone Aug 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants