sebastian_klose
07-05-2007, 04:02 PM
Hi,
I'm trying to use multiple DAOs in one transaction, but I always get an "ObjectDisposedException" saying: "can't access disposed object: AdoTransaction." The exception is thrown when I call the second DAO - without the second DAO everything works fine.
My Code looks like this:
class AccountDao : IAccountDao
{
public Account Create(string username, string password)
{
ISession session = base.SessionFactory.GetCurrentSession();
// or: session = base.Session;
// or: session = base.DoGetSession(true);
// - doesn't make any difference in regard to the exception
Account a = new Account(username, password);
session.Save(a);
// or: HibernateTemplate.Save(a) - without creating the session before
return a;
}
class SessionDao : ISessionDao
{
public Session Create(Account forAccount)
{
ISession session = base.SessionFactory.GetCurrentSession();
Session s = new Session(forAccount, Guid.NewGuid());
session.Save(s);
return s;
}
}
class AccountService : IAccountService
{
[Transaction()]
public string Register(string username, string password)
{
Account a = AccountDao.Create(username, password);
Session s = SessionDao.Create(a);
return s.Ticket;
}
// initialized by spring
public IAccountDao { get ...; set ...; }
public ISessionDao { get; set; }
}
The configuration is:
<!-- Property placeholder configurer for database settings -->
<object type="Spring.Objects.Factory.Config.PropertyPlaceholderC onfigurer, Spring.Core">
<property name="ConfigSections" value="databaseSettings"/>
</object>
<!-- Database and NHibernate Configuration -->
<db:dbProvider id="DbProvider" provider="MySql" connectionString="xxx" />
<object id="MySessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>Hpi.Middleware.CarRental.DataNHibernate</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="hibernate.dialect" value="NHibernate.Dialect.MySQLDialect"/>
<entry key="hibernate.connection.driver_class" value="NHibernate.Driver.MySqlDataDriver"/>
<entry key="hibernate.current_session_context_class" value="Spring.Data.NHibernate.SpringSessionContext, Spring.Data.NHibernate12"/>
</dictionary>
</property>
</object>
<!-- Transaction Management -->
<object id="AutoProxyCreator" type="Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoP roxyCreator, Spring.Aop">
</object>
<object id="TransactionAdvisor" type="Spring.Transaction.Interceptor.TransactionAttribut eSourceAdvisor, Spring.Data" autowire="constructor">
</object>
<object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager , Spring.Data.NHibernate12">
<property name="DbProvider" ref="DbProvider" />
<property name="SessionFactory" ref="MySessionFactory" />
</object>
<object id="AttributeTransactionAttributeSource" type="Spring.Transaction.Interceptor.AttributesTransacti onAttributeSource, Spring.Data">
</object>
<object id="TransactionInterceptor" type="Spring.Transaction.Interceptor.TransactionIntercep tor, Spring.Data">
<property name="TransactionManager" ref="HibernateTransactionManager"/>
<property name="TransactionAttributeSource" ref="AttributeTransactionAttributeSource"/>
</object>
Any ideas how to solve this problem? My last resort would be combining both DAOs into one - but that's not a nice solution.
Best regards
Sebastian Klose
I'm trying to use multiple DAOs in one transaction, but I always get an "ObjectDisposedException" saying: "can't access disposed object: AdoTransaction." The exception is thrown when I call the second DAO - without the second DAO everything works fine.
My Code looks like this:
class AccountDao : IAccountDao
{
public Account Create(string username, string password)
{
ISession session = base.SessionFactory.GetCurrentSession();
// or: session = base.Session;
// or: session = base.DoGetSession(true);
// - doesn't make any difference in regard to the exception
Account a = new Account(username, password);
session.Save(a);
// or: HibernateTemplate.Save(a) - without creating the session before
return a;
}
class SessionDao : ISessionDao
{
public Session Create(Account forAccount)
{
ISession session = base.SessionFactory.GetCurrentSession();
Session s = new Session(forAccount, Guid.NewGuid());
session.Save(s);
return s;
}
}
class AccountService : IAccountService
{
[Transaction()]
public string Register(string username, string password)
{
Account a = AccountDao.Create(username, password);
Session s = SessionDao.Create(a);
return s.Ticket;
}
// initialized by spring
public IAccountDao { get ...; set ...; }
public ISessionDao { get; set; }
}
The configuration is:
<!-- Property placeholder configurer for database settings -->
<object type="Spring.Objects.Factory.Config.PropertyPlaceholderC onfigurer, Spring.Core">
<property name="ConfigSections" value="databaseSettings"/>
</object>
<!-- Database and NHibernate Configuration -->
<db:dbProvider id="DbProvider" provider="MySql" connectionString="xxx" />
<object id="MySessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>Hpi.Middleware.CarRental.DataNHibernate</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="hibernate.dialect" value="NHibernate.Dialect.MySQLDialect"/>
<entry key="hibernate.connection.driver_class" value="NHibernate.Driver.MySqlDataDriver"/>
<entry key="hibernate.current_session_context_class" value="Spring.Data.NHibernate.SpringSessionContext, Spring.Data.NHibernate12"/>
</dictionary>
</property>
</object>
<!-- Transaction Management -->
<object id="AutoProxyCreator" type="Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoP roxyCreator, Spring.Aop">
</object>
<object id="TransactionAdvisor" type="Spring.Transaction.Interceptor.TransactionAttribut eSourceAdvisor, Spring.Data" autowire="constructor">
</object>
<object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager , Spring.Data.NHibernate12">
<property name="DbProvider" ref="DbProvider" />
<property name="SessionFactory" ref="MySessionFactory" />
</object>
<object id="AttributeTransactionAttributeSource" type="Spring.Transaction.Interceptor.AttributesTransacti onAttributeSource, Spring.Data">
</object>
<object id="TransactionInterceptor" type="Spring.Transaction.Interceptor.TransactionIntercep tor, Spring.Data">
<property name="TransactionManager" ref="HibernateTransactionManager"/>
<property name="TransactionAttributeSource" ref="AttributeTransactionAttributeSource"/>
</object>
Any ideas how to solve this problem? My last resort would be combining both DAOs into one - but that's not a nice solution.
Best regards
Sebastian Klose