Spring事务管理案例
AccountServiceImpl
public class AccountServiceImpl implements AccountService {private AccountDao accountDao;public AccountDao getAccountDao() {return accountDao;}//在AccountServiceimpl中注入AccountDao,要提供set方法//配置中有<property name="accountDao" ref="accountDao"></property>public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}@Overridepublic void account(String outName, String inName, double money) {// TODO Auto-generated method stubaccountDao.accountOut(outName, money);//设置异常int i = 10/0;accountDao.accountIn(inName, money);}}
AccountDao
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {//AccountDaoImpl继承了JdbcDaoSupport,所以底层会自动创建一个JdbcTemplate
//所以这里选择用this.getJdbcTemplate来获取JdbcTemplate@Overridepublic void accountIn(String inName, double money) {// TODO Auto-generated method stubthis.getJdbcTemplate().update("update account set money = money+? where name = ?",money,inName);}@Overridepublic void accountOut(String outName, double money) {// TODO Auto-generated method stubthis.getJdbcTemplate().update("update account set money = money-? where name = ?",money,outName);}}
Test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class AccountTest {@Autowired//(将applicationContext中的accountService注入)private AccountService accountService;@Testpublic void test1() {accountService.account("tom", "fox", 500);}}
applicationContext.xml
<bean id="c3p0dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql:///springtest"/><property name="user" value="root"/><property name="password" value="root"></property></bean><!--(配置AccountServiceImpl) --><bean id="accountService" class="service.AccountServiceImpl"><!-- 注入dao,此时需要在AccountServiceImplements中为accountDao创建set方法 --><property name="accountDao" ref="accountDao"></property></bean><!-- 配置AccountDaoImpl --><bean id="accountDao" class="dao.AccountDaoImpl"><!-- 注入dataSource,由于AccountDaoImpl继承了JdbcDaoSupport,所以底层会自动创建一个JdbcTemplate --><property name="dataSource" ref="c3p0dataSource"></property></bean><!-- 配置事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="c3p0dataSource"></property></bean><!-- 配置通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- name:必须的,对哪些方法进行事务控制isolation 可选 设置事务隔离级别 默认是DEFAULT propagation:可选 设置事务传播 默认值 REQUIREDtimeout 可选 超时时间 默认值-1 read-only 可选 默认值是false 如果不是只读,它可以对insert update delete操作,如果是只读不可以。rollback-for 可选 可以设置一个异常,如果产生这个异常,触发事务回滚no-rolback-for 可选 可以设置一个异常,如果产生这个异常,不会触发事务回滚--><tx:method name="account"/></tx:attributes></tx:advice><!-- 配置切面 --><aop:config><aop:pointcut expression="execution(* service.AccountService.account(..))" id="txPointCut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/></aop:config><!-- 配置采用注解方式 --><tx:annotation-driven transaction-manager="transactionManager"/> <!-- 注入dataSource,由于AccountDaoImpl继承了JdbcDaoSupport,所以底层会自动创建一个JdbcTemplate,这里便不用再配置 --><!-- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="c3p0dataSource"/></bean> -->
问题:关于xml方式与annotation方式进行事务管理的优缺点?
1.从简单上来说,使用注解更方便。
2.使用配置的方案,可以对事务配置进行集中维护:
<tx:method name="get*" />
<tx:method name="save*" />
Spring事务管理案例相关推荐
- Spring事务管理的demo
事务是逻辑上的一组操作,这组操作要么全部成功,要么全部失败,最为典型的就是银行转账的案例: A要向B转账,现在A,B各自账户中有1000元,A要给B转200元,那么这个转账就必须保证是一个事务,防止中 ...
- Spring 事务管理的详细讲解及使用
文章目录 一. Spring 事务管理简介 二.Spring 事务管理器 1.Spring 事务管理接口 1.1. PlatformTransactionManager 接口 1.2. Transac ...
- Spring事务管理 与 SpringAOP
1,Spring事务的核心接口 Spring事务管理的实现有许多细节,如果对整个接口框架有个大体了解会非常有利于我们理解事务,下面通过讲解Spring的事务接口来了解Spring实现事务的具体策略. ...
- 什么是事务的传播_这么漂亮的Spring事务管理详解,你不来看看?
事务概念回顾 什么是事务? 事务是逻辑上的一组操作,要么都执行,要么都不执行. 事物的特性(ACID): 原子性: 事务是最小的执行单位,不允许分割.事务的原子性确保动作要么全部完成,要么完全不起作用 ...
- SPRING 事务管理说明
spring 事务管理是通过AOP拦截指定的方法,进行事务管理. 事务配置 <aop:config proxy-target-class="true"><aop: ...
- Spring 事务管理高级应用难点剖析
Spring 事务管理高级应用难点剖析: 第 1 部分 http://www.ibm.com/developerworks/cn/java/j-lo-spring-ts1/index.html htt ...
- spring事务管理 TransactionProxyFactoryBean源码分析
J2EE,当然离不开事务,事务又当然少不了Spring声明式事务.spring声明式事务,很多码农门,应该和笔者一样,停留在使用上,及仅仅了解点原理.如:Spring事务管理原理"代理+AO ...
- Spring 事务管理高级应用难点剖析--转
第 1 部分 http://www.ibm.com/search/csass/search/?q=%E4%BA%8B%E5%8A%A1&sn=dw&lang=zh&cc=CN& ...
- Spring事务管理(详解+实例)
写这篇博客之前我首先读了<Spring in action>,之后在网上看了一些关于Spring事务管理的文章,感觉都没有讲全,这里就将书上的和网上关于事务的知识总结一下,参考的文章如下: ...
最新文章
- Flask之请求钩子
- 算法--------------整数反转
- oa php mysql_PHP+MYSQL的OA为何没有Java的值钱?
- [转]div里table居中的问题 Div与body顶部间隙
- js轮播图片小圆点变化_原生js实现轮播图(两种方法)
- 回顾 | 进击吧! Blazor !第三期 信息交互
- 不用第三方插件如何统计自己wordpress的访问量
- c语言第4份实验报告,C语言实验报告04.doc
- Python面试必须要看的15个问题
- 计算机术语中英文对照表(流水线/微架构/体系结构/指令集)
- 一图总结:软件测试原则|策略|模型|生命周期
- fianl属性 java_Java反射如何有效的修改final属性值详解
- Linux 下Nginx+Tomcat 完美整合 nginx tomcat 整合方式
- IntelliJ IDEA如何去掉xml文件背景色
- 工科数学分析之数学感悟
- Oracle如何创建数据库
- 帮你举例说明什么是Python鸭子类型
- IDEA怎样自定义 Touch Bar
- 必知之vs2019添加外加库文件操作
- 图片轮播器——javascript