-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: How to clear Session.Transaction property
PostPosted: Tue Oct 11, 2005 2:41 am 
Newbie

Joined: Tue Oct 11, 2005 2:29 am
Posts: 2
I use following pattern with Nhibenate transaction:

using (ITransaction tr = session.BeginTransaction())
{
// data update
tr.Commit();
}


But commiting/rolling back (disposing) transaction doesn't clear Session.Transaction property. So following out-of-transaction operation are failed because of disposed transaction.

I have considered three workaround:
1) Begin new transaction after this one. (bad solution - i don't need transaction in followed operations)
2) Implement own dummy ITransaction and use it for nontransactional stub.
3) Dispose session and use another one.

I have looked through the sources and didn't found any way to clear SessionImpl.transaction after it was disposed.
I wonder if such behaviour is feature or bad design.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 11, 2005 3:28 am 
A good practice is to keep the session as short as possible. So, open a new session, do job and close the session.
Personnaly, I create a DAL (Data Access Layer) class (called DatabaseManager) which contains all the functions I need to access datas.
So, I call the functions of this class in all my BLL (Business Logic Layer) classes.

[code]
using System;
using System.Collections;

using NHibernate;
using NHibernate.Cfg;


namespace GestionnaireDeCandidatures.DAL
{
/// <summary>
/// Couche d'acc


Top
  
 
 Post subject:
PostPosted: Tue Oct 11, 2005 9:42 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Zetta, your 3) is the way to go.

Grace, I think that your session instance should be a local instance like transaction.

On side note, did you test prepareToGC() to see if it really has any effect? Anyway, I don't think it is really useful.
And you should replace "throw e;" by "throw;"...

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject: dirty hack solution
PostPosted: Wed Oct 12, 2005 6:48 am 
Newbie

Joined: Tue Oct 11, 2005 2:29 am
Posts: 2
I don't like disposing session because it breaks session-per-request strategy in my app.
So i have created dirty hack to solve my need:

Code:
private static FieldInfo transaction = null;
public static void ClearSessionTransaction()
{
   if (transaction == null)
   {
      Assembly nhib = Assembly.Load("NHibernate");
      foreach (Type type in nhib.GetTypes())
         if (type.FullName == "NHibernate.Impl.SessionImpl")
         {
            transaction = type.GetField("transaction", BindingFlags.NonPublic | BindingFlags.Instance);
            break;
         }
   }
   transaction.SetValue(DefaultSession, null);
}



PS I don't recomend this way, but it best for me.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.