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 Collin Peters <ca...@gmail.com> on 2007/06/09 02:15:23 UTC

Transactions with iBatis and Spring

I am trying to figure out how transactions work when using Spring and
iBatis.  I am ultimately trying to use transactions at the Spring
level using annotations.  I am very confused about all the different
places for config info.

This is my config BEFORE I tried to implement transactions
applicationContext.xml
--------------------------------
<beans>
    --snip out unrelated code

	<!-- Pooled datasource from JNDI -->
	<jee:jndi-lookup id="dataSource"
		jndi-name="/jdbc/devel"
		resource-ref="true" />
	
	<!--  Web SqlMap setup for iBATIS Database Layer -->
	<bean id="dataImportSqlMapConfig"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation">
			<value>classpath:dataImportSqlMapConfig.xml</value>
		</property>
	</bean>

	<!-- Define DAO beans-->
	<bean id="dataImportDAOBean"
class="intouch.dataimport.dao.impl.DataImportDAOImpl">
		<property name="sqlMapClient" ref="dataImportSqlMapConfig"/>
	</bean>
</beans>

dataImportSqlMapConfig.xml
-------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config
2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
	<sqlMap resource="sqlmaps/DataImport.xml" />
</sqlMapConfig>


This all worked fine.  Then I changed the configs to the following in
an effort to implement transactions
applicationContext.xml
--------------------------------
	<!-- Transaction Manager -->
	<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
 >
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- Tell Spring to look for beans annotated for transaction -->
	<tx:annotation-driven/>

application code
-------------------------
	@Transactional(propagation=Propagation.REQUIRES_NEW, readOnly=false)
	public void runCSVDataImport() throws Exception
	{
....

Using this code has done *nothing* so I must be missing something.
All the code still executes if it were not in a transaction.  I have
debugged manually by throwing a RuntimeException (unchecked) which
should, as the docs say, implement a rollback.

So I have been trying to get into the iBatis docs to see if anything
additional is required on the iBatis side.  I currently do not have a
<transactionManager> (or nested <dataSource>) element in my iBatis
config.  My first question is "are these elements required when using
Spring transactions?".  I tested by adding it with the following to my
iBatis config:
    <transactionManager type="EXTERNAL">
        <property name="SetAutoCommitAllowed" value="false"/>
        <dataSource type="JNDI">
            <property name="DataSource" value="java:comp/env/jdbc/devel"/>
        </dataSource>
    </transactionManager>
This did not help matters, and added to my confusion because I could
then remove the dataSource bean from my Spring config which means it
definitely isn't using any of the Spring transaction management stuff.

Are the transactionManager elements from Spring (a bean) and iBatis
(an xml element) mutually exclusive?  If I use the Spring one do I
need one in iBatis?

Please let me know if any other information would help.

Collin

Re: Transactions with iBatis and Spring

Posted by Collin Peters <ca...@gmail.com>.
The only difference I see there is the "useTransactionAwareDataSource"
property.  I cannot find any information on this property, but it did
not help anyways.

There must be some detailed information on this somewhere.  My first
main question is do I need the iBatis configuration parameter
<transactionManager>, when I wish to use Spring's transaction support.

I did some more research and perhaps the problem is my lack of
understanding of the Spring internals.  A detail I omitted before is
that this code is being run through the Quartz scheduler.  It seems
the Quartz SchedulerFactoryBea has it's own property for setting the
transaction properties to use.  However, it also seems I am not using
these anyways as I am passing in a DAO into the class being executed
by the scheduler, and that DAO is using iBatis which *does* have the
transactional properties set.  So I'm really at a loss of how all this
is supposed to come together.

Any help would be greatly appreciated.

Collin

On 6/11/07, Meindert <me...@pastelebusiness.com> wrote:
> Hi Colin,
>
> There is a description for the change from Ibatis DAO on the petstore to the
> Spring framework including the transaction handling
> http://opensource.atlassian.com/confluence/oss/display/IBATIS/Converting+iBA
> TIS+DAO+to+Spring+DAO
>
> Kind Regards
>  Meindert
>
> -----Original Message-----
> From: Collin Peters [mailto:cadiolis@gmail.com]
> Sent: 09 June 2007 02:15 AM
> To: user-java@ibatis.apache.org
> Subject: Transactions with iBatis and Spring
>
> I am trying to figure out how transactions work when using Spring and
> iBatis.  I am ultimately trying to use transactions at the Spring
> level using annotations.  I am very confused about all the different
> places for config info.
>
> This is my config BEFORE I tried to implement transactions
> applicationContext.xml
> --------------------------------
> <beans>
>     --snip out unrelated code
>
>         <!-- Pooled datasource from JNDI -->
>         <jee:jndi-lookup id="dataSource"
>                 jndi-name="/jdbc/devel"
>                 resource-ref="true" />
>
>         <!--  Web SqlMap setup for iBATIS Database Layer -->
>         <bean id="dataImportSqlMapConfig"
> class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
>                 <property name="dataSource" ref="dataSource" />
>                 <property name="configLocation">
>                         <value>classpath:dataImportSqlMapConfig.xml</value>
>                 </property>
>         </bean>
>
>         <!-- Define DAO beans-->
>         <bean id="dataImportDAOBean"
> class="intouch.dataimport.dao.impl.DataImportDAOImpl">
>                 <property name="sqlMapClient" ref="dataImportSqlMapConfig"/>
>         </bean>
> </beans>
>
> dataImportSqlMapConfig.xml
> -------------------------------------------
> <?xml version="1.0" encoding="UTF-8" ?>
> <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config
> 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
> <sqlMapConfig>
>         <sqlMap resource="sqlmaps/DataImport.xml" />
> </sqlMapConfig>
>
>
> This all worked fine.  Then I changed the configs to the following in
> an effort to implement transactions
> applicationContext.xml
> --------------------------------
>         <!-- Transaction Manager -->
>         <bean id="transactionManager"
> class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
>  >
>                 <property name="dataSource" ref="dataSource" />
>         </bean>
>
>         <!-- Tell Spring to look for beans annotated for transaction -->
>         <tx:annotation-driven/>
>
> application code
> -------------------------
>         @Transactional(propagation=Propagation.REQUIRES_NEW, readOnly=false)
>         public void runCSVDataImport() throws Exception
>         {
> ....
>
> Using this code has done *nothing* so I must be missing something.
> All the code still executes if it were not in a transaction.  I have
> debugged manually by throwing a RuntimeException (unchecked) which
> should, as the docs say, implement a rollback.
>
> So I have been trying to get into the iBatis docs to see if anything
> additional is required on the iBatis side.  I currently do not have a
> <transactionManager> (or nested <dataSource>) element in my iBatis
> config.  My first question is "are these elements required when using
> Spring transactions?".  I tested by adding it with the following to my
> iBatis config:
>     <transactionManager type="EXTERNAL">
>         <property name="SetAutoCommitAllowed" value="false"/>
>         <dataSource type="JNDI">
>             <property name="DataSource" value="java:comp/env/jdbc/devel"/>
>         </dataSource>
>     </transactionManager>
> This did not help matters, and added to my confusion because I could
> then remove the dataSource bean from my Spring config which means it
> definitely isn't using any of the Spring transaction management stuff.
>
> Are the transactionManager elements from Spring (a bean) and iBatis
> (an xml element) mutually exclusive?  If I use the Spring one do I
> need one in iBatis?
>
> Please let me know if any other information would help.
>
> Collin
>
>

RE: Transactions with iBatis and Spring

Posted by Meindert <me...@pastelebusiness.com>.
Hi Colin,

There is a description for the change from Ibatis DAO on the petstore to the
Spring framework including the transaction handling
http://opensource.atlassian.com/confluence/oss/display/IBATIS/Converting+iBA
TIS+DAO+to+Spring+DAO

Kind Regards
 Meindert

-----Original Message-----
From: Collin Peters [mailto:cadiolis@gmail.com] 
Sent: 09 June 2007 02:15 AM
To: user-java@ibatis.apache.org
Subject: Transactions with iBatis and Spring

I am trying to figure out how transactions work when using Spring and
iBatis.  I am ultimately trying to use transactions at the Spring
level using annotations.  I am very confused about all the different
places for config info.

This is my config BEFORE I tried to implement transactions
applicationContext.xml
--------------------------------
<beans>
    --snip out unrelated code

	<!-- Pooled datasource from JNDI -->
	<jee:jndi-lookup id="dataSource"
		jndi-name="/jdbc/devel"
		resource-ref="true" />
	
	<!--  Web SqlMap setup for iBATIS Database Layer -->
	<bean id="dataImportSqlMapConfig"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation">
			<value>classpath:dataImportSqlMapConfig.xml</value>
		</property>
	</bean>

	<!-- Define DAO beans-->
	<bean id="dataImportDAOBean"
class="intouch.dataimport.dao.impl.DataImportDAOImpl">
		<property name="sqlMapClient" ref="dataImportSqlMapConfig"/>
	</bean>
</beans>

dataImportSqlMapConfig.xml
-------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config
2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
	<sqlMap resource="sqlmaps/DataImport.xml" />
</sqlMapConfig>


This all worked fine.  Then I changed the configs to the following in
an effort to implement transactions
applicationContext.xml
--------------------------------
	<!-- Transaction Manager -->
	<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
 >
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- Tell Spring to look for beans annotated for transaction -->
	<tx:annotation-driven/>

application code
-------------------------
	@Transactional(propagation=Propagation.REQUIRES_NEW, readOnly=false)
	public void runCSVDataImport() throws Exception
	{
....

Using this code has done *nothing* so I must be missing something.
All the code still executes if it were not in a transaction.  I have
debugged manually by throwing a RuntimeException (unchecked) which
should, as the docs say, implement a rollback.

So I have been trying to get into the iBatis docs to see if anything
additional is required on the iBatis side.  I currently do not have a
<transactionManager> (or nested <dataSource>) element in my iBatis
config.  My first question is "are these elements required when using
Spring transactions?".  I tested by adding it with the following to my
iBatis config:
    <transactionManager type="EXTERNAL">
        <property name="SetAutoCommitAllowed" value="false"/>
        <dataSource type="JNDI">
            <property name="DataSource" value="java:comp/env/jdbc/devel"/>
        </dataSource>
    </transactionManager>
This did not help matters, and added to my confusion because I could
then remove the dataSource bean from my Spring config which means it
definitely isn't using any of the Spring transaction management stuff.

Are the transactionManager elements from Spring (a bean) and iBatis
(an xml element) mutually exclusive?  If I use the Spring one do I
need one in iBatis?

Please let me know if any other information would help.

Collin