如何使用@Transactional?

Even713 2010-08-08 05:43:47
要在controller层加事务

public ModelAndView updateFunction(HttpServletRequest request, HttpServletResponse response) throws IOException{
....................// Collect info for form1
Service.saveForm1(Form1);
....................// Collect info for form2
Service.saveForm2(Form2);
....................
}

调用两个service的方法去保存不同的form,为了一致性,希望在这里加上事务控制,要成功就一起成功,失败的话就回滚,一个也不保存。

如何在这个案例中使用@Transactional,设置哪些属性?不需要在xml文件中配置什么吧?
不要劝我将事务放在数据库或back-end。我这边局限着,只能在controller层加事务。
我很菜的,啥都不懂,请多包涵!

参考:http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html


Table 9.3. @Transactional properties
Property Type Description
propagation enum: Propagation optional propagation setting
isolation enum: Isolation optional isolation level
readOnly boolean read/write vs. read-only transaction
timeout int (in seconds granularity) the transaction timeout
rollbackFor an array of Class objects, which must be derived from Throwable an optional array of exception classes which must cause rollback
rollbackForClassname an array of class names. Classes must be derived from Throwable an optional array of names of exception classes that must cause rollback
noRollbackFor an array of Class objects, which must be derived from Throwable an optional array of exception classes that must not cause rollback.
noRollbackForClassname an array of String class names, which must be derived from Throwable an optional array of names of exception classes that must not cause rollback
...全文
583 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
liufeng0209 2010-08-12
  • 打赏
  • 举报
回复
建议你把事务放在service层实现,而非control层,多写个接口方法吧
libRA_kin 2010-08-10
  • 打赏
  • 举报
回复
可以加 不过你的数据库的表要支持事务 mysql INNODB格式的表 支持事务
strawren 2010-08-10
  • 打赏
  • 举报
回复
@Transactional
public ModelAndView updateFunction(HttpServletRequest request, HttpServletResponse response) throws IOException{
....................// Collect info for form1
Service.saveForm1(Form1);
....................// Collect info for form2
Service.saveForm2(Form2);
....................
}

在你的xml里添加如下
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
Even713 2010-08-09
  • 打赏
  • 举报
回复
没人知道吗?这个很冷门么?
跑跑鱼 2010-08-08
  • 打赏
  • 举报
回复
菜鸟路过,帮顶

81,094

社区成员

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

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