springMVC+mybatis+sqlserver事务不生效
1、application.xml
<!-- 引入jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<aop:config proxy-target-class="true" />
<!--创建jdbc数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="initialSize" value="${initialSize}"></property>
<property name="maxActive" value="${maxActive}"></property>
<property name="maxIdle" value="${maxIdle}"></property>
<property name="defaultAutoCommit" value="false"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="mapperLocations" value="classpath*:com.his/**/dao/*.xml" />
</bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.his.**.dao" />
</bean>
<!-- 可通过注解控制事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
/////
controler类:
@RequestMapping(value="/saveDrugDeliveryOrDepInfo")
public String saveDrugDeliveryOrDepInfo(DrugDeliveryInfo deliveryInfo,HttpServletRequest request,
HttpServletResponse response,Model model) throws Exception
{
deliveryInfoService.addDrugToDeliveryOrDep(deliveryInfo);
return null;
}
////serviceimpl类
@Transactional
@Service("drugDeliveryInfoService")
public class DrugDeliveryInfoServiceImpl implements DrugDeliveryInfoService
{
@Autowired
private DrugDeliveryInfoMapper deliveryInfoMapper;
@Autowired
private DrugDeliveryDetailInfoMapper deliveryDetailInfoMapper;
@Override
@Transactional(propagation=Propagation.REQUIRED,rollbackFor={Error.class,RuntimeException.class,
NullPointerException.class,ClassNotFoundException.class,Exception.class})
public Integer addDrugToDeliveryOrDep(DrugDeliveryInfo drugDeliveryInfo) throws Exception{
Integer num = 0;
for (int i = 201506001; i < 201506010; i++) {
drugDeliveryInfo.setOutBound_billsNum(i);
if (i==201506005) {
drugDeliveryInfo.setOutBound_billsNum(201506003); //此处设置错值,让它抛出异常 }
num = deliveryInfoMapper.addDrugToDeliveryOrDep(drugDeliveryInfo); //此处保存,当出现异常时,前面保存的值没有回滚 }
return num;
}