基于xml文件的事务控制配置


  • 配置事务管理器

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver">property>
        <property name="url" value="jdbc:mysql://localhost:3306/myword">property>
        <property name="username" value="root">property>
        <property name="password" value="123456">property>
    bean>
    ?
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="datasource" ref="datasource">property>
    bean>
  • 配置事务的通知

    • 此时我们需要导入事务的约束:tx和aop名称空间和约束

    • 属性:id是事务通知的唯一标识;transaction-manager是引用事务管理器的bean的id

    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">tx:advice>
  • 配置AOP中通用切入点表达式

  • 建立事务通知和切入点表达式的对应关系

    
    <aop:config>
        
        <aop:pointcut id="pt1" expressioin="execution(* com.mypro.service.impl.*.*(..))"/>
        
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1">aop:advisor>
    aop:config>
  • 配置事务的属性:在tx:advice标签内部配置

    • isolation:用于指定事务隔离级别,默认是DEFAULT

    • propagation:用于指定事务的传播行为,默认是REQUIRED,表示一定有事务,增删改的选择;查询方法时可以选择SUPPORTS

    • read-only:用于指定事务是否只读,只有查询方法才能设置true,默认是false表示读写

    • timeout:用于指定事务的超时时间,默认值是-1表示永不超时;若设置以秒为单位

    • rollback-for:用于指定一个异常,当产生该异常时事务回滚,产生其他异常事务不回滚。没有默认值表示任何异常都回滚

    • no-rollback-for:用于指定一个异常,当产生该异常时事务不回滚,产生其他异常事务回滚。没有默认值表示任何异常都回滚

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" read-only="false">tx:method>
            <tx:method name="select*" propagationi="SUPPORTS" read-only="true">tx:method>
        tx:attributes>
    tx:advice>
  • 完整的bean.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: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/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="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource">property>
        bean>
    ?
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver">property>
            <property name="url" value="jdbc:mysql://localhost:3306/myword">property>
            <property name="username" value="root">property>
            <property name="password" value="123456">property>
        bean>
    ?
        <bean id="wordsService" class="com.mypro.service.impl.WordsServiceImpl">bean>
        <bean id="wordsDao" class="com.mypro.dao.impl.WordsDaoImpl">bean>
    ?
        
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource">property>
        bean>
        
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            
            <tx:attributes>
                <tx:method name="*" propagation="REQUIRED" read-only="false">tx:method>
                <tx:method name="select*" propagation="SUPPORTS" read-only="true">tx:method>
            tx:attributes>
        tx:advice>
    ?
    ?
        
        <aop:config>
            
            <aop:pointcut id="pt1" expression="execution(* com.mypro.service.impl.*.*(..))"/>
            
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1">aop:advisor>
        aop:config>
    ?
    beans>