Results 1 to 2 of 2

Thread: Illegally attempted to associate a proxy with two open Sessions

  1. #1
    Join Date
    Mar 2012
    Posts
    3

    Default Illegally attempted to associate a proxy with two open Sessions

    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>
    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>
    c#:

    Code:
    [Transaction(ReadOnly = false)]
    public void SynchronizeCandidate()
    {
            CandidateEntity candidate = CandidateRepositoryRemote.Get(View.Identification);
            CandidateRepository.Add(candidate);
    }
    I think I have to 'disconnect' the candidate object from the first sessionFactory, but I don't know how.
    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

  2. #2
    Join Date
    Jul 2010
    Posts
    245

    Default

    This scenario (passing an NH proxy object from one session to another) isn't support by NHibernate (which is why its not supported by SPRNET either ). 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.

    I'd recommend posting this issue in the NHUSERS group on google (http://groups.google.com/group/nhusers) to see how others recommend you solve it (e.g., its not per se an issue with SPRNET, but instead an issue with NH itself).

    -Steve B.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •