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<[email protected]> 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
>