spring 事务管理 1(使用spring的JdbcTemplate访问数据库)

持久层和事务的关系java

dao层脱离事务也能操做数据库,事务是保证dao层数据操做的完整性(即原子性、一致性、隔离性、持久性,也即所谓的 ACID)node

事务能够保证一组操做要么全成功,要么所有失败,就是事务是一个不可分割的总体spring

使用spring 封装的jdbc访问数据库(未使用事务)

定义beansql

package sping.jdbc;

import java.sql.SQLException;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service(value="jdbcWithoutTM")
public class JdbcWithoutTM {
   
    @Resource(name="jdbcTemplate")
    private JdbcTemplate jt;
   
  
    public void addPerson(){
        jt.execute("insert into person(pname) values ('名字')");
    }

}数据库

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: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-3.0.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.0.xsd  
           http://www.springframework.org/schema/aop  
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">


    <context:component-scan base-package="sping.jdbc*"></context:component-scan>

    <!-- 数据源默认将autoCommit设置为true -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <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>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>

    <context:property-placeholder location="jdbc.properties" />
</beans>测试

 

junit测试类

 package sping.jdbc.test;


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

import sping.jdbc.JdbcWithoutTM;

public class SpringJdbcTest {
    private static ApplicationContext context;
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        context = new ClassPathXmlApplicationContext(
                new String[] { "jdbc.xml" });
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Test
    public void test() {
        JdbcWithoutTM jtm=context.getBean("jdbcWithoutTM", JdbcWithoutTM.class);
        jtm.addPerson();
    }

}url

此程序虽然没有配置和使用事务控制,可是数据仍是插入到了数据库。这说明数据库操做并必定须要事务控制。spa

 如今咱们修改下JdbcWithoutTM的插入方法,让他在数据库插入操做完成以后手动抛出个异常,看数据是否还能插入。component

     public void addPerson(){

        jt.execute("insert into person(pname) values ('王江涛')");
        throw new RuntimeException();
    }

 

运行程序,查看数据库,结果是数据仍是插入到了数据库(事务自动提交),这明显不符合咱们的要求,说明此操做没有回滚,形成操做的不完整性,固然,咱们只是一个插入操做,若是咱们在插入操做以后再有个与之相关的数据库操做,而在两个操做中间出了个异常,那么就形成数据不完整了。

因此咱们就要加入事务,而spring让咱们从繁琐的事务控制中解脱出来。

咱们修改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: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-3.0.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.0.xsd  
           http://www.springframework.org/schema/aop  
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">


    <context:component-scan base-package="sping.jdbc*"></context:component-scan>

    <!-- 数据源默认将autoCommit设置为true -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <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>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>

    <!-- 事务管理配置 若是使用orm框架,则替换为相应的事务管理器,注意,若是想使用注解性事务,

                        则需引入cglib-nodep.jar,不是cglib.jar-->
    <bean id="myTxManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:annotation-driven transaction-manager="myTxManager"></tx:annotation-driven>

    <context:property-placeholder location="jdbc.properties" />
</beans>

 

在插入操做方法上面加上事务注解

    @Transactional(propagation=Propagation.REQUIRED)
    public void addPerson(){

        jt.execute("insert into person(pname) values ('王江涛')");//1
        System.out.println(1/0);//2
        jt.execute("insert into person(pname) values ('王江涛')");//3
    }

再次执行程序,能够看到,数据库并无插入任何数据,由于程序在2处出现了异常,因此之前作的全部操做所有回滚了。这就符合咱们的要求啦

相关文章
相关标签/搜索