spring mvc高手进了,关于注解事务执行多条sql语句的问题

weblogicssl 2011-12-21 04:35:37
代码如下:
@Service
public class JoyServiceImpl implements JoyService {


@Autowired
private BaseDao baseDao;

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void method_upd(String str) {

String sql_1="select * from tab1";
baseDao.queryForList(sql_1);

String sql_2="update tab2 set name='"+str+"' ";
baseDao.update(sql_2);

String sql_3="delete from tab3 where name='"+str+"'";
baseDao.update(sql_3);

String sql_4="insert into tab4 (name) values ('"+str+"')";
baseDao.update(sql_4);

}
在执行此方法的时候,如果其中一条语句执行错误,则其他语句回滚
但是我在测试的时候,一条语句出现错误,其他还照样执行
请问是怎么回事,在线等!
...全文
744 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
斗者_2013 2014-04-01
  • 打赏
  • 举报
回复
1、要将 <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> 这句话配置在spring mvc的总配置文件中,即XXX-servlet.xml。而不是放在application.xml中。 2.确实是没有抛出异常的问题,一定要在业务处理方法中抛出异常,不然也不会回滚。
liu119361940 2012-12-21
  • 打赏
  • 举报
回复
帮顶。。。。。。。等高手回答。。
weblogicssl 2012-06-20
  • 打赏
  • 举报
回复
难道没有人会吗?自己顶
weblogicssl 2012-06-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />

加了这个应该可以了
[/Quote]
我加了,不管用
wshcdr 2012-06-13
  • 打赏
  • 举报
回复
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />

加了这个应该可以了
roycxshun 2012-06-13
  • 打赏
  • 举报
回复
你需要把你那些代码放在try里面,然后重新抛出异常
如下:

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void method_upd(String str) {

try{
String sql_1="select * from tab1";
baseDao.queryForList(sql_1);

String sql_2="update tab2 set name='"+str+"' ";
baseDao.update(sql_2);

String sql_3="delete from tab3 where name='"+str+"'";
baseDao.update(sql_3);

String sql_4="insert into tab4 (name) values ('"+str+"')";
baseDao.update(sql_4);
} catch(Exception e){
throw e;//注意,这里必须是重新抛出,让spring来处理,不然是不会rollback的。
}
}
weblogicssl 2012-06-13
  • 打赏
  • 举报
回复
spring 注解 可不可以用三层架构来写程序
weblogicssl 2011-12-22
  • 打赏
  • 举报
回复
我已经设置了,
代码如下:

<!-- 使用注解方式配置事务 -->
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>

<!-- 使用 Spring事务管理器 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="c3p0DataSource" />
</bean>
我用的是注解事物方式,可是还不行
huangxw000 2011-12-22
  • 打赏
  • 举报
回复
baseDao里面的操作要求是同一库,而且里面不要catch异常,让它抛给Service。
weblogicssl 2011-12-22
  • 打赏
  • 举报
回复
自己顶
行舟 2011-12-21
  • 打赏
  • 举报
回复
你的事物没有配置。
spring可以利用aop来配置事物,注意要配置在你的service层,并非dao层

81,122

社区成员

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

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