PDA

View Full Version : Transaction Mgm using Hibernate Dao's and ADO Dao's together



felipenasc
02-14-2008, 12:20 PM
Hi

I am new to Spring.NET, but have some experience with Java Spring. I am trying to "sell" the idea of using Spring.NET to a company that is starting a new .NET project. To help sell this idea, I am developing a sample app that has DAO interfaces with two implementations: HibernateDao's and AdoDao's. Now I am trying to implement one service configured to use a HibernateDao and another service class configured to use a AdoDao.

My problem now is: how do I configure Spring's Transaction Management so that the same transaction can be used by AdoDao and HibernateDao at the same time?

I have configured like this:



<?xml version="1.0"?>
<objects xmlns='http://www.springframework.net'
xmlns:db="http://www.springframework.net/database">

<!-- Database and NHibernate Configuration -->
<db:provider id="DbProvider"
provider="SqlServer-1.1"
connectionString="Integrated Security=false; Data Source=${db.datasource};Database=${db.database};Us er ID=${db.user};Password=${db.password};" />

<!-- Property placeholder configurer for database settings -->
<object type="Spring.Objects.Factory.Config.PropertyPlaceholderC onfigurer, Spring.Core">
<property name="ConfigSections" value="databaseSettings"/>
</object>



<!-- DB configuration -->
<object id="sessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate">
<property name="DbProvider" ref="DbProvider"/>
<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.show_sql" value="true"/>
<entry key="hibernate.max_fetch_depth" value="2" />
<!--
<entry key="hibernate.hbm2ddl.auto" value="create"/>
-->
</dictionary>
</property>
<property name="MappingAssemblies">
<list>
<value>Fgv.Dao.Hibernate</value>
</list>
</property>
</object>

<object id="transactionManager" type="Spring.Data.Core.TxScopeTransactionManager, Spring.Data">
</object>


<object id="hibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager , Spring.Data.NHibernate">
<property name="DbProvider" ref="DbProvider"/>
<property name="sessionFactory" ref="sessionFactory"/>
</object>

<object id="HibernateTemplate" type="Spring.Data.NHibernate.HibernateTemplate">
<property name="SessionFactory" ref="sessionFactory" />
<property name="TemplateFlushMode" value="Auto" />
<property name="CacheQueries" value="true" />
</object>

<object id="AdoTemplate" type="Spring.Data.Core.AdoTemplate, Spring.Data">
<property name="DbProvider" ref="DbProvider"/>
</object>


</objects>


Another config file has:

<?xml version="1.0"?>
<objects xmlns="http://www.springframework.net">

<object id="ProxyCreator" type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxy Creator, Spring.Aop">
<property name="ObjectNames">
<list>
<value>*Service*</value>
</list>
</property>
<property name="InterceptorNames">
<list>
<value>aroundAdvisor</value>
<value>transactionInterceptor</value>
</list>
</property>
</object>

<!-- transaction management -->
<object id="transactionAdvisor"
type="Spring.Transaction.Interceptor.TransactionAttribut eSourceAdvisor, Spring.Data"
autowire="constructor">
</object>
<object id="transactionInterceptor"
type="Spring.Transaction.Interceptor.TransactionIntercep tor, Spring.Data">
<property name="TransactionManager" ref="transactionManager"/>
<property name="TransactionAttributeSource" ref="attributeTransactionAttributeSource"/>
</object>
<object id="attributeTransactionAttributeSource"
type="Spring.Transaction.Interceptor.AttributesTransacti onAttributeSource, Spring.Data">
</object>
</objects>


And my service class is like this:



using System.Collections;
using Fgv.Dao;
using Fgv.Domain;
using Spring.Transaction;
using Spring.Transaction.Interceptor;

namespace Fgv.Services.Impl
{
public class PessoaService : IPessoaService
{
//... Spring injected properties

[Transaction(TransactionPropagation.Supports, ReadOnly = true)]
public IList GetPessoas()
{
return PessoaDao.GetAll();
}

[Transaction(TransactionPropagation.Required)]
public void SavePessoa(Pessoa p)
{
PessoaDao.Save(p);
}

}
}



Is this the right way to go?
The thing I find strange is that I configured transactionManager and hibernateTransactionManager but no other object references hibernateTransactionManager.... are HibernateDaos being executed with transaction management?

Thanks
Felipe

Mark Pollack
02-17-2008, 12:47 PM
Hi Felipe,

You do not need to specify both TxScopeTransactionManager and HibernateTransactionManager. Only specify HibernateTransactionManager, this will allow you to mix both ado.net and nhibernate operations within the same local ADO.NET based transaction. The chapter on NHibernate transaction management (http://www.springframework.net/doc-latest/reference/html/orm.html#orm-tx-mgmt) may help if you haven't read it already, if yes, let me know how it can be improved to better help.

Please post back if you have any issues getting mixed ado.net/nhibernate functionality to work. If needed I can provide a sample.

Cheers,
Mark

felipenasc
02-18-2008, 09:16 PM
Thanks a lot for the quick reply.
This NHibernate Tx Mgm documentation is what I need. I have read part of it before, but not the first two paragraphs...
:-P
That is the problem with quick readings...

Anyway, my issue is solved now.
Thanks
Felipe