天天看点

快马加鞭学习spring的声明式事务管理第一篇,xml配置spring声明式事务

事务管理是一个单一的工作单元,这个单元有一系列操作,这些操作要么全部执行,要么全部不执行。事务管理具有以下四个特性:

原子性:事务应该当作一个单独单元的操作,这意味着整个序列操作要么是成功,要么是失败的。

一致性:这表示数据库的引用完整性的一致性,表中唯一的主键等。

隔离性:可能同时处理很多有相同的数据集的事务,每个事务应该与其他事务隔离,以防止数据损坏。

持久性:一个事务一旦完成全部操作后,这个事务的结果必须是永久性的,不能因系统故障而从数据库中删除。

spring中的事务管理案例如下:

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
        
        <!-- 自动扫描 -->
        <context:component-scan base-package="cn.com.gjw"/>

		<!-- 配置数据源 -->
		<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
			<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		    <property name="url" value="jdbc:mysql://localhost:3306/test"/>
		    <property name="username" value="root"/>
		    <property name="password" value="root"/>
        </bean>
        
        <!-- 配置jdbcTemplate -->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        	<constructor-arg ref="dataSource"/>
        </bean>

	    <!-- 1.配置事务管理器 -->
	    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	    	<property name="dataSource" ref="dataSource"></property>
	    </bean>
	    
	    <!-- 2.配置通知 -->
	    <tx:advice id="txAdvice" transaction-manager="txManager">
	    	<tx:attributes>
	    		<!-- *代表所有方法,REQUIRED表示方法必须在事务中执行, false代表不是只读  -->
	    		<tx:method name="*" propagation="REQUIRED" read-only="false"/>
	    	</tx:attributes>
	    </tx:advice>
	    
	    <!-- 配置aop -->
	    <aop:config>
	    	<aop:pointcut expression="execution(* *cn.com.gjw.service.impl.*.*(..))" id="point"/>
	    	<aop:advisor advice-ref="txAdvice" pointcut-ref="point"/>
	    </aop:config>
	    <!-- 防止aop配置混乱 -->
	    <aop:aspectj-autoproxy proxy-target-class="true"/>
        
</beans>
           

service实现类:

package cn.com.gjw.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.gjw.dao.impl.AccountDaoImpl;
import cn.com.gjw.pojo.Account;
import cn.com.gjw.service.AccountService;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

	@Autowired
	private AccountDaoImpl adi;
	
	@Override
	public void updateAccount(Account[] account) {
		adi.updateAccount(account[0]);
		//int x = 1 / 0;
		adi.updateAccount(account[1]);
	}

}
           

dao实现类:

package cn.com.gjw.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import cn.com.gjw.dao.AccountDao;
import cn.com.gjw.pojo.Account;

@Repository
public class AccountDaoImpl implements AccountDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public void updateAccount(Account account) {
		int x = jdbcTemplate.update("update account set money=? where id=?", account.getMoney(), account.getId());
		System.out.println(x);
	}

}
           

测试类:

package cn.com.gjw.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.gjw.pojo.Account;
import cn.com.gjw.service.AccountService;
import cn.com.gjw.service.impl.AccountServiceImpl;

public class Test {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		AccountService as = ac.getBean("accountService", AccountServiceImpl.class);
		Account a1 = new Account();
		a1.setId(1);
		a1.setMoney(1000);
		Account a2 = new Account();
		a2.setId(2);
		a2.setMoney(1000);
		Account[] accounts = {a1, a2};
		as.updateAccount(accounts);
	}

}
           

通过以上配置即可实现数据库事务管理,service中两次调用dao的updateAccount()方法,要么两次都执行成功,要么都执行不成功。