@[toc]
事务管理
事务指的是DML操作,保证多个DML操作要么同时成功,要么同时失败。“增、删、改”同时成功,或同时失败就是事务。事务是保证数据的一致性的。一个逻辑工作单元要成为事务,必须满足所谓的ACID(原子性、一致性、隔离性和持久性)属性。事务是数据库运行中的逻辑工作单位,由DBMS中的事务管理子系统负责事务的处理。
案例、
不使用AOP的事务案例,使用普通的JDBC
public class Test {
private String driverClassName="com.mysql.jdbc.Driver";
private String password="1234";
private String userName="root";
private String url="jdbc:mysql://localhost:3306/ps?characterEncoding=utf-8";
private Connection coon;
/**
* 事务:保证多个DML操作要么同时成功要么同时失败
* 不满足事务的情况
* @throws Exception
*/
@org.junit.Test
public void test() throws Exception {
Class.forName(driverClassName);
coon=DriverManager.getConnection(url,userName,password);
String sql1= "insert into t_user(username,password)values('彭s','asd')";
Statement s = coon.createStatement();
s.executeUpdate(sql1);
String sql2= "insert into t_user(username,password)values1('爱是12','asd21')";
s.executeUpdate(sql2);
s.close();
coon.close();
}
}
有一条数据插入失败,但是依旧插入了一条数据进去,不满足事务的情况(多个DML同时成功或者失败)
满足事务
java/**
* 满足事务的情况
* @throws Exception
*/
@org.junit.Test
public void test1() throws Exception {
Class.forName(driverClassName);
coon=DriverManager.getConnection(url,userName,password);
//将事务自动提交关闭
coon.setAutoCommit(false);
Statement s=null;
try {
String sql1= "insert into t_user(username,password)values('11','asd')";
s = coon.createStatement();
s.executeUpdate(sql1);
String sql2= "insert into t_user(username,password)values1('b22b','asd21')";
s.executeUpdate(sql2);
//提交数据
coon.commit();
} catch (Exception e) {
//抛异常回滚
coon.rollback();
}finally {
s.close();
coon.close();
}
}
程序运行成功,但是有一个语句错误,使两个同时失败 满足事务
事务管理。配置文件的方式
dao
public interface UserDAO {
public void add();
public void update();
public void delete();
public void query1();
public void query2();
}
package com.sxt.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.sxt.dao.UserDAO;
import com.sxt.proj.User;
@Repository
public class UserDAOImpl implements UserDAO{
@Resource
private JdbcTemplate template;
@Override
public void add() {
String sql = "insert into t_user(username,password)values(?,?)";
int i = template.update(sql,"pssa","111");
System.out.println("影响的行数"+i);
}
@Override
public void update() {
String sql = "update t_user set usernamle=? ,password=? where id=?";
int i = template.update(sql,"a1663","1991",4);
System.out.println("影响的行数"+i);
}
@Override
public void delete() {
String sql = "delete from t_user where id = ?";
int i = template.update(sql,6);
System.out.println("影响的行数"+i);
}
@Override
public void query1() {
String sql = "select * from t_user";
List<User> list = template.query(sql, new BeanPropertyRowMapper<User>(User.class));
for (User user : list) {
System.out.println(user);
}
}
@Override
public void query2() {
// TODO Auto-generated method stub
}
}
service层
public interface IUserService {
public void fun1();
}
@Service
public class UserServiceImpl implements IUserService {
@Resource
private UserDAO dao;
@Override
public void fun1() {
dao.add();
dao.update();
}
}
配置文件
<?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: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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 开启扫描 -->
<context:component-scan base-package="com.sxt.*"></context:component-scan>
<!-- 配置数据源 -->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource">
<!-- 配置数据信息 -->
<property name="url"
value="jdbc:mysql://localhost:3306/ps?characterEncoding=utf-8" />
<property name="driverClassName" value=" com.mysql.jdbc.Driver" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
<!-- 配置jdbctemplate -->
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<!-- 配置注入关联数据 -->
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
<!-- 配置事务 -->
<!-- 配置事务管理器 -->
<bean
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
id="source">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务的行为 -->
<tx:advice id="txAdvice" transaction-manager="source">
<tx:attributes>
<tx:method name="fun*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 配置AOP -->
<aop:config>
<aop:pointcut expression="execution(* com.sxt.service.impl.*.*(..))"
id="myPointcut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />
</aop:config>
</beans>
测试
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
IUserService service = ac.getBean(IUserService.class);
service.fun1();
}
}
dao层实现类里有条语句出现了错误,虽然有一条语句运行成功,但是由于事务的管理 并没有插入到数据库里面
配置文件的方式
该配置文件
<?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: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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 开启扫描 -->
<context:component-scan base-package="com.sxt.*"></context:component-scan>
<!-- 配置数据源 -->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource">
<!-- 配置数据信息 -->
<property name="driverClassName" value=" com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/ps?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
<!-- 配置jdbctemplate -->
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<!-- 配置注入关联数据 -->
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
<!-- 配置事务 -->
<!-- 配置事务管理器 -->
<bean
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
id="dataSourceTransactionManager">
<!-- 关联数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
</beans>
在service实现类加一个注解
@Service
public class UserServiceImpl implements IUserService {
@Resource
private UserDAO dao;
@Transactional //该方法被spring的事务管理 好处:灵活 坏处:跟业务的耦合性加强了
@Override
public void fun1() {
dao.add();
dao.update();
}
}