当前位置: 移动技术网 > IT编程>开发语言>Java > 基于xml文件的事务控制配置

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

2020年04月21日  | 移动技术网IT编程  | 我要评论
  • 配置事务管理器

    <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 -->
    <aop:config>
        <!-- 配置aop通用切入点表达式 -->
        <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 -->
        <aop:config>
            <!-- 配置aop通用切入点表达式 -->
            <aop:pointcut id="pt1" expression="execution(* com.mypro.service.impl.*.*(..))"/>
            <!-- 建立事务通知和切入点表达式的对应关系 -->
            <aop:advisor advice-ref="txadvice" pointcut-ref="pt1"></aop:advisor>
        </aop:config>
    ​
    </beans>

     

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网