You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Robert Glover <ro...@yahoo.com> on 2007/06/25 17:08:29 UTC

Question on using Transactions with iBatis and Spring

I am confused over how to use transactions with iBatis and Spring. (I'm
actually using iBatis and Abator and Spring).

   The Spring 2.0.6 manual (section 12.5) says,

    "...Transaction management can be handled through Spring's standard
facilities.  There are no special transaction strategies for iBATIS, as there
is no special transactional resource involved other than a JDBC Connection.
Hence, Spring's standard JDBC DataSourceTransactionManager or
JtaTransactionManager are perfectly sufficient. ..."

    On the other hand, page 152 of the book "iBatis in Action" by
Begin/Goddin/Meadors says:  "...Local transaction are configured in the iBatis
SQL Map configuration XML file as a JDBC transaction manager."  The iBatis book
then shows an example of using "sqlMapClient.startTractsion()",
"sqlMapClient.commitTransaction", and "sqlMapClient.endTransaction()".

    In my sql-map-config.xml I don't have any transaction related statements at
all.  In my Spring application context file I have: 

<!-- SqlMap setup for iBATIS Database Layer -->
<bean id="sqlMapClient"    
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation" value="WEB-INF/sql-map-config.xml"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

     Can anybody add any light on using iBatis with Spring and transactions?
Note that because I use Abator (a wonderful facility) I don't write an iBatis
code at all, I just invoke the methods on the DAOs that Abator creates.

Robert




Re: Question on using Transactions with iBatis and Spring

Posted by Chris Lamey <cl...@localmatters.com>.
Hello,

I think it depends on how you are setting your data source.  If you are
setting the iBATIS DataSource via the Spring SqlMapClientFactoryBean in
a Spring context, you are using an EXTERNAL DataSource as far as iBATIS
is concerned.  iBATIS at that point wouldn't have anything to do with
the creation of configuration of the DataSource or any transaction
management.  Note that means any explicit transaction Java code
(startTransaction()/endTransaction()/etc) you might have will also be
ignored.

If you don't set a DataSource on the Spring SqlMapClientFactoryBean in
your context, then you need to handle the DataSource and transaction
handling in the sqlmap config file.

IMHO, using the Spring declarative transaction handling is the way to
go.  It's completely transparent, you don't need to write any code for
it, and it keeps your application loosely coupled.

Cheers,
Chris

On Mon, 2007-06-25 at 08:08 -0700, Robert Glover wrote:
> I am confused over how to use transactions with iBatis and Spring. (I'm
> actually using iBatis and Abator and Spring).
> 
>    The Spring 2.0.6 manual (section 12.5) says,
> 
>     "...Transaction management can be handled through Spring's standard
> facilities.  There are no special transaction strategies for iBATIS, as there
> is no special transactional resource involved other than a JDBC Connection.
> Hence, Spring's standard JDBC DataSourceTransactionManager or
> JtaTransactionManager are perfectly sufficient. ..."
> 
>     On the other hand, page 152 of the book "iBatis in Action" by
> Begin/Goddin/Meadors says:  "...Local transaction are configured in the iBatis
> SQL Map configuration XML file as a JDBC transaction manager."  The iBatis book
> then shows an example of using "sqlMapClient.startTractsion()",
> "sqlMapClient.commitTransaction", and "sqlMapClient.endTransaction()".
> 
>     In my sql-map-config.xml I don't have any transaction related statements at
> all.  In my Spring application context file I have: 
> 
> <!-- SqlMap setup for iBATIS Database Layer -->
> <bean id="sqlMapClient"    
> class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
>     <property name="configLocation" value="WEB-INF/sql-map-config.xml"/>
>     <property name="dataSource" ref="dataSource"/>
> </bean>
> 
>      Can anybody add any light on using iBatis with Spring and transactions?
> Note that because I use Abator (a wonderful facility) I don't write an iBatis
> code at all, I just invoke the methods on the DAOs that Abator creates.
> 
> Robert
> 
> 
> 

Re: Question on using Transactions with iBatis and Spring

Posted by Brandon Goodin <br...@gmail.com>.
There are multiple ways to handle transcations. You can handle them from the
sqlmap. This is apparently what you are referencing in the book. You can
also manage them with normal Spring mechansisms. Following is the
configuration for Spring we are using on a current project.

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- DAO Configuration -->

    <bean name="transactionManager" class="
org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="sqlMapClient" class="
org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation"
value="classpath:com/mwt/internal/dao/sqlmap/SqlMapConfig.xml"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="useTransactionAwareDataSource" value="true"/>
    </bean>

    <bean id="dataSource" class="some.class.here.for.a.DataSource">
        <constructor-arg index="0" value="${driverClassName}"/>
        <constructor-arg index="1" value="${databaseUrl}"/>
        <constructor-arg index="2" value="${userName}"/>
        <constructor-arg index="3" value="${password}"/>
    </bean>

We do not start transactions on the SqlMap rather we use the Spring
transaction callback or annotations. It would also be possible to configure
the transactions in the context xml.

Callback example:
TransactionTemplate tt = new TransactionTemplate(transactionManager);

tt.execute(new TransactionCallbackWithoutResult() {
  public void doInTransactionWithoutResult(TransactionStatus
transactionStatus) {
    myDao.insert(myObject);
    myOtherDao.update(myOtherObject);
    ....
  }
}

Annotation Example:
@Transactional
public void insertMethodOnServiceClass(MyObject myObject) {
  myDao.insert(myObject);
  myOtherDao.update(myObject);
}

Hope that helps,
Brandon Goodin

On 6/25/07, Robert Glover <ro...@yahoo.com> wrote:
>
> I am confused over how to use transactions with iBatis and Spring. (I'm
> actually using iBatis and Abator and Spring).
>
>    The Spring 2.0.6 manual (section 12.5) says,
>
>     "...Transaction management can be handled through Spring's standard
> facilities.  There are no special transaction strategies for iBATIS, as
> there
> is no special transactional resource involved other than a JDBC
> Connection.
> Hence, Spring's standard JDBC DataSourceTransactionManager or
> JtaTransactionManager are perfectly sufficient. ..."
>
>     On the other hand, page 152 of the book "iBatis in Action" by
> Begin/Goddin/Meadors says:  "...Local transaction are configured in the
> iBatis
> SQL Map configuration XML file as a JDBC transaction manager."  The iBatis
> book
> then shows an example of using "sqlMapClient.startTractsion()",
> "sqlMapClient.commitTransaction", and "sqlMapClient.endTransaction()".
>
>     In my sql-map-config.xml I don't have any transaction related
> statements at
> all.  In my Spring application context file I have:
>
> <!-- SqlMap setup for iBATIS Database Layer -->
> <bean id="sqlMapClient"
> class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
>     <property name="configLocation" value="WEB-INF/sql-map-config.xml"/>
>     <property name="dataSource" ref="dataSource"/>
> </bean>
>
>      Can anybody add any light on using iBatis with Spring and
> transactions?
> Note that because I use Abator (a wonderful facility) I don't write an
> iBatis
> code at all, I just invoke the methods on the DAOs that Abator creates.
>
> Robert
>
>
>
>