首页 > 代码库 > Spring事务管理

Spring事务管理

1、导入jar包

spring核心包

commons-logging.jar

spring-beans.jar

spring-context.jar

spring-core.jar

spring-expression.jar

spinrg jdbc和事务jar包

spring-jdbc.jar

spinrg-tx.jar

Oracel数据库驱动包

ojdcb6.jar

c3p0连接池jar包

c3p0.jar

spring aop jar包

spring-aop.jar

aspectj.jar(aop的依赖包)

aspectjweaver.jar

aopalliance.jar

 

Spring事务管理简单说明:如果一个Service调用了好几个Dao,其中只要有一个dao发生异常,则不管之前的dao有没有执行成功,全部回滚!

 

2、编写Emp类

public class Emp {

    private int empno;
    private String ename;
    public int getEmpno() {
        return empno;
    }
    public void setEmpno(int empno) {
        this.empno = empno;
    }
    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
}

2、编写DAO

//使用Spring对jdbc的支持功能
public class EmpDao {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    public void save(Emp emp){
        String sql = "INSERT INTO emp(empno, ename) VALUES(?,?)";
        jdbcTemplate.update(sql, emp.getEmpno(),emp.getEname());
    }
}

3、编写service

public class EmpService {

    private EmpDao empDao;

    public void setEmpDao(EmpDao empDao) {
        this.empDao = empDao;
    }

    public void save(Emp emp){
        empDao.save(emp);
    }
}

4、编写action

public class EmpAction {

    @Test
    public void testTX() throws Exception{
        ApplicationContext ac = new ClassPathXmlApplicationContext("com/isoftstone/applicationContext.xml");
        
        Emp emp = new Emp();
        emp.setEmpno(1236);
        emp.setEname("zhangsan");
        
        EmpService empService = (EmpService)ac.getBean("empService");
        empService.save(emp);
    }
}

5、编写applicationContext文件

<?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">
     
     <!-- 1. 数据源对象: C3P0连接池 -->
     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
        <property name="jdbcUrl" value="jdbc:oracle:thin:localhost:8080:orcl"></property>
        <property name="user" value="scott"></property>
        <property name="password" value="tiger"></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>
    
    <!-- 2. JdbcTemplate工具类实例 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 3. dao实例 -->
    <bean id="empDao" class="com.isoftstone.EmpDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
 
    <!-- 4. service实例 -->
    <bean id="empService" class="com.isoftstone.EmpService">
        <property name="empDao" ref="empDao"></property>
    </bean>
    
    <!-- 5 spring声明式事物管理配置 -->
    <!-- 5.1 配置事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 5.2 配置事务增强(如何管理事务) -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 5.3 AOP配置 拦截哪些方法(切入点表达式) + 5.2的事务增强配置 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.isoftstone.EmpService.save(..))" id="pt"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>
</beans>

 

Spring事务管理