私は、JDK1.6.0_21を使用するApacheTomcat6.0.29で実行されているmybatis-3.0.2およびmybatis-spring-1.0.0とともにspring3.0.3.RELEASEを使用しています。
DAOクラスとServiceクラスを作成し、宣言型トランザクション制御に従って定義しました-
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="dtxops"
expression="execution(* com.project.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="dtxops" />
</aop:config>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
このメソッドは、ItemDAOを使用するcom.project.service.ItemDAOServiceImplクラスにあります。SystemExceptionはRunTimeExceptionです。削除する2つのIDを渡します。1つのIDはシステムに存在し、もう1つは存在しません。1つのIDが存在しないため、SystemExceptionが発生しますが、データベースを確認すると、ロールバックではなく、もう1つのIDが削除されます。
public void deleteItem(List<Integer> itemIds) {
for (int itemId : itemIds) {
try {
int result = itemDAO.delete(itemId);
if (result != 1) {
throw new SystemException(
"Failed to delete item");
}
} catch (DataAccessException dae) {
log.error("Failed to delete item", dae);
throw new SystemException("Failed to delete items");
}
}
}