MyBatis Integration of spring source analysis for transaction management

Source: Internet
Author: User
Tags stmt

A: Preface

Not complete, but saw some key places, here to make a record, the process will be a bit messy, later gradually supplemented by the final archive for the complete process; I believe that the framework of the source of all know the process can not fully determine how the process, after all, it is not possible to test all over

, but it is important to see the inspiration in the process (intuition knows what the next steps are supposed to be), but this self-consciously guesses rather than casts, that is, the process is going to recursively see some key points rather than tossing the dice to determine which sub-class.

, so they can not write their own time to see the process of the idea of writing very thin, the process also a little jump, if you have a question of the best to verify (the way is to search and then see where to call this step to see the line, some small trick is if the public is generally called by the external class [Portal], If it's protected, it's not true.

The description is implemented in such a way that if it has been implemented, this method will be called in the parent class, and if it is private then it will be called in the current class;

Two: Important classes or methods

1) for MyBatis

1.org.apache.ibatis.executor.executor interface, there are about batchexecutor/reuseexecutor/simpleexecutor three subclasses have a common parent class baseexecutor, It is a component of the Defaultsqlsession class, and the Sqlsession object, such as SelectOne, is actually performed by executor;

2.org.apache.ibatis.transaction.transaction interface, its implementation class has many, but each implementation class has the DataSource attribute is used to generate the data source and database service interaction;

3.SpringManagedTransaction is the key to support spring's management of mybatis transactions;

4.DefaultSqlSessionFactory

5.DefaultSqlSession

The important relationship between them: There are transaction objects in the Executor;executor in the Sqlsession, and DataSource in transaction;

Process: Sqlsession execution code delegate for executor execution, executor through transaction get connection , and transaction by judging whether there are connectionholder objects in the Threadlocalmap, if not, the DataSource get one, otherwise the return exists in Threadlocalmap;

Three: Execution process

such as: Sqlsession.selectone execution Process:

3.1selectOne final turn to selectlist;

3.2 Call the Sqlsession property Executor.query method execution;

3.3 Execute this sentence in the Baseexecutor Query method: List = This.queryfromdatabase (ms, Parameter, Rowbounds, Resulthandler, Key, Boundsql);So this code is critical;

3.4 Then look inside there are this.doquery (MS, Parameter, Rowbounds, Resulthandler, Boundsql); method;

3.5 then look at the Doquery method of Simpleexecutor (see source many times is by the immediate inspiration, more difficult to complete the description of the idea and process);

Supplement start: 1. Then look at stmt = this.preparestatement (handler, Ms.getstatementlog ()); Because the statement executes the SQL statement also through its internal connection object to execute;

2. See Connection connection = this.getconnection (statementlog), and then look inside: Connection connection = This.transaction.getConnection (); important , transaction finally appeared in the process of executing SQL;

3. In the case of integrating spring, it is known through the context of this article that this transaction is a Springmanagedtransaction object;

4. Then look at Springmanagedtransaction's this.openconnection (); There is code:

this. Connection = Datasourceutils.getconnection (this. DataSource);          This  This . Connection.getautocommit ();         this. isconnectiontransactional = datasourceutils.isconnectiontransactional ( This . DataSource);

5. We look at datasourceutils.getconnection (this. DataSource), and then go to the Dogetconnection method with the code:

Connectionholder Conholder = (connectionholder) Transactionsynchronizationmanager.getresource (dataSource); if NULL  ...) {A new connection is obtained through DataSource} Else  {conholder.getconnection (); Return a Connection object in the Threadlocalmap;}

Notice that above the datasourceutils and here when Transactionsynchronizationmanager and Connectionholder are the classes in spring are not mybatis, The Threadlocal objects inside are static, so mybatis can get the Connectionholder object that is placed in Threadlocalmap in spring;

Whether spring produces connectionholder and whether it is placed in Threadlocalmap is based on txadvice strategy;

The end of the supplement;

3.6 then look at Handler.query (stmt, Resulthandler); Then can pick Routingstatementhandler this method to see;

3.7 will be entrusted to other handler processing, here can be tentatively Preparedstatementhandler class;

3.8 See Ps.execute (), there is nothing to look at behind the JDK through the stmt execution of SQL, the key here is stmt connection is what;

Four: Important steps in the integration process with spring

1. Org.mybatis.spring.SqlSessionFactoryBeanis configured, By default, the Transactionfactory property inside it is a Springmanagedtransactionfactory object (you can proactively specify Sqlsessionfactorybean TRANSACTIONFA if you do not want spring to manage the transaction) Ctory for other classes);

2. The environment of Sqlsessionfactorybean's sqlsessionfactory configuration holds the Transactionfactory object as Springmanagedtransactionfactory Object, this step is important;

3. Let's take a look at Defaultsqlsessionfactory's Opensessionfromdatasource method (where we really get the Sqlsession object) with the code

 This . Configuration.getenvironment ();              This . Gettransactionfactoryfromenvironment (Environment);             = transactionfactory.newtransaction (Environment.getdatasource (), level, autocommit);              This . Configuration.newexecutor (TX, exectype);             New Defaultsqlsession (this. Configuration, executor, autocommit);

Note: The Exectype default corresponds to Simpleexecutor, and this executor holds the Springmanagedtransaction object that the springmanagedtransactionfactory produces, Here is a clear first;

4. The above has been made clear that the Sqlsession execution statement is commissioned for the Exector to execute, the following steps and three in accordance with the same will not repeat;

Five: Important classes and methods of transaction management in spring

1.Org.springframework.transaction.interceptor.TransactionInterceptor, extremely important, This is when the Pointcut method is executed, the transaction interceptor will do some extra processing (a around) based on the propagation configured by the <TX:attributes> . Includes whether the connection object needs to be generated and the autocommit of this connection object set why value (Springmanagedtransaction this.openconnection (); will use this value ), such as REQUIRED will first determine whether there is connectionholder in the Threadlocalmap, There is no preprocessing is obtained by DataSource connection and set to Autocommit to false, and then by

Connectionholer Packing this connection and set into Threalocalmap;

2.Org.springframework.jdbc.datasource.DataSourceTransactionManager, extremely important, Is the important component that spring uses to perform different logic based on the propagation strategy (executed in the Transactioninterceptor Invoke method);

3.TxNamespaceHandler is used to process elements with a namespace of TX;

4.TxAdviceBeanDefinitionParser is used to parse <tx:advice elements, which are processed by Doparse;

5.NameMatchTransactionAttributeSource, extremely important, stored the <tx:attributes configuration of the strategy for transactioninterceptor use;

Add: If you want to understand spring's transaction management, you can start with the Transactioninterceptor invoke method implementation, this is the entry function, and the internal use of the Connectionholder is the class with mybatis interaction;

MyBatis Integration of spring source analysis for transaction management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.