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 Gwyn Evans <gw...@gmail.com> on 2009/06/08 18:22:31 UTC

Adding programable transactions to a Spring/Ibatis setup?

Hi,
Does anyone have any guides for adding programmable transactions to a
Spring/Ibatis setup?

I've got my 'normal' setup using Spring, Ibatis & whatever you get if
you don't explicitly configure any transactional behavior.  That's
been working fine, but I've got one method that's going to need to do
a block of inserts & I'd like to avoid loading them in as single
transactions, so I was looking to see what the options might be.  I'm
not sure that I'd be able to add AOP support, so was looking at
programmable transactions but I've not been able to really work out
what's needed in the config.xml and/or the sqlMapConfig.xml order to
add them for the one method - can anyone provide some help/pointers,
please!

/Gwyn

Re: Adding programable transactions to a Spring/Ibatis setup?

Posted by hernan gonzalez <hg...@gmail.com>.
I copy below my stripped code/configuration for programmatic
transactions with ibatis+spring .

Bear in mind that (besides the ugly static methods) this setup is
rather limited,
I assume my beginTransation() always joins the outer transaction, if it exists.
Besides, in a web-app environment, it would be good idea (to prevent
bugs with unfinished transactions) to collect the obtained
PlatformTransactionManager objects in a threadlocal variable, and do
some checking-cleaning-debugging at the ending of the thread (with a
servlet-filter).


------------  spring.xml:
-------------------------------------------------------

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

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

------------ and some static methods in DbUtil class
-------------------------------------------------------

public static TransactionStatus beginTransaction() {
 PlatformTransactionManager txMan = getTxManager();
 TransactionStatus status =
txMan.getTransaction(getDefaultTransactionDefinition());
 //TransactionsThreadLocal.add(status); // collect for bookkeeping /
cleaning in filter
 return status;
}

public static void commit(TransactionStatus status) {
 getTxManager().commit(status);
}

public static void rollback(TransactionStatus status) {
 getTxManager().rollback(status);
}

private static DefaultTransactionDefinition getDefaultTransactionDefinition() {
 DefaultTransactionDefinition td = new DefaultTransactionDefinition();
 td.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
 return td;
}

----------------------------------------

Then your code you do something like

TransactionStatus tx = Db.beginTransaction();
try {
       ... do something
 	Db.commit(tx);
     } catch(Exception e) {
	 Db.rollback(tx);
	 throw e;
     }
-------------------------------------------

Hope this helps
Best regards

Hernán J. González
Argentina

On Mon, Jun 8, 2009 at 1:22 PM, Gwyn Evans<gw...@gmail.com> wrote:
> Hi,
> Does anyone have any guides for adding programmable transactions to a
> Spring/Ibatis setup?
>
> I've got my 'normal' setup using Spring, Ibatis & whatever you get if
> you don't explicitly configure any transactional behavior.  That's
> been working fine, but I've got one method that's going to need to do
> a block of inserts & I'd like to avoid loading them in as single
> transactions, so I was looking to see what the options might be.  I'm
> not sure that I'd be able to add AOP support, so was looking at
> programmable transactions but I've not been able to really work out
> what's needed in the config.xml and/or the sqlMapConfig.xml order to
> add them for the one method - can anyone provide some help/pointers,
> please!
>
> /Gwyn
>

Re: Adding programable transactions to a Spring/Ibatis setup?

Posted by Gwyn Evans <gw...@gmail.com>.
Thanks, both, but I'm still unclear on the part where I actually
obtain the transactionManager - do I need to explicitly DI it into the
DaoImpl bean in the context.xml, or am I missing something somewhere?

/Gwyn

On Mon, Jun 8, 2009 at 6:31 PM, Alex Shneyderman<a....@gmail.com> wrote:
> take a look at the source code of this class:
>
> http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/test/AbstractTransactionalSpringContextTests.html
>
> it has plenty of clues on how to do this.
>
> Cheers,
> Alex.
>
> On Mon, Jun 8, 2009 at 6:22 PM, Gwyn Evans<gw...@gmail.com> wrote:
>> Hi,
>> Does anyone have any guides for adding programmable transactions to a
>> Spring/Ibatis setup?
>>
>> I've got my 'normal' setup using Spring, Ibatis & whatever you get if
>> you don't explicitly configure any transactional behavior.  That's
>> been working fine, but I've got one method that's going to need to do
>> a block of inserts & I'd like to avoid loading them in as single
>> transactions, so I was looking to see what the options might be.  I'm
>> not sure that I'd be able to add AOP support, so was looking at
>> programmable transactions but I've not been able to really work out
>> what's needed in the config.xml and/or the sqlMapConfig.xml order to
>> add them for the one method - can anyone provide some help/pointers,
>> please!
>>
>> /Gwyn
>>
>

Re: Adding programable transactions to a Spring/Ibatis setup?

Posted by Alex Shneyderman <a....@gmail.com>.
take a look at the source code of this class:

http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/test/AbstractTransactionalSpringContextTests.html

it has plenty of clues on how to do this.

Cheers,
Alex.

On Mon, Jun 8, 2009 at 6:22 PM, Gwyn Evans<gw...@gmail.com> wrote:
> Hi,
> Does anyone have any guides for adding programmable transactions to a
> Spring/Ibatis setup?
>
> I've got my 'normal' setup using Spring, Ibatis & whatever you get if
> you don't explicitly configure any transactional behavior.  That's
> been working fine, but I've got one method that's going to need to do
> a block of inserts & I'd like to avoid loading them in as single
> transactions, so I was looking to see what the options might be.  I'm
> not sure that I'd be able to add AOP support, so was looking at
> programmable transactions but I've not been able to really work out
> what's needed in the config.xml and/or the sqlMapConfig.xml order to
> add them for the one method - can anyone provide some help/pointers,
> please!
>
> /Gwyn
>