Java Reference
In-Depth Information
<bean id= " bookShop "
class= " com.apress.springenterpriserecipes.bookshop.spring.
TransactionalJdbcBookShop " >
<property name= " dataSource " ref= " dataSource " />
<property name= " transactionManager " ref= " transactionManager " />
</bean>
</beans>
You can also have the IoC container inject a transaction template instead of creating it directly.
Because a transaction template handles all transactions, there's no need for your class to refer to the
transaction manager any more.
package com.apress.springenterpriserecipes.bookshop.spring;
...
import org.springframework.transaction.support.TransactionTemplate;
public class TransactionalJdbcBookShop extends JdbcDaoSupport implements
BookShop {
private TransactionTemplate transactionTemplate;
public void setTransactionTemplate(
TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
public void purchase(final String isbn, final String username) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
...
}
});
}
}
Then you define a transaction template in the bean configuration file and inject it, instead of the
transaction manager, into your book shop bean. Notice that the transaction template instance can be
used for more than one transactional bean because it is a thread-safe object. Finally, don't forget to set
the transaction manager property for your transaction template.
<beans ...>
...
<bean id= " transactionManager "
class= " org.springframework.jdbc.datasource.DataSourceTransactionManager " >
<property name= " dataSource " ref= " dataSource " />
</bean>
<bean id= " transactionTemplate "
class= " org.springframework.transaction.support.TransactionTemplate " >
<property name= " transactionManager " ref= " transactionManager " />
</bean>
Search WWH ::




Custom Search