PDA

View Full Version : Transaction Management With Ibatis.NET


darryl_staflund
02-01-2007, 06:49 PM
Hi all,

Does anyone have any tips on how I might be able to integrate Spring's ADO transaction manage features with IBatis.NET? I have managed to integrate Spring.NET and IBatis.NET after a little work and would like to have Spring manage transactions.

Here is how I do my Spring.NET / IBatis.NET integration. I won't detail everything but will cut and paste some of the more important code:


<!-- ################################################## ##################
##
## Section: Ibatis Manager Builder.
## Notes: This class is responsible for parsing the various
## IBatis.NET configuration files. It is the start of
## everything IBatis in our project.
##
################################################## ################## -->
<object id="DAOManagerBuilder"
type="IBatisNet.DataAccess.Configuration.DomDaoManagerBu ilder, IBatisNet.DataAccess"
init-method="Configure">
<description>
<![CDATA[
This object is responsible for parsing the iBatis.NET configuration files and then
creating 'IBatisNET.DataMapper.IDaoManager' singletons for each DAO context defined
in 'dao.config'.

The 'Configure' initialization kicks off the parsing process. Although it is possible
to pass the URI of 'dao.config' to the 'Configure' method outside of Spring.NET, I have
not found a way to pass parameters to initialization methods from within a Spring.NET
configuration file. As such, the 'dao.config' file must be located in the root of the
web application (i.e. the default iBatis.NET location for the file.)
]]>
</description>
</object>


<!-- ################################################## ##################
##
## IBatis Context: CED Mirror
##
################################################## ################## -->
<object id="CEDMirrorContextDAOManager"
depends-on="DAOManagerBuilder"
type="IBatisNet.DataAccess.DaoManager"
factory-method="GetInstance">
<description>
<![CDATA[
This DAO Manager is responsible for managing iBatis.NET data access objects (DAOs)
that reside within the CED Mirror Context specified within the 'dao.config' file.

Since this manager object is created by the 'DAOManagerBuilder' object, the
'DAOManagerBuilder' object must be initialized by Spring.NET before this object.
As such, this dependency is noted in the 'depends-on' attribute.

Since this object is a singleton, the static 'GetInstance' factory method is used
to obtain an instance of it. The name of the iBatis.NET context defined in the
'dao.config' file is passed to the factory method as an argument.

]]>
</description>
<constructor-arg type="string" value="CEDMirrorContext" />
</object>

<object id="CEDMirrorContextDAO" abstract="true">
<description>
<![CDATA[
This is an abstract object that all concrete DAO objects within this iBatis.NET context
are to extend. It defines properties common to all DAOs within the context.
]]>
</description>
<property name="DAOManager" ref="CEDMirrorContextDAOManager" />
</object>

<object id="BusinessUnitDAO" type="NSII.src.daos.impl.BusinessUnitDAOSqlMapImpl, NSII" parent="CEDMirrorContextDAO" />
<object id="CompanyDAO" type="NSII.src.daos.impl.CompanyDAOSqlMapImpl, NSII" parent="CEDMirrorContextDAO" />
<object id="EmployeeDAO" type="NSII.src.daos.impl.EmployeeDAOSqlMapImpl, NSII" parent="CEDMirrorContextDAO" />
<object id="JobTypeDAO" type="NSII.src.daos.impl.JobTypeDAOSqlMapImpl, NSII" parent="CEDMirrorContextDAO" />
<object id="OrganizationUnitDAO" type="NSII.src.daos.impl.OrganizationUnitDAOSqlMapImpl, NSII" parent="CEDMirrorContextDAO" />


Basically with this configuration, I can inject IBatis DAOs directly into my service classes. However, I still have to use the IBatis IDaoManager to manage transactions.

What I would like to know if there is something I can do in the config files to integrate IBatis's IDaoManager transaction features with those of Spring.

Thanks,
Darryl

Erich Eichinger
02-01-2007, 10:29 PM
Hi Darryl,

I'm pretty sure that it won't be possible to use spring-style IBatis transactions without writing some code. For integrating IBatis you may want to have a look at the Spring.IBatis (http://springnet.cvs.sourceforge.net/springnet/Spring.Net.Integration/projects/Spring.IBatis/) integration project. I don't know the state of this project, but it may be a good starting point. For implementing transaction support, you can take a look at Spring.Data.AdoPlatformTransactionManager or HibernateTransactionManager from the Spring.Data.NHibernate integration project.

As a simpler solution - if this is an option for you - you can always use Spring.Data.ServiceDomainPlatformTransactionManage r to use MS-DTC transactions. This requires running your application on COM+1.5 machines (WinXP, Win2003).


cheers,
Erich

darryl_staflund
02-01-2007, 11:05 PM
Thanks for your reply Erich. I had looked at the Spring.IBatis integration project early on in our project but found (if I remember rightly) that it focused on integrating IBatis.DataMapper but not IBatis.DataAccess. Since we were using the latter in addition to the former, I couldn't use it.

I will do some investigation and post anything I find of interest.

Darryl

Erich Eichinger
02-01-2007, 11:21 PM
Hi,

asfaik you would be doing pioneer work. Even the java world doesn't have DataAccess support I think. It will be great reading your research results!

cheers,
Erich

darryl_staflund
03-20-2007, 05:54 PM
Hi there,

I am just writing to amend my post on IBatis DataAccess integration.

DataAccess and Spring.NET don't play well together, and after having heard that IBatis (the Java version) is no longer being developed, I've decided not to pursue the issue any further.

I did want to mention though that I have found a better way of defining my IBatis DAOs within Spring.NET so that developers don't explicitly have to open and close connections. Here is how my configuration file now looks (NB: Sample snippet follows...):

1. First, slurp in the dao.config using the following definition:

<object id="DAOManagerBuilder"
type="IBatisNet.DataAccess.Configuration.DomDaoManagerBu ilder, IBatisNet.DataAccess"
init-method="Configure" />

2. Next, obtain a reference to a DAO manager:

<object id="CEDMirrorContextDAOManager"
depends-on="DAOManagerBuilder"
type="IBatisNet.DataAccess.DaoManager"
factory-method="GetInstance">
<constructor-arg type="string" value="CEDMirrorContext" />
</object>

3. Then, obtain the IBatis DAOs from the DAO manager:

<object id="BusinessUnitDAO" factory-object="CEDMirrorContextDAOManager" factory-method="GetDao">
<constructor-arg type="System.Type" value="NSII.src.daos.IBusinessUnitDAO, NSII" />
</object>
<object id="CompanyDAO" factory-object="CEDMirrorContextDAOManager" factory-method="GetDao">
<constructor-arg type="System.Type" value="NSII.src.daos.ICompanyDAO, NSII" />
</object>

etc.

At this point you are free to inject the DAOs into your services and use them without having to manage connections.

There are some comments to be made about doing things this way though:

1. I found that since DAOs are obtained from an instance factory, I am never allowed to work with the DAO's implementation class directly -- only its interface.

2. I found that I can't inject object references into the DAO because I get dynamic proxy-related exceptions. I did not copy these exceptions down so I can't remember what they are offhand.

Other than that, it works very nicely :-)

Darryl