数据源事务管理器
当使用jdbc方式管理事务时采用DataSourceTransactionMananger
发现在图书管理案例中,当余额不足时,理应报错,但书本的数量却减少了,破坏了事务的一致性,需要添加事务管理器
<!-- 配置事务管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启注解驱动,即对事务相关的注解进行扫描,解析含义并执行功能 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/><!-- 事务管理器要保持一致! -->
@Service
public class BookServiceImpl implements BookService{
@Autowired
private BookDao dao;
/**
* @Transactional:对方法中所有的操作作为一个事务管理
* 在方法上使用,只对方法有效果
* 在类上使用,对类中所有的方法都有效果
*/
@Transactional
public void buyBook(String bid, String uid) {
Integer price = dao.selectPrice(bid);
dao.updateSt(bid);
dao.updateBalance(uid, price);
}
}
事务的传播性
在业务逻辑层添加一个接口 购买多本书籍
public interface Cashier {
void checkOut(String uid,List<String> bids);
}
定义实现类
@Service
public class CashierServiceImpl implements Cashier{
@Autowired
private BookService service;
public void checkOut(String uid, List<String> bids) {
for (String bid : bids) {
service.buyBook(bid, uid);
}
}
}
控制层添加方法
@Controller
public class BookController {
@Autowired
public Cashier cashier;
public void checkOut(){
List<String> bids = new ArrayList<>();
bids.add("1");
bids.add("2");
cashier.checkOut("1001", bids);
}
}
测试类
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("book.xml");
BookController controller = ac.getBean("bookController", BookController.class);
controller.checkOut();
}
}
在测试时发现当余额不足支付全部商品时,会购买能支付的,然后再报错 因为在业务逻辑层实现类未添加事务管理器@Transactional
正确写法:
@Service
@Transactional
public class CashierServiceImpl implements Cashier{
@Autowired
private BookService service;
public void checkOut(String uid, List<String> bids) {
for (String bid : bids) {
service.buyBook(bid, uid);
}
}
}
//在实现类上添加事务管理器的注解@Transactional
/*
propagation:A方法和B方法都有事务,当A在调用时,会将A中的事务传播给B方法,B方法对于事务的处理方式就是事务的传播行为
Propagation.REQUIRED:必须使用调用者的事务
Propagation.REQUIRES_NEW:将调用者的事务挂起,不使用调用者的事务,使用新的事务进行处理
*/
@Transactional(propagation=Propagation.REQUIRES_NEW)
public void buyBook(String bid, String uid) {
Integer price = dao.selectPrice(bid);
dao.updateSt(bid);
dao.updateBalance(uid, price);
}
@Transactional中可以设置的属性:
ioslation:事务的隔离级别,在并发的情况下,操作数据的一种规定
* 读未提交:脏读
* 读已提交:不可重复读
* 可重复读:幻读
* 串行化:性能低,消耗大
通过xml的方式来实现事务管理 修改xml文件
<!-- 配置事务管理器,不管是用注解方式或xml方式配置事务,一定要有DataSourceTransactionManager事务管理器的支持 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务通知 -->
<tx:advice id="tx" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<!-- 在设置好的切入点表达式下再次进行事务设置 -->
<tx:method name="buyBook"/>
<tx:method name="checkOut"/>
<!-- 只有select开头的方法才能被事务处理 -->
<tx:method name="select*" read-only="true"/>
<tx:method name="insert*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置切入点表达式 -->
<aop:config>
<aop:pointcut expression="execution(* com.atguigu.book_xml.service.impl.*.*(..))" id="pointCut"/>
<aop:advisor advice-ref="tx" pointcut-ref="pointCut"/>
</aop:config>