Hello,
I have an error when trying to add an object received from one sessionFactory into an other sessionFactory: Illegally attempted to associate a proxy with two open Sessions.
My code:
Config:
Code:<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net" xmlns:db="http://www.springframework.net/database" xmlns:tx="http://www.springframework.net/tx"> <!-- Property placeholder configurer for database settings --> <object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core"> <property name="ConfigSections" value="appSettings"/> </object> <!-- Database and NHibernate Configuration --> <db:provider id="DbProvider" provider="${provider}" connectionString="${connection}"/> <object id="sessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12"> <property name="DbProvider" ref="DbProvider"/> <property name="MappingAssemblies"> <list> <value>Core</value> <value>Presenters</value> </list> </property> <property name="HibernateProperties"> <dictionary> <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/> <entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/> <entry key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/> <entry key="hibernate.cache.provider_class" value="NHibernate.Caches.SysCache.SysCacheProvider,NHibernate.Caches.SysCache" /> <!-- this parameter makes really slow some parts of the application: --> <entry key="hibernate.show_sql" value="true"/> <entry key="expiration" value="120" /> </dictionary> </property> </object> <!-- Database Remote and NHibernate Configuration --> <db:provider id="DbProviderRemote" provider="${providerRemote}" connectionString="${connectionRemote}"/> <object id="sessionFactoryRemote" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12"> <property name="DbProvider" ref="DbProviderRemote"/> <property name="MappingAssemblies"> <list> <value>Core</value> <value>Presenters</value> </list> </property> <property name="HibernateProperties"> <dictionary> <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/> <entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/> <entry key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/> <entry key="hibernate.cache.provider_class" value="NHibernate.Caches.SysCache.SysCacheProvider,NHibernate.Caches.SysCache" /> <!-- this parameter makes really slow some parts of the application: --> <entry key="hibernate.show_sql" value="true"/> <entry key="expiration" value="120" /> </dictionary> </property> </object> <object id="transactionManager" type="Spring.Data.Core.TxScopeTransactionManager, Spring.Data"> </object> <tx:attribute-driven/> </objects>c#:Code:<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net"> <object id="candidateRepository" type="Cebir.Core.Repository.CandidateRepository, Core"> <property name="SessionFactory" ref="sessionFactory"/> </object> <object id="candidateRepositoryRemote" type="Cebir.Core.Repository.CandidateRepository, Core"> <property name="SessionFactory" ref="sessionFactoryRemote"/> </object> </objects>
I think I have to 'disconnect' the candidate object from the first sessionFactory, but I don't know how.Code:[Transaction(ReadOnly = false)] public void SynchronizeCandidate() { CandidateEntity candidate = CandidateRepositoryRemote.Get(View.Identification); CandidateRepository.Add(candidate); }
I already tried:
- Evict the candidate from first session after Get
- Use SaveOrUpdate instead of Add
Someone know the correct solution? I'm using NHibernate 1.2 and Spring 1.1.
Thanks in advance.
Kevin


Reply With Quote
). Instead, you need to 'unwrap' the proxy (IIRC there is a method in the HibernateUtility class in NH for this) before you attempt to add it to another session.