After the transaction mybatis management and integrated spring to spring on how to manage affairs

   Management of transactions 1.mybatis

    After SqlSessionFactory open a SqlSession, will inject current affairs sqlsession factory TransactionFactory:

    

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {    
Transaction tx = null;

try
{ final Environment environment = configuration.getEnvironment(); final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); final Executor executor = configuration.newExecutor(tx, execType); return new DefaultSqlSession(configuration, executor, autoCommit); } catch (Exception e) { closeTransaction(tx); // may have fetched a connection so lets call close() throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }   
//事务的生产交给
TransactionFactory

private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) { if (environment == null || environment.getTransactionFactory() == null) { return new ManagedTransactionFactory(); } return environment.getTransactionFactory(); }

 

 

So the production mybatis affairs by the production TransactionFactory, this class is the interface, and the specific implementation is the logical place to perform true, integrated mybatis in the spring, SpringManagedTransactionFactory implements the interface, thereby transaction management mybatis of the spring to be managed

To achieve the transfer of logic:

  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath*:*.xml"/>
    </bean>

Arranged in the spring SqlSessionFactoryBean performs afterPropertiesSet method initializes SqlSessionFactory, and arranged TransactionFactory to SpringManagedTransactionFactory

 

 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.*.**"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

The MapperScannerConfigurer performs postProcessBeanDefinitionRegistry way to achieve Mapper interface proxy, the proxy object to the spring and container management

Guess you like

Origin www.cnblogs.com/eason-ou/p/12515921.html