1. 程式人生 > >Spring事務管理

Spring事務管理

classpath 依賴 font word .get 執行 pointcut str 攔截

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事務管理