Illegal attempt to associate a collection with two open sessions

199 views
Skip to first unread message

Deepraj

unread,
Jun 2, 2010, 4:34:26 AM6/2/10
to nhusers, deep...@gmail.com
HI All,

When I try to update the entity using NHibernate, I am getting the
error as follows

"Illegal attempt to associate a collection with two open sessions"

Find below the stack trace

at
NHibernate.Collection.AbstractPersistentCollection.SetCurrentSession(ISessionImplementor
session)
at NHibernate.Event.Default.OnLockVisitor.ProcessCollection(Object
collection, CollectionType type)
at NHibernate.Event.Default.AbstractVisitor.ProcessValue(Object
value, IType type)
at NHibernate.Event.Default.AbstractVisitor.ProcessValue(Int32 i,
Object[] values, IType[] types)
at
NHibernate.Event.Default.AbstractVisitor.ProcessEntityPropertyValues(Object[]
values, IType[] types)
at NHibernate.Event.Default.AbstractVisitor.Process(Object obj,
IEntityPersister persister)
at
NHibernate.Event.Default.AbstractReassociateEventListener.Reassociate(AbstractEvent
event, Object entity, Object id, IEntityPersister persister)
at
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformUpdate(SaveOrUpdateEvent
event, Object entity, IEntityPersister persister)
at
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsDetached(SaveOrUpdateEvent
event)
at
NHibernate.Event.Default.DefaultUpdateEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent
event)
at
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent
event)
at NHibernate.Impl.SessionImpl.FireUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.Update(Object obj)
at CS3Repository.PreRegHeaderRepository.EditPreRegHeader(ISession
session, PreRegHeader PreRegHeaderToUpdate) in E:\generate
\PersistentClasses\Temp\CS3Entities\Repository
\PreRegHeaderRepository.cs:line 153
at
RegEvntServiceBase.RegEvntServiceBase.EditPreRegHeader(PreRegHeader
PreRegHeaderToEdit) in E:\generate\PersistentClasses\Temp\RegEvnt
\RegEvntService\RegEvntServiceBase.cs:line 102


Given below is my code

I am opening a session like this

ISession session = nHibernateHelper.OpenSession();

Inside nHibernateHelper, a static variable is used to create the
session factory and each time not null value is checked inorder to
create a new session factory.

private static ISessionFactory _sessionFactory;

public static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
Configuration configuration = new Configuration();
configuration.Configure();
...................
}
}
}


After creating the session, a call is made to open a transaction
and then the update statement is executed like

session.Update(...);

Also these are the settings in hibernate.config.xml

<property
name="connection.provider">NHibernate.Connection.DriverConnectionProvider</
property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</
property>
<property
name="connection.driver_class">NHibernate.Driver.SqlClientDriver</
property>
<property
name="connection.connection_string">Server=DevServer;initial
catalog=SportsDB;Trusted_Connection=True</property>
<property name="show_sql">false</property>
<property
name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory,
NHibernate.ByteCode.Castle</property>
<property name="current_session_context_class">web</property>
<property name='adonet.batch_size'>20</property>
<property
name='cache.provider_class'>NHibernate.Cache.HashtableCacheProvider</
property>
<property name='cache.use_query_cache'>true</property>
<property name='default_schema'>SportsDB.dbo</property>
<property name='connection.isolation'>ReadCommitted</property>

Also note that cascade = All is given the mapping files..

<set name="PreRegVols" cascade="all" lazy="true" inverse="true" batch-
size="4">
<...>
</set>

On seeing a post I removed cascade ="all" from the set tag, but the
result is same.

Please help me on this. This is urgent.

Thanks in advance,

Deepraj D

John Davidson

unread,
Jun 2, 2010, 7:26:40 AM6/2/10
to nhu...@googlegroups.com
Try using SaveOrUpdate instead of just Update

John Davidson


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


DeepRaj D

unread,
Jun 2, 2010, 7:48:08 AM6/2/10
to nhu...@googlegroups.com
Hi John,
 
I tried using SaveOrUpdate instead of Update.
 
Also I missed one thing. While creating the session factory, I am adding more than one assembly to the configuration object.
 
Code sample
 

files =

Directory.GetFiles(src, "*Entities.dll");

foreach (string file in files)

{

        Assembly assembly;

        assembly = Assembly.LoadFrom(file);

        configuration.AddAssembly(assembly);

}      

 
Will this be the reason for the above said issue?
 
Regards,
Deepraj D

Sachin

unread,
Jul 22, 2010, 3:25:36 PM7/22/10
to nhusers
session.begintransaction and session.saveorupdate/delete creates two
separate session. did u solve this one?

On Jun 2, 6:48 am, DeepRaj D <deepr...@gmail.com> wrote:
> Hi John,
>
> I tried using SaveOrUpdate instead of Update.
>
> Also I missed one thing. While creating the session factory, I am adding
> more than one assembly to the configuration object.
>
> *Code sample*
> **
>
> files = Directory.GetFiles(src, "*Entities.dll");
>
> foreach (string file in files)
>
> {
>
>         Assembly assembly;
>
>         assembly = Assembly.LoadFrom(file);
>
>         configuration.AddAssembly(assembly);
>
> }
>
> Will this be the reason for the above said issue?
>
> Regards,
> Deepraj D
>
> On Wed, Jun 2, 2010 at 4:56 PM, John Davidson <jwdavid...@gmail.com> wrote:
> > Try using SaveOrUpdate instead of just Update
>
> > John Davidson
>
> >> nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/nhusers?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to nhu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> > .

Sachin

unread,
Jul 22, 2010, 3:24:51 PM7/22/10
to nhusers
So far what i have read ......
session.begintransaction and session.saveorupdate/Delete create two
separate session.

i do not know how....but if you have solutions regarding this let me
know as well...

On Jun 2, 6:48 am, DeepRaj D <deepr...@gmail.com> wrote:
> Hi John,
>
> I tried using SaveOrUpdate instead of Update.
>
> Also I missed one thing. While creating the session factory, I am adding
> more than one assembly to the configuration object.
>
> *Code sample*
> **
>
> files = Directory.GetFiles(src, "*Entities.dll");
>
> foreach (string file in files)
>
> {
>
>         Assembly assembly;
>
>         assembly = Assembly.LoadFrom(file);
>
>         configuration.AddAssembly(assembly);
>
> }
>
> Will this be the reason for the above said issue?
>
> Regards,
> Deepraj D
>
> On Wed, Jun 2, 2010 at 4:56 PM, John Davidson <jwdavid...@gmail.com> wrote:
> > Try using SaveOrUpdate instead of just Update
>
> > John Davidson
>
> >> nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/nhusers?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to nhu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
0 new messages