什么是事务
事务是数据库操作最基本单元,逻辑上一组操作,要么都成功,如果有一个失败所有操作都失败。
事务的特性:
- 1,原子性:过程中要么都成功,要么都失败
- 2,一致性:操作前后,总量不变
- 3,隔离性:操作之间不产生影响
- 4,持久性
Spring进行事务管理的两种方式:
- 1,编程式事务管理
- 2,声明式事务管理(使用)
声明式事务管理实现方式:
- 1,基于注解方式(使用)
- 2,基于xml配置文件方式
底层原理:
Spring进行声明式事务管理,底层使用AOP原理
基于注解的实现步骤:
- 1,在Spring配置文件中创建事务管理器
- 2,在Spring配置文件中开启事务注解
- 3,在Service类上面添加事务注解
可以添加到类上边,也可以添加到方法上边
在注解里面可以配置事务的相关参数:
- 1,propagation:事务船舶行为
- 2,isolation:事务隔离级别
- 3,timeout:超时时间
- 4,readOnly:是否只读
- 5,rollbackFor:回滚
- 6,noRollbackFor:不回滚
实现例子:
先说明以下什么是事务:
当以下代码执行时,a等15,后,系统就会报错,就停止往下运行。也就是说,假设这是一个数据库系统,我们原本a应该要等于23的,但是最后只是等于15,所以,我们觉得要么执行完,要么就之前的操作一起无效。
@Test
public void test(){
int a=10;
a=a+5;
int b=10/0;
a=a+8;
}
Dao:
public interface FourDao {
public void testMethod(int account,String inputName,String outputName);
}
Service:
我们习惯性的吧,事务放在Service层,往类上面添加则表示该类的所有方法都添加了事务
@Service
@Transactional
public class FourService {
@Autowired
private FourDao fourDao;
public void method(int account, String inputName, String outputName){
fourDao.testMethod(account,inputName,outputName);
}
}
Dao实现类:
@Repository
public class FourDaoImpl implements FourDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void testMethod(int account, String inputName, String outputName) {
String sql1="update book set loginpassword=loginpassword-? where username=?";
int update = jdbcTemplate.update(sql1, account, outputName);
int aa=10/0;
String sql2="update book set loginpassword=loginpassword+? where username=?";
int update1 = jdbcTemplate.update(sql2, account, inputName);
System.out.println(update+"----"+update1);
}
}
Spring配置文件:
<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: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">
<!--开启注解扫描-->
<context:component-scan base-package="four"></context:component-scan>
<!--直接配置连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--数据库驱动-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<!--数据库地址 + 数据库名字-->
<property name="url" value="jdbc:mysql://localhost:3306/userdata"></property>
<!--连接数据库用户名-->
<property name="username" value="root"></property>
<!--连接数据库密码-->
<property name="password" value="caonimabi555"></property>
</bean>
<!--JdbcTemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入DataSource-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--创建事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>