Research on Spring transaction configuration

Keywords: Spring xml JDBC Mybatis

I. configuration and analysis of spring+mybaits xml in the project

Generally, we will configure in datasource.xml as follows, but the principle and purpose of each configuration item is not so clear. If not clear, there will be a pit in use, so we will explain these configuration items one by one

(1)Configure data sources
 <bean id="dataSourace" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="${db_url}" />
        <property name="username" value="$db_user}" />
        <property name="password" value="${db_passwd}" />
        <property name="maxWait" value="${db_maxWait}" />
        <property name="maxActive" value="28" /> 
        <property name="initialSize" value="2" />
        <property name="minIdle" value="0" />
        <property name="timeBetweenEvictionRunsMillis" value="db_time" />
    </bean>

(2)establish sqlSessionFactory
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="mapperLocations" value="classpath*:com/**/mapper/*Mapper*.xml" /> 
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.test.***.dal" />
</bean>
    
(3)Configure the scanner to scan the mapper Generate database operation agent class
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="annotationClass" value="javax.annotation.Resource"></property>
        <property name="basePackage" value="com.test.***.dal.***.mapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

(4)Configure transaction manager
<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

(5)Declare use of annotated transactions
<tx:annotation-driven transaction-manager="transactionManager" />
(6)Register various beanfactory processor
<context:annotation-config />    

(7)This configuration creates a TransactionInterceptor Of bean,Execution method as transaction facet
 <tx:advice id="defaultTxAdvice">
    <tx:attributes>
        <tx:method name="*" rollback-for="Exception" />
    </tx:attributes>
</tx:advice>

(8)This configuration creates a DefaultBeanFactoryPointcutAdvisor Of bean,This bean It's a advisor,It contains pointcut and advice.The former explains where the facets are added, and the latter is the execution logic. There can be more than one advisor
<aop:config>
    <aop:pointcut id="myCut" expression="(execution(* *..*BoImpl.*(..))) "/>
    <aop:advisor pointcut-ref="myCut" advice-ref="defaultTxAdvice" />
</aop:config>

1.1 data source configuration

(1) it's data source configuration. There's nothing to say about it.

1.2 configure SqlSessionFactory

(2) the function is to create a SqlSessionFactory according to the configuration. Look at the code of SqlSessionFactoryBean to see that it implements the FactoryBean and InitializingBean classes. Because it implements the InitializingBean, its afterpropertieset method is natural. Because it implements the FactoryBean class, there will be a getObject method. Look at the sequence diagram below:

Posted by Courbois on Tue, 17 Dec 2019 10:26:15 -0800