springMVC+mybatis+sqlserver事务不生效

cjbedu2012 2015-07-06 04:14:18
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;

}
...全文
504 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
avichen 2015-07-21
  • 打赏
  • 举报
回复
楼主,我是这么实现的,不知道对你有没有价值 @Transactional(readOnly = false, rollbackFor = Exception.class) public int deleteUser(String id){ int user_id = Integer.parseInt(id); try { int a = this.userPropertyDao.deleteUserProperty(user_id); int b = this.userDao.deleteUser(user_id); return b; } catch (Exception e) { logger.info("updateUser" + e); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return 0; } }
cjbedu2012 2015-07-20
  • 打赏
  • 举报
回复
引用 3 楼 u012050427 的回复:
贴出 drugDeliveryInfo.setOutBound_billsNum(201506003); 抛异常的过程
这个值是个重复值,数据库表对这个值类型做了唯一处理的 if (i==201506005) { drugDeliveryInfo.setOutBound_billsNum(201506003); //此处设置错值,让它抛出异常 } num = deliveryInfoMapper.addDrugToDeliveryOrDep(drugDeliveryInfo); //此处保存,当出现异常时,前面保存的值没有回滚 } 上面的代码在保存时提示违反了唯一约束等
cjbedu2012 2015-07-20
  • 打赏
  • 举报
回复
这个值是个重复值,数据库表对这个值类型做了唯一处理的 if (i==201506005) { drugDeliveryInfo.setOutBound_billsNum(201506003); //此处设置错值,让它抛出异常 } num = deliveryInfoMapper.addDrugToDeliveryOrDep(drugDeliveryInfo); //此处保存,当出现异常时,前面保存的值没有回滚 } 上面的代码在保存时提示违反了唯一约束等
王良辰 2015-07-18
  • 打赏
  • 举报
回复
贴出 drugDeliveryInfo.setOutBound_billsNum(201506003); 抛异常的过程
cjbedu2012 2015-07-18
  • 打赏
  • 举报
回复
别沉下去了 就没人用过注解事务了????
cjbedu2012 2015-07-06
  • 打赏
  • 举报
回复
最近在做项目时,通过注解来控制事务,捉摸好久,事务遇到异常时就是不回滚。 真假希望大神指点指点

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧