1.使用XML方式配置事务
配置步骤:
【第一步】、把之前com.zzc下的子包全都粘贴复制到com.zzc.xml包下,并去掉里面添加的注解
【第二步】、添加spring配置文件applicationContext-tx-xml.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd">
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置c3p0数据源 -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- 配置spring中的jdbcTemplate -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置bean -->
<bean id="bookShopDao" class="com.zzc.xml.dao.impl.BookShopDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="bookShopService" class="com.zzc.xml.service.impl.BookShopServiceImpl">
<property name="bookShopDao" ref="bookShopDao"></property>
</bean>
<bean id="cashierService" class="com.zzc.xml.service.impl.CashierServiceImpl">
<property name="bookShopService" ref="bookShopService"></property>
</bean>
</beans>
【第三步】、测试
TxTest.java
public class TxTest {
private ApplicationContext ctx = null;
private BookShopDao bookShopDao = null;
private BookShopService bookShopService = null;
{
ctx = new ClassPathXmlApplicationContext("applicationContext-tx-xml.xml");
bookShopDao = (BookShopDao) ctx.getBean("bookShopDao");
bookShopService = (BookShopService) ctx.getBean("bookShopService");
}
@Test
public void test02() {
bookShopService.purchase("zzc", 1);
}
}
测试当用户余额不够买一本书时的结果。
测试之前:
- 账户余额
- 书的库存
测试之后:
账户余额没变化,但1号书的库存减一,这不符合现实生活
于是,给它配置事务
【第四步】、配置事务
修改applicationContext-tx-xml.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置c3p0数据源 -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- 配置spring中的jdbcTemplate -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置bean -->
<bean id="bookShopDao" class="com.zzc.xml.dao.impl.BookShopDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="bookShopService" class="com.zzc.xml.service.impl.BookShopServiceImpl">
<property name="bookShopDao" ref="bookShopDao"></property>
</bean>
<bean id="cashierService" class="com.zzc.xml.service.impl.CashierServiceImpl">
<property name="bookShopService" ref="bookShopService"></property>
</bean>
<!-- 1.配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2.配置事务属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- 3.配置事务切入点,以及把事务切入点和事务属性关联起来 -->
<aop:config>
<aop:pointcut id="txPointCut"
expression="execution(* com.zzc.xml.service.BookShopService.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
</aop:config>
</beans>
- 配置事务管理器
- 配置事务属性
- 配置事务切入点
再次运行上述方法,用户购买不成功,账户余额不变化,书的库存量也没减一
【第五步】、测试购买2本书
有2本书,账户余额只够买其中的一本书,用户执行此操作后,结果如何?
修改applicationContext-tx-xml.xml
<!-- 3.配置事务切入点,以及把事务切入点和事务属性关联起来 -->
<aop:config>
<aop:pointcut id="txPointCut"
expression="execution(* com.zzc.xml.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
</aop:config>
要调用CashierServiceImpl类里面的checkOut()方法
结果时,购买不成功。余额不变,书的库存量也不变
如何让用户购买成功呢?也就是说,只让用户购买其中的一本
【第六步】、修改applicationContext-tx-xml.xml
<!-- 2.配置事务属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 配置根据方法名执行事务的属性-->
<tx:method name="purchase" propagation="REQUIRES_NEW" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
- 配置根据方法名执行事务的属性
- purchase():此方法执行时,必须开启一个新事务
还可以添加一些其他方法属性,如:
<!-- 2.配置事务属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="purchase" propagation="REQUIRES_NEW" />
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" />
</tx:attributes>
</tx:advice>