数据库表Account:

导包:

    <dependencies>
<!--测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<!--连接池-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<!--日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>

tx,aop配置声名式事务:

首先编写我们的dao和service代码:

public class AccountDao extends JdbcDaoSupport {
//出账
public void rollout(String name, double money) {
String sql = "update account set money=money-? where name=?";
super.getJdbcTemplate().update(sql, money, name);
} //入账
public void shiftto(String name, double money) {
String sql = "update account set money=money+? where name=?";
super.getJdbcTemplate().update(sql, money, name);
}
}
public class AccountService {
private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
} //转账
public void transfer(String outName, String inName, double money) {
accountDao.rollout(outName, money);
//int i = 1 / 0;
accountDao.shiftto(inName, money);
}
}

添加数据库配置文件jdbc.properties:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/temp_db?useUnicode=true&characterEncoding=utf8&autoReconnect=true\
&allowMultiQueries=true
jdbc.username=root
jdbc.password=root

添加并配置spring配置文件applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!--关联jdbc配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--配置c3p0连接池-->
<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--dao和service-->
<bean id="accountDao" class="com.zy.dao.AccountDao">
<!--由于我们的AccountDao继承了JdbcDaoSupport 所以跳过jdbcTemplate 直接设置datasource-->
<property name="dataSource" ref="c3p0DataSource"></property>
</bean>
<bean id="accountService" class="com.zy.service.AccountService">
<property name="accountDao" ref="accountDao"></property>
</bean> <!--定义事务管理器(声明式的事务)-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="c3p0DataSource"></property>
</bean>
<!--tx标签配置的拦截器 底层依赖aop环绕通知 所以下面会配置aop切入点和切面-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 配置事务管理属性,TransactionDefinition -->
<!--
name 方法名
isolation 隔离级别 默认DEFAULT
propagation 传播行为 默认 REQUIRED
timeout 超时时间 默认 -1 不超时
read-only 是否只读 默认false 不是只读
rollback-for 配置一些异常,发生这些异常事务进行回滚
no-rollback-for 配置一些异常,发生这些异常,将被忽略,事务仍然进行提交
-->
<!--我们拦截的方法是转账方法 transfer-->
<tx:method name="transfer"/>
</tx:attributes>
</tx:advice> <!--配置切入点和切面-->
<aop:config proxy-target-class="false">
<aop:advisor advice-ref="txAdvice" pointcut="bean(*Service)"></aop:advisor>
</aop:config>
</beans>

测试代码:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class AccountServiceTest { @Value("#{accountService}")
AccountService accountService; @Test
public void transfer() throws Exception {
accountService.transfer("张学友", "刘德华", 100);
} }

使用@Transactional注解进行声明式事务管理:

需要加事务的方法上加上注解:

public class AccountService {
private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
} //转账
@Transactional
public void transfer(String outName, String inName, double money) {
accountDao.rollout(outName, money);
accountDao.shiftto(inName, money);
}
}

applicationContext.xml修改如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!--关联jdbc配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--配置c3p0连接池-->
<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--dao和service-->
<bean id="accountDao" class="com.zy.dao.AccountDao">
<!--由于我们的AccountDao继承了JdbcDaoSupport 所以跳过jdbcTemplate 直接设置datasource-->
<property name="dataSource" ref="c3p0DataSource"></property>
</bean>
<bean id="accountService" class="com.zy.service.AccountService">
<property name="accountDao" ref="accountDao"></property>
</bean> <!--&lt;!&ndash;定义事务管理器(声明式的事务)&ndash;&gt;-->
<!--<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">-->
<!--<property name="dataSource" ref="c3p0DataSource"></property>-->
<!--</bean>-->
<!--&lt;!&ndash;tx标签配置的拦截器 底层依赖aop环绕通知 所以下面会配置aop切入点和切面&ndash;&gt;-->
<!--<tx:advice id="txAdvice" transaction-manager="transactionManager">-->
<!--<tx:attributes>-->
<!--&lt;!&ndash; 配置事务管理属性,TransactionDefinition &ndash;&gt;-->
<!--&lt;!&ndash;-->
<!--name 方法名-->
<!--isolation 隔离级别 默认DEFAULT-->
<!--propagation 传播行为 默认 REQUIRED-->
<!--timeout 超时时间 默认 -1 不超时-->
<!--read-only 是否只读 默认false 不是只读-->
<!--rollback-for 配置一些异常,发生这些异常事务进行回滚-->
<!--no-rollback-for 配置一些异常,发生这些异常,将被忽略,事务仍然进行提交-->
<!--&ndash;&gt;-->
<!--&lt;!&ndash;我们拦截的方法是转账方法 transfer&ndash;&gt;-->
<!--<tx:method name="transfer"/>-->
<!--</tx:attributes>-->
<!--</tx:advice>--> <!--&lt;!&ndash;配置切入点和切面&ndash;&gt;-->
<!--<aop:config proxy-target-class="false">-->
<!--<aop:advisor advice-ref="txAdvice" pointcut="bean(*Service)"></aop:advisor>-->
<!--</aop:config>--> <!--增加注解版 事务管理器-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> <!--事务通知依赖于 事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="c3p0DataSource"></property>
</bean>
</beans>

ok

Spring总结十:事务案例的更多相关文章

  1. Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...

  2. Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...

  3. Spring五个事务隔离级别和七个事务传播行为

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt216 Spring五个事务隔离级别和七个事务传播行为 1. 脏读 :脏读就是 ...

  4. spring中aop事务

    一.事务 二.spring封装了事务管理代码 1.事务操作 2.事务操作对象 (1)因为在不同平台,操作事务的代码各不相同.spring提供了一个接口 (2) PlatformTransactionM ...

  5. Spring Boot(十四):spring boot整合shiro-登录认证和权限管理

    Spring Boot(十四):spring boot整合shiro-登录认证和权限管理 使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉 ...

  6. Java基础-SSM之Spring和Mybatis整合案例

    Java基础-SSM之Spring和Mybatis整合案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   在之前我分享过mybatis和Spring的配置案例,想必大家对它们的 ...

  7. Spring 框架的事务管理

    1. Spring 框架的事务管理相关的类和API PlateformTransactionManager 接口: 平台事务管理器(真正管理事务的类); TransactionDefinition 接 ...

  8. (转)使用Spring配置文件实现事务管理

    http://blog.csdn.net/yerenyuan_pku/article/details/52886207 前面我们讲解了使用Spring注解方式来管理事务,现在我们就来学习使用Sprin ...

  9. Spring框架之事务管理

    Spring框架之事务管理 一.事务的作用 将若干的数据库操作作为一个整体控制,一起成功或一起失败. 原子性:指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生. 一致性:指事务前后 ...

  10. (三)Spring框架之事务管理

    一.编程式事务管理 Spring事务管理器的接口是org.springframework.transaction.PlatformTransactionManager,事务管理器接口PlatformT ...

随机推荐

  1. Javascript+CSS实现影像卷帘效果

    用过Arcgis的筒子们对于Arcmap里面的一个卷帘效果肯定记忆很深刻,想把它搬到自己的WebGIS系统中去,抱着同样的想法,我也对这种比较炫的卷帘效果做了一下研究,吼吼,出来了,给大家汇报一下成果 ...

  2. tomcat8启动慢原因及解决办法

    tomcat8在linux下安装使用一段时间后启动非常慢,6分钟左右. 原因是一个随机数生成参数导致的. 处理如下: 修改catalina.sh .配置JRE使用非阻塞的Entropy Source ...

  3. 浅谈java使用指定字符集编码,以及常见的字符集

    问题的引入:在InputStreamReader(OutputStreamWriter)的构造方法中,有指定字符集编码,那么什么是字符集?有哪些常用的字符集?怎么用字符集进行编码? 一   什么是字符 ...

  4. c++ 基础知识 0001 const 知识2

    1.const修饰函数返回值 (1)指针传递 如果返回const data,non-const pointer,返回值也必须赋给const data,non-const pointer.因为指针指向的 ...

  5. LINUX下的ssh登录之后的文件远程copy:scp命令(接前文ssh登录)

    先记录参考: 1:http://www.cnblogs.com/peida/archive/2013/03/15/2960802.html 2:http://www.vpser.net/manage/ ...

  6. 【解题报告】[动态规划]RQNOJ PID2 / 开心的金明

    原题地址:http://www.rqnoj.cn/problem/2 解题思路:背包问题. 状态转移方程:DP[i][j]=max(DP[i-v[j]][j-1]+p[j]*v[j],DP[i][j- ...

  7. HiHoCoder1156 彩色的树(树值的记忆化ORZ+map强势出场)

    1156 : 彩色的树 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵n个节点的树,节点编号为1, 2, …, n.树中有n - 1条边,任意两个节点间恰好有一条 ...

  8. 【转】C# Socket编程(2)识别网络主机

    [转自:https://www.cnblogs.com/IPrograming/archive/2012/10/11/CSharp_Socket_2.html] 一个客户端想要发起一次通信,先决条件就 ...

  9. javabrideg的使用实践

    (1)进入这个网站http://sourceforge.net/projects/php-java-bridge/files,选择Binary package,然后选择最新的版本Php-java-br ...

  10. JMeter使用经历

    JMeter是Apache大树下的又一个果实,是一个压力测试工具,因为使用方便又开源免费,也被用来做功能测试.项目里也是拿JMeter来做功能性的接口自动化测试.这里大概说明下怎么用. 首先还是先下载 ...