xml方式实现spring的声明式事务管理及对jdbc操作的支持

事务是一组操作的执行单元,相对于数据库操作来讲,事务管理的是一组 SQL 指令,比如增加,修改,删除等,事务的一致性,要求,这个事务内的操作必须全部执行成功,如果在此过程种出现了差错,比如有一条 SQL 语句没有执行成功,那么这一组操作都将全部回滚。
 事务特性( ACID
Atomic( 原子性 ): 要么都成功,要么都失败;
Consistent( 一致性 ): 数据应该不被破坏;
Isolate( 隔离性 ): 用户间操作不相混淆;
Durable( 持久性 ): 永久保存;

Spring提供了两种事务管理方式

     编程式事务管理

        编写程序式的事务管理可以清楚的定义事务的边界,可以实现细粒度的事务控制,比如你可以通过程序代码来控制你的事务何时开始,何时结束等,与后面介绍的声明式事务管理相比,它可以实现细粒度的事务控制,例如jdbc,hibernatespring中不提倡使用。

     声明式事务管理

        如果你并不需要细粒度的事务控制,你可以使用声明式事务,在Spring中,你只需要在Spring配置文件中做一些配置,即可将操作纳入到事务管理中,解除了和代码的耦合,这是对应用代码影响最小的选择,从这一点再次验证了Spring关于AOP的概念。当你不需要事务管理的时候,可以直接从Spring配置文件中移除该设置


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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        <!-- c3p0连接池的一些配置-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="user" value="root"></property>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/day16"></property>
            <property name="password" value="root"></property>
            <property name="maxPoolSize" value="10"></property>
            <property name="maxStatements" value="20"></property>
        </bean>
        
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <bean id="deptDao" class="com.spring.tranzcation.DeptDao">
            <property name="jdbcTemplate" ref="jdbcTemplate"></property>
        </bean>
        <bean id="deptService" class="com.spring.tranzcation.DeptService">
            <property name="deptDao" ref="deptDao"></property>
        </bean>
        <!-- 配置事物管理 -->
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>        
        <!-- 配置事物增强 -->
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="get*" read-only="true"/>
                <tx:method name="*" read-only="false"/>
            </tx:attributes>
        </tx:advice>
        <!-- AOP配置拦截那些方法,切入点表达式和事物配置增强相配合 -->
        <aop:config>
            <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.spring.tranzcation.DeptService.*(..))"/>
        </aop:config>    
</beans>

相关jar的引入


测试用的DeptService,此处设置一个错误,检查事物管理是否应用成功,如果保存失败,

并且数据库无记录,则说明事物应用成功。

package com.spring.tranzcation;

import org.springframework.jdbc.core.JdbcTemplate;

public class DeptDao implements IDeptDao{
	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	@Override
	public void save() {
		String sql = "insert into dept(deptName) values(?)";
		jdbcTemplate.update(sql, "生活部");
	}

}


package com.spring.tranzcation;

public class DeptService {
	private DeptDao deptDao;
	public void setDeptDao(DeptDao deptDao) {
		this.deptDao = deptDao;
	}
	public void savaDept() {
		deptDao.save();
		int num = 4/0;
	}
}

调用DeptService中的saveDept()报错

package com.spring.tranzcation;

import static org.junit.Assert.*;

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

public class App {
	@Test
	public void testTx() throws Exception {
		ApplicationContext ac = 
				new ClassPathXmlApplicationContext("com/spring/tranzcation/applicationContext.xml");
		DeptService deptService = (DeptService)ac.getBean("deptService");
		deptService.savaDept();
	}
}



并检查数据库无记录插入(和未执行前想比较),说明事物应用成功,已经回滚



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lz_94

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值