Detailed Explanation of spring Transaction Configuration in JSP

  • 2021-12-19 06:29:39
  • OfStack

Detailed Explanation of spring Transaction Configuration in JSP

A few days ago, I was asked how to prevent the server from going down and causing incomplete data operation.

Asked a colleague, it is a matter. Ah, it suddenly dawned on me that I was confused for 1 o'clock.

Declarative transaction configuration, which is the most recommended, is configured to service layer.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  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/tx
  http://www.springframework.org/schema/tx/spring-tx.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-2.0.xsd">
  
  <!--  Use annotation @Repository,@Service Automatic registration bean,  And guarantee that @Required , @Autowired The scope of the package in which the attributes of are injected  -->
  <context:component-scan base-package="com.rd,com.rongdu"/>
  <context:annotation-config/>
  

  <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
    <!-- Connection Info -->
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sfd?useUnicode=true&amp;characterEncoding=utf8"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>

    <!--  Check the interval between idle connections in the database connection pool  -->
    <property name="idleConnectionTestPeriod" value="4" />
    <!--  Maximum lifetime of unused links in connection pool  -->
    <property name="idleMaxAge" value="240" />
    <!--  Set each partition to contain connection Maximum number  -->
    <property name="maxConnectionsPerPartition" value="20" />
    <!--  Set each partition to contain connection Minimum number  -->
    <property name="minConnectionsPerPartition" value="10" />
    <!--  Set the number of partitions per partition  -->
    <property name="partitionCount" value="3" />
    <!--  Set the in the partition connection Increased quantity  -->
    <property name="acquireIncrement" value="5" />
    <property name="statementsCacheSize" value="50"/>
     <property name="releaseHelperThreads" value="3"/>
  </bean>

  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource">
      <ref bean="dataSource" />
    </property>
  </bean>
  <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    <constructor-arg index="0" ref="dataSource"/> 
  </bean>

  <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource"/>
  </bean>
  
  
  <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
      <tx:method name="add*" propagation="REQUIRED" />
      <tx:method name="delete*" propagation="REQUIRED" />
      <tx:method name="update*" propagation="REQUIRED" />
      <tx:method name="*" propagation="REQUIRED" />

    </tx:attributes>
  </tx:advice>
  
  <aop:config>
    <aop:pointcut id="allManagerMethod"
      expression="execution(* com.test.service.*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" />
  </aop:config>
</beans>

Among them, there is something wrong with a wildcard character. Transactions are used when updating data, and queries do not require transactions. So it's too violent to use * directly.

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: