The DataSourceTransactionManager bean that we have been declaring in the previous xml files, is of type of PlatformTransactionManager and we can directly use is to programmatically handle the transactions.
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userDao" class="springjdbc.transactions.programmatic.usingplatformmanager.PlatformTxManagerUserDao">
<property name="platformTransactionManager" ref="transactionManager"></property>
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
Following is a Dao class that uses PlatformTransactionManager. The code can be seen in deleteUser and selectUser method. So lets understand the deleteUser now.
DefaultTransactionDefinition. This object exposes various setter methods to configure the transaction accordingly. For eg: defaultTransactionDefinition.setReadOnly(..) etc.TransactionStatus object using the transaction definition we creataed in Step 1. Eventually this TransactionStatus will be used to commit or rollback the transaction like platformTransactionManager.commit(transactionStatus); or platformTransactionManager.rollback(transactionStatus);
public class PlatformTxManagerUserDao implements IUserDao {
private JdbcTemplate jdbcTemplate;
private PlatformTransactionManager platformTransactionManager;
public void setPlatformTransactionManager(
PlatformTransactionManager platformTransactionManager) {
this.platformTransactionManager = platformTransactionManager;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void deleteUser(final int uid) {
DefaultTransactionDefinition paramTransactionDefinition = new DefaultTransactionDefinition();
TransactionStatus status=platformTransactionManager.getTransaction(paramTransactionDefinition );
try{
String delQuery = "delete from users where id = ?";
jdbcTemplate.update(delQuery, new Object[]{uid});
platformTransactionManager.commit(status);
}catch (Exception e) {
platformTransactionManager.rollback(status);
}
}
Spring TransactionTemplate is similar to JdbcTemplate and is used to abstract away the boilerplate code from the user. It provides simple callback methods which are automatically wrapped in a transaction. It also exposes direct setter methods for configuring various properties of transactions like readOnly, isolationLevel and propagation behaviour etc. The Spring TransactionTemplate bean is initialized by providing a reference of transactionManager bean. See below(line 21)
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- Create instance of transaction template for programmatic transaction manipulation -->
<bean id="txTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userDao" class="springjdbc.transactions.programmatic.usingtxtemplate.TxTemplatedUserDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
<property name="transactionTemplate" ref="txTemplate"></property>
</bean>
The Dao class the uses TransactionTemplate can be seen in the deleteUser below. Using TransactionTemplate, we dont have to explicitly create a TransactionDefinition and Transaction object. The TransactionTemplate provides a callback method called execute where in we can add our business logic that we want to wrap in a transaction. There are two types of callback methods that we can use to wrap our code i.e TransactionCallbackWithoutResult and TransactionCallback(T). Both these variations can be seen below. The deleteUser method uses TransactionCallbackWithoutResult (line 13) and insertUser method uses TransactionCallback (line 27).
public class TxTemplatedUserDao implements IUserDao {
private JdbcTemplate jdbcTemplate;
private TransactionTemplate transactionTemplate;
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void deleteUser(final int uid) {
//use TransactionCallbackWithoutResult handler if ur query doesnt result anything
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus paramTransactionStatus) {
try{
String delQuery = "delete from users where id = ?";
jdbcTemplate.update(delQuery, new Object[]{uid});
}catch (Exception e) {
//use this to rollback exception in case of exception
paramTransactionStatus.setRollbackOnly();
}
}
});
}
public int insertUser(final User user) {
//use TransactionCallback handler if some result is returned
return transactionTemplate.execute(new TransactionCallback<Integer>() {
public Integer doInTransaction(TransactionStatus paramTransactionStatus) {
String inserQuery = "insert into users (username, password, enabled , id) values (?, ?, ?, ?) ";
Object[] params = new Object[]{user.getUserName(), user.getPassword(),user.isEnabled(),user.getId()};
int[] types = new int[]{Types.VARCHAR,Types.VARCHAR,Types.BIT,Types.INTEGER};
return jdbcTemplate.update(inserQuery,params,types);
}
});
}}
The complete code for this tutorial can be downloaded from
Download Source