初识spring(十二)

Spring之事务管理

long long ago,我们管理事务是用begintrancation之类的方法。这是一种细粒度的方法管理。这样是需要写很多重复代码。但是spring是为简化重复代码而生。当然不能忍受这玩意的出现。
我们在XML中声明事务管理。action->service->dao层。我们把所有的事物控制都放在service层。
t_dept.sql

create table `t_dept` (
    `id` double ,
    `ename` varchar (90)
); 

先写XML吧。在这里我们必须引入tx空间。



<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">


    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver">property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3308/test">property>
        <property name="user" value="root">property>
        <property name="password" value="123">property>
        <property name="initialPoolSize" value="3">property>
        <property name="maxPoolSize" value="10">property>
        <property name="maxStatements" value="100">property>
        <property name="acquireIncrement" value="2">property>
    bean>

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

    
    <bean id="deptDao" class="cn.itcast.a_tx.DeptDao">
        <property name="jdbcTemplate" ref="jdbcTemplate">property>
    bean>

    
    <bean id="deptService" class="cn.itcast.a_tx.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="*" read-only="false"/>
        tx:attributes>
    tx:advice>

    
    <aop:config>
        <aop:pointcut expression="execution(* cn.itcast.a_tx.DeptService.save(..))" id="pt"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    aop:config>

beans>   
  • name 是必须的,表示与事务属性关联的方法名(业务方法名),对切入点进行细化。通配符(*)可以用来指定一批关联到相同的事务属性的方法。
    如:’get*’、’handle*’、’on*Event’等等.
    * propagation 不是必须的 ,默认值是REQUIRED
    表示事务传播行为, 包括REQUIRED,SUPPORTS,MANDATORY,REQUIRES_NEW,NOT_SUPPORTED,NEVER,NESTED
    * isolation 不是必须的 默认值DEFAULT
    表示事务隔离级别(数据库的隔离级别)
    * timeout 不是必须的 默认值-1(永不超时)
    表示事务超时的时间(以秒为单位)
      * read-only    不是必须的 默认值false不是只读的 
                        表示事务是否只读? 

      * rollback-for 不是必须的   
                        表示将被触发进行回滚的 Exception(s);以逗号分开。
                        如:'com.foo.MyBusinessException,ServletException' 

      * no-rollback-for 不是必须的  
                          表示不被触发进行回滚的 Exception(s);以逗号分开。
                          如:'com.foo.MyBusinessException,ServletException'


    任何 RuntimeException 将触发事务回滚,但是任何 checked Exception 将不触发事务回滚 

Dept.java

package cn.itcast.a_tx;

public class Dept {

    private int deptId;
    private String deptName;
    public int getDeptId() {
        return deptId;
    }
    public void setDeptId(int deptId) {
        this.deptId = deptId;
    }
    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

}



DeptDao.java

package cn.itcast.a_tx;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * dao实现,使用Spring对jdbc支持功能
 * @author Jie.Yuan
 *
 */
public class DeptDao {

    // 容器注入JdbcTemplate对象
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void save(Dept dept){
        String sql = "insert into t_dept (ename) values(?)";
        jdbcTemplate.update(sql,dept.getDeptName());
    }
}

DeptService.java

package cn.itcast.a_tx;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * Service
 * @author Jie.Yuan
 *
 */
public class DeptService {

    // 容器注入dao对象
    private DeptDao deptDao;
    public void setDeptDao(DeptDao deptDao) {
        this.deptDao = deptDao;
    }

    /*
     * 事务控制?
     */
    public void save(Dept dept){
        // 第一次调用
        deptDao.save(dept);

        int i = 1/0; // 异常: 整个Service.save()执行成功的要回滚

        // 第二次调用
        deptDao.save(dept);
    }
}

App.java

package cn.itcast.a_tx;

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

public class App {

    @Test
    public void testApp() throws Exception {
        //容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/a_tx/bean.xml");

        // 模拟数据
        Dept dept = new Dept();
        dept.setDeptName("测试: 开发部");

        DeptService deptService = (DeptService) ac.getBean("deptService");
        deptService.save(dept);

    }
}

你可能感兴趣的:(spring)