什么是事务?
单个逻辑单元处理的一系列功能 ,要么同时成功,要么同时失败
事务特点:
1.原子性 一个事务不可分隔
2.持久性 1个事务执行完毕后 ,数据持久化到 数据库(硬盘)
3.隔离性 1个事务的执行不受另外1个事务的影响
4.一致性 数据一致性 (事务执行完毕后,数据的一致性)
不考虑隔离性引发的问题 ?
1.数据的读取问题 (数据库设置有关)
脏读: 1个事务读取到另外1个事务未提交的数据
虚读/幻读:1个事务读取另外1个事务 insert的数据 , 1个事务中多次查询结果不一样
不可重复读: 1个事务读取另外1个事务 update的数据 , 1个事务中多次查询结果不一样
2.数据写的问题
导致数据丢失 (需要程序员处理)
解决读的问题— 数据库隔离级别
- Read uncommitted 读未提交 – 不能解决任何读的问题
- Repeatable read 可重复读 – 可以解决"脏读/不可重复读" , 虚读/幻读有可能会发生 (mysql的默认级别)
- Read committed 读已提交 – 解决"脏读和虚读/幻读", 不可重复读可能发生 . (oracle和 sqlserver默认级别)
- SERIALIZABLE 序列化 – 解决所有读的问题 ,(oracle支持)
**事务可以使用xml或者注解两种方式,下面使用事务简单写个转账
**
实体类:
public class User {
Integer id;
String name;
Double money;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
}
jdbc.properties:
# 一般都会将数据库配置放到此处
jdbc.driverClassName =com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///mydb1?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
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: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
https://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 https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--=================注解配置aop====================-->
<!--配置动态代理 ,支持aop的注解-->
<aop:aspectj-autoproxy/>
<!--开启注解扫描-->
<context:component-scan base-package="com.ld"/>
<!--读取jdbc配置文件-->
<context:property-placeholder location="jdbc.properties"/>
<!--c3p0设置配置信息-->
<bean id="dataSource" 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>
<!--配置jdbcTemplate模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--==============xml事务管理 (优势,只写1次,永久生效)===============-->
<!--1.配置事务管理器平台 (无论是xml还是注解都需这个配置)-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--2.配置事务通知 , id :事务通知的名称 ,随意给名-->
<tx:advice id="myAdvice">
<tx:attributes>
<!--name是 匹配规则: -->
<!--<tx:method name="transfer"/>
<tx:method name="find*"/>
<tx:method name="delete*"/>
<tx:method name="add*"/>
<tx:method name="save*"/>-->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!--3.aop的配置-->
<aop:config>
<!--表达式 指定 事务作用于哪一层-->
<aop:pointcut id="pointcut1" expression="execution(* com.ld.service.impl.UserServiceImpl.*(..))"/>
<!--<aop:pointcut id="pointcut1" expression="execution(* cn.hp.dao.*.*(..))"/>-->
<!--advisor 和 aspect都是切面 , 前者一般只配置1个 切入点-->
<aop:advisor advice-ref="myAdvice" pointcut-ref="pointcut1"/>
</aop:config>
</beans>
业务层:
public interface UserService {
/**
* 从from 转给to 多少money
* @param from
* @param to
* @param money
*/
void transfer(String from,String to,double money);
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Override
public void transfer(String from, String to, double money) {
userDao.out(from,money);
System.out.println(1/0);
userDao.in(to,money);
}
}
持久层:
public interface UserDao {
/**
* 转出多少钱
* @param name
* @param money
*/
void out(String name,double money);
/**
* 接收多少钱
* @param name
* @param money
*/
void in(String name,double money);
}
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public void out(String name, double money) {
jdbcTemplate.update("update user set money=money-? where name=?",money,name);
}
@Override
public void in(String name, double money) {
jdbcTemplate.update("update user set money=money+? where name=?",money,name);
}
}
测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class UserTest {
@Autowired
UserService userService;
@Test
public void test1(){
userService.transfer("张三","李四",200);
}
执行结果:
这是添加了事务,就如上面说的,要么同时成功,要么同时失败,下面我把事务注释掉,在看下结果:
可以看出,如果不添加事务,张三的钱虽然减少200,李四的钱却没有增加
上面使用xml配置了事务,当然还可以使用注解来配置,只需修改两个位置,来看下面:
applicationContext2.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
https://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 https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--=================注解配置aop====================-->
<!--配置动态代理 ,支持aop的注解-->
<aop:aspectj-autoproxy/>
<!--开启注解扫描-->
<context:component-scan base-package="com.ld"/>
<!--读取jdbc配置文件-->
<context:property-placeholder location="jdbc.properties"/>
<!--c3p0设置配置信息-->
<bean id="dataSource" 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>
<!--配置jdbcTemplate模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--==============xml事务管理 (优势,只写1次,永久生效)===============-->
<!--1.配置事务管理器平台 (无论是xml还是注解都需这个配置)-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--2.开启事务注解配置-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
业务层:
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Transactional //事务注解,可加在类上,也可加在方法上
@Override
public void transfer(String from, String to, double money) {
userDao.out(from,money);
System.out.println(1/0);
userDao.in(to,mon**ey);
}
测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext2.xml")
public class UserTest2 {
@Autowired
UserService userService;
@Test
public void test1(){
userService.transfer("张三","李四",200);
}
}
这就是使用注解来配置事务的操作了