Transaction control configuration based on xml file

  • Configure transaction manager

    <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><!-- Configure transaction manager -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="datasource" ref="datasource"></property>
    </bean>
  • Configure notifications for transactions

    • At this point we need to import the constraints of the transaction: tx and aop namespaces and constraints

    • Property: id is the unique identification of transaction notification; transaction manager is the id of the bean referencing the transaction manager

    <!--Configure notifications for transactions-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager"></tx:advice>
  • Configure common pointcut expressions in AOP

  • Establish correspondence between transaction notification and pointcut expression

    <! -- configure AOP -- >
    <aop:config>
        <! -- configure AOP universal pointcut expression -- >
        <aop:pointcut id="pt1" expressioin="execution(* com.mypro.service.impl.*.*(..))"/>
        <! -- establish correspondence between transaction notification and pointcut expression -- >
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
    </aop:config>
  • Configure the properties of the transaction: configure inside the tx:advice tag

    • Isolation: used to specify transaction isolation level, DEFAULT is DEFAULT

    • Propagation: used to specify the propagation behavior of a transaction. The default value is REQUIRED, which indicates that there must be a transaction. You can select SUPPORTS when querying

    • Read only: used to specify whether the transaction is read-only. Only the query method can set true. The default value is false, which means read-write

    • Timeout: used to specify the timeout of a transaction. The default value is - 1, which means never timeout. If it is set in seconds

    • Rollback for: used to specify an exception. When the exception is generated, the transaction is rolled back, and other exception transactions are not rolled back. No default value means that any exception will be rolled back

    • No rollback for: used to specify an exception. When the exception is generated, the transaction will not be rolled back, and other exception transactions will be rolled back. No default value means that any exception will be rolled back

    <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>
  • Complete bean.xml file content

    <?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><!-- Configure transaction manager -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- Configure transaction notifications -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <!-- Configure transaction properties -->
            <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>
    ​
    ​
        <!-- To configure AOP -->
        <aop:config>
            <!-- To configure AOP Common pointcut expression -->
            <aop:pointcut id="pt1" expression="execution(* com.mypro.service.impl.*.*(..))"/>
            <!-- Establish correspondence between transaction notification and pointcut expression -->
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
        </aop:config></beans>

     

Keywords: Java JDBC MySQL Spring xml

Added by blt2589 on Tue, 21 Apr 2020 06:49:54 +0300