什么是事物:简单来说一个Session中所进行所有的操作,要么同时成功,要么同时失败
ACID — 数据库事务正确执行的四个基本要素
包含:
- 原子性(Atomicity)
- 一致性(Consistency)
- 隔离性(Isolation)
- 持久性(Durability)
一个支持事务(Transaction)中的数据库系统,必需要具有这四种特性,否则在事务过程(Transaction processing)当中无法保证数据的正确性,交易过程极可能达不到交易
举个简单例子:A向B转账,转账这个流程中如果出现问题,事务可以让数据恢复成原来一样【A账户的钱没变,B账户的钱也没变】
事务隔离级别
数据库定义了4个隔离级别:
- Serializable【可避免脏读,不可重复读,虚读】
- Repeatable read【可避免脏读,不可重复读】
- Read committed【可避免脏读】
- Read uncommitted【级别最低,什么都避免不了】
事物在springmvc中的应用
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<context:component-scan base-package="com.baidu.test.*"></context:component-scan>
<!-- 数据连接池 dbcp连接池-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 不需要定义SqlSessionFactory,注意configLocation当您使用MapperFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 自动扫描doamin目录, 省掉Configuration.xml里的手工配置 -->
<property name="typeAliasesPackage" value="com.baidu.test.domain" /><!--
以类名当别名 -->
<property name="dataSource" ref="dataSource" />
<!-- <property name="configLocation" value="classpath:mybatis-config.xml"
/> -->
<!-- 载入mapper.xml文件 -->
<property name="mapperLocations" value="classpath:com/baidu/test/mapper/*Mapper.xml" />
</bean>
<!-- 扫描映射器 Mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
<!-- 代替mybatis中的载入类 <mappers> <mapper resource="com/baidu/test/mapper/BookMapper.xml"/> </mappers> -->
<property name="basePackage" value="com.baidu.test.mapper" />
<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
</bean>
<!-- 事务处理 -->
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
BookMapper
@Service
public class BookService {
@Autowired
private BookMapper bookMapper;
@Transactional
public void save(Book book,Book book1) {
bookMapper.save(book1);
bookMapper.save(book);
}
}
BookMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.baidu.test.mapper.BookMapper">
<insert id="save" parameterType="Book">
insert into book
(bookid,bookname,bookprice)
values
(#{bookid},#{bookname},#{bookprice})
</insert>
</mapper>