When i am inserting into DB everything works, but when i update i get error of "Illegal attempt to associate a collection with two open sessions". I use transaction, but if i do not have for example userDao.CommitChanges(); nothing is stored to DB, but if i use userDao.CommitChanges(); then i get previous error. I search in forum, google try many things but no one works. Problem is that in VS 2008 update works, in IIS i get this problem. Any ideas. Thx
Fluent NHibernate version: 1:0 RTM
Code for updateCode:
user.Name = txtName.Text;
user.Surname = txtSurname.Text;
user.HealthCardId = txtHealthCardId.Text;
user.Address = address;
userDao.Update(user);
userDao.CommitChanges();
Code:
public T Update(T entity)
{
NHibernateSession.Update(entity);
return entity;
}
Name and version of the database you are using:MSSQL 2008
MappingCode:
public UsersMap()
{
Id(x => x.UserId);
Map(x => x.AspNetUserGuid);
Map(x => x.HealthCardId)
.Not.Nullable()
.Length(15);
Map(x => x.Name)
.Not.Nullable()
.Length(50);
Map(x => x.Surname)
.Not.Nullable()
.Length(80);
Map(x => x.Email)
.Not.Nullable()
.Length(80);
References(x => x.Address)
.Column(TableNames.AddresId)
.LazyLoad()
.Cascade.SaveUpdate();
HasManyToMany(x => x.HealthCenters)
.AsBag()
.Table(TableNames.HealthCenterEmployeesTabel)
.ParentKeyColumn(TableNames.UserId)
.ChildKeyColumn(TableNames.HealthCenterId)
.LazyLoad()
.Cascade.SaveUpdate();
HasMany(x => x.Clinics)
.KeyColumn(TableNames.UserId)
//.Inverse()
.LazyLoad()
.AsBag();
HasMany(x => x.PersonalDoctors)
.KeyColumn(TableNames.UserId)
//.Inverse()
.LazyLoad()
.AsBag();
HasMany(x => x.Orders)
.KeyColumn(TableNames.UserId)
//.Inverse()
.LazyLoad()
.AsBag();
Version(x => x.Timestamp);
}
Full stack trace of any exception that occurs: Quote:
Server Error in '/' Application.
Illegal attempt to associate a collection with two open sessions
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: NHibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[HibernateException: Illegal attempt to associate a collection with two open sessions]
NHibernate.Collection.AbstractPersistentCollection.SetCurrentSession(ISessionImplementor session) +237
NHibernate.Event.Default.OnUpdateVisitor.ProcessCollection(Object collection, CollectionType type) +184
NHibernate.Event.Default.AbstractVisitor.ProcessValue(Int32 i, Object[] values, IType[] types) +46
NHibernate.Event.Default.AbstractVisitor.ProcessEntityPropertyValues(Object[] values, IType[] types) +74
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformUpdate(SaveOrUpdateEvent event, Object entity, IEntityPersister persister) +432
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event) +99
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event) +333
NHibernate.Impl.SessionImpl.FireSaveOrUpdate(SaveOrUpdateEvent event) +162
NHibernate.Impl.SessionImpl.SaveOrUpdate(String entityName, Object obj) +151
NHibernate.Engine.SaveUpdateCascadingAction.Cascade(IEventSource session, Object child, String entityName, Object anything, Boolean isCascadeDeleteEnabled) +139
NHibernate.Engine.Cascade.CascadeOn(IEntityPersister persister, Object parent, Object anything) +578
NHibernate.Event.Default.AbstractFlushingEventListener.CascadeOnFlush(IEventSource session, IEntityPersister persister, Object key, Object anything) +145
NHibernate.Event.Default.AbstractFlushingEventListener.PrepareEntityFlushes(IEventSource session) +387
NHibernate.Event.Default.AbstractFlushingEventListener.FlushEverythingToExecutions(FlushEvent event) +292
NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) +85
NHibernate.Impl.SessionImpl.Flush() +275
ProjectBase.Data.AbstractNHibernateDao`2.CommitChanges() in D:\PROJEKTI\eMedicine\ProjectBase.Data\AbstractNHibernateDao.cs:120
EMedicine.UserControls.UsersUC.btnAdd_Update(Object sender, EventArgs e) in D:\PROJEKTI\eMedicine\eMedicine\UserControls\UsersUC.ascx.cs:390
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981
My NHibernateSessionManager codeCode:
using System;
using System.Collections;
using System.IO;
using System.Runtime.Remoting.Messaging;
using System.Web;
using NHibernate;
using NHibernate.Cache;
using NHibernate.Cfg;
using ProjectBase.Utils;
using FluentNHibernate;
using System.Reflection;
namespace ProjectBase.Data
{
/// <summary>
/// Handles creation and management of sessions and transactions. It is a singleton because
/// building the initial session factory is very expensive. Inspiration for this class came
/// from Chapter 8 of Hibernate in Action by Bauer and King. Although it is a sealed singleton
/// you can use TypeMock (http://www.typemock.com) for more flexible testing.
/// </summary>
public sealed class NHibernateSessionManager
{
#region Thread-safe, lazy Singleton
/// <summary>
/// This is a thread-safe, lazy singleton. See http://www.yoda.arachsys.com/csharp/singleton.html
/// for more details about its implementation.
/// </summary>
public static NHibernateSessionManager Instance {
get {
return Nested.NHibernateSessionManager;
}
}
/// <summary>
/// Private constructor to enforce singleton
/// </summary>
private NHibernateSessionManager() { }
/// <summary>
/// Assists with ensuring thread-safe, lazy singleton
/// </summary>
private class Nested
{
static Nested() { }
internal static readonly NHibernateSessionManager NHibernateSessionManager =
new NHibernateSessionManager();
}
#endregion
/// <summary>
/// This method attempts to find a session factory stored in <see cref="sessionFactories" />
/// via its name; if it can't be found it creates a new one and adds it the hashtable.
/// </summary>
/// <param name="sessionFactoryConfigPath">Path location of the factory config</param>
private ISessionFactory GetSessionFactoryFor(string sessionFactoryConfigPath) {
Check.Require(!string.IsNullOrEmpty(sessionFactoryConfigPath),
"sessionFactoryConfigPath may not be null nor empty");
// Attempt to retrieve a stored SessionFactory from the hashtable.
ISessionFactory sessionFactory = (ISessionFactory) sessionFactories[sessionFactoryConfigPath];
// Failed to find a matching SessionFactory so make a new one.
if (sessionFactory == null) {
Check.Require(File.Exists(sessionFactoryConfigPath),
"The config file at '" + sessionFactoryConfigPath + "' could not be found");
Configuration cfg = new Configuration();
cfg.Configure(sessionFactoryConfigPath);
/*MINE*/
var persistenceModel = new PersistenceModel();
persistenceModel.AddMappingsFromAssembly(Assembly.Load("EMedicine.Core"));
persistenceModel.Configure(cfg);
/*END_OF_MINE*/
// Now that we have our Configuration object, create a new SessionFactory
sessionFactory = cfg.BuildSessionFactory();
if (sessionFactory == null) {
throw new InvalidOperationException("cfg.BuildSessionFactory() returned null.");
}
if (sessionFactoryConfigPath != null) sessionFactories.Add(sessionFactoryConfigPath, sessionFactory);
}
return sessionFactory;
}
/// <summary>
/// Allows you to register an interceptor on a new session. This may not be called if there is already
/// an open session attached to the HttpContext. If you have an interceptor to be used, modify
/// the HttpModule to call this before calling BeginTransaction().
/// </summary>
public void RegisterInterceptorOn(string sessionFactoryConfigPath, IInterceptor interceptor) {
ISession session = (ISession)ContextSessions[sessionFactoryConfigPath];
if (session != null && session.IsOpen) {
throw new CacheException("You cannot register an interceptor once a session has already been opened");
}
GetSessionFrom(sessionFactoryConfigPath, interceptor);
}
public ISession GetSessionFrom(string sessionFactoryConfigPath) {
return GetSessionFrom(sessionFactoryConfigPath, null);
}
/// <summary>
/// Gets a session with or without an interceptor. This method is not called directly; instead,
/// it gets invoked from other public methods.
/// </summary>
private ISession GetSessionFrom(string sessionFactoryConfigPath, IInterceptor interceptor) {
ISession session = (ISession)ContextSessions[sessionFactoryConfigPath];
if (session == null) {
if (interceptor != null) {
session = GetSessionFactoryFor(sessionFactoryConfigPath).OpenSession(interceptor);
}
else {
session = GetSessionFactoryFor(sessionFactoryConfigPath).OpenSession();
}
ContextSessions[sessionFactoryConfigPath] = session;
}
Check.Ensure(session != null, "session was null");
return session;
}
/// <summary>
/// Flushes anything left in the session and closes the connection.
/// </summary>
public void CloseSessionOn(string sessionFactoryConfigPath) {
ISession session = (ISession)ContextSessions[sessionFactoryConfigPath];
if (session != null && session.IsOpen) {
session.Flush();
session.Close();
}
ContextSessions.Remove(sessionFactoryConfigPath);
}
public ITransaction BeginTransactionOn(string sessionFactoryConfigPath) {
ITransaction transaction = (ITransaction)ContextTransactions[sessionFactoryConfigPath];
if (transaction == null) {
transaction = GetSessionFrom(sessionFactoryConfigPath).BeginTransaction();
ContextTransactions.Add(sessionFactoryConfigPath, transaction);
}
return transaction;
}
public void CommitTransactionOn(string sessionFactoryConfigPath) {
ITransaction transaction = (ITransaction)ContextTransactions[sessionFactoryConfigPath];
try {
if (HasOpenTransactionOn(sessionFactoryConfigPath)) {
transaction.Commit();
ContextTransactions.Remove(sessionFactoryConfigPath);
}
}
catch (HibernateException) {
RollbackTransactionOn(sessionFactoryConfigPath);
throw;
}
}
public bool HasOpenTransactionOn(string sessionFactoryConfigPath) {
ITransaction transaction = (ITransaction)ContextTransactions[sessionFactoryConfigPath];
return transaction != null && !transaction.WasCommitted && !transaction.WasRolledBack;
}
public void RollbackTransactionOn(string sessionFactoryConfigPath) {
ITransaction transaction = (ITransaction)ContextTransactions[sessionFactoryConfigPath];
try {
if (HasOpenTransactionOn(sessionFactoryConfigPath)) {
transaction.Rollback();
}
ContextTransactions.Remove(sessionFactoryConfigPath);
}
finally {
CloseSessionOn(sessionFactoryConfigPath);
}
}
/// <summary>
/// Since multiple databases may be in use, there may be one transaction per database
/// persisted at any one time. The easiest way to store them is via a hashtable
/// with the key being tied to session factory. If within a web context, this uses
/// <see cref="HttpContext" /> instead of the WinForms specific <see cref="CallContext" />.
/// Discussion concerning this found at http://forum.springframework.net/showthread.php?t=572
/// </summary>
private Hashtable ContextTransactions {
get {
if (IsInWebContext()) {
if (HttpContext.Current.Items[TRANSACTION_KEY] == null)
HttpContext.Current.Items[TRANSACTION_KEY] = new Hashtable();
return (Hashtable)HttpContext.Current.Items[TRANSACTION_KEY];
}
else {
if (CallContext.GetData(TRANSACTION_KEY) == null)
CallContext.SetData(TRANSACTION_KEY, new Hashtable());
return (Hashtable)CallContext.GetData(TRANSACTION_KEY);
}
}
}
/// <summary>
/// Since multiple databases may be in use, there may be one session per database
/// persisted at any one time. The easiest way to store them is via a hashtable
/// with the key being tied to session factory. If within a web context, this uses
/// <see cref="HttpContext" /> instead of the WinForms specific <see cref="CallContext" />.
/// Discussion concerning this found at http://forum.springframework.net/showthread.php?t=572
/// </summary>
private Hashtable ContextSessions {
get {
if (IsInWebContext()) {
if (HttpContext.Current.Items[SESSION_KEY] == null)
HttpContext.Current.Items[SESSION_KEY] = new Hashtable();
return (Hashtable)HttpContext.Current.Items[SESSION_KEY];
}
else {
if (CallContext.GetData(SESSION_KEY) == null)
CallContext.SetData(SESSION_KEY, new Hashtable());
return (Hashtable)CallContext.GetData(SESSION_KEY);
}
}
}
private bool IsInWebContext() {
return HttpContext.Current != null;
}
private Hashtable sessionFactories = new Hashtable();
private const string TRANSACTION_KEY = "CONTEXT_TRANSACTIONS";
private const string SESSION_KEY = "CONTEXT_SESSIONS";
}
}
Regards