Ibatis. Net study note 10-database connection Processing

Source: Internet
Author: User
In ibatis. net, many operations are hidden. For example, database connections and transaction processing are all processed in the framework. What is the specific solution in the framework? Is there anything worth learning from? Can we skip the framework and handle it by ourselves?

First, let's take a look at a common database query operation in ibatis. Net:
Call accountbusinessCode: // Obtain static daomanager
Idaomanager daomanager = Daocommon. getdaomanager ();
Iaccountdao accountdao = (Iaccountdao) daomanager [ Typeof (Iaccountdao)];
System. Collections. arraylist list = Accountdao. getallaccount ();

Where The first statement Is an initialization operation, which connects to the actual database according to the configuration file: // Build the connection Template
Type = Assembly. GetType (_ connectionclass, True );
Checkpropertytype ( " Dbconnectionclass " , Typeof (Idbconnection), type );
_ Templateconnection = (Idbconnection) type. getconstructor (type. emptytypes). Invoke ( Null );

At the same time, all DaO proxies will be initialized to the static variables according to the DaO configuration, that is, the DaO objects that have been initialized will be retrieved from the static variables for future calls.Note: The variables in Dao are always saved and shared by all users. Therefore, pay attention to the use of some class variables (similar to struts actions)

Second statementIs to get a specified Dao Proxy from the static variable that has been processed (see:Ibatis. Net study note 9-Design and Analysis of Dynamic Dao Selection)

Statement 3 Is to call the getallaccount method in the actual accountdao. Before calling this method, the interception will first go to daoproxy for processing:Daomanager = _ Daoimplementation. daomanager;
If (Daomanager. isdaosessionstarted ())
{
Try  
{
Result=Invocation. method. Invoke (_ daoimplementation. daoinstance, arguments );
}  
Catch (Exception E)
{
ThrowUnwrapexception (E, invocation. method. Name );
}
}  
Else  
{
Logging # Region Logging
If (_ Logger. isdebugenabled)
{
_ Logger. debug ("Dao proxy, open a connection");
}
# Endregion
// Open a connection
Try  
{
Daomanager. openconnection ();
Result=Invocation. method. Invoke (_ daoimplementation. daoinstance, arguments );
}  
Catch (Exception E)
{
ThrowUnwrapexception (E, invocation. method. Name );
}  
Finally  
{
Daomanager. closeconnection ();
}
}

From the code above, we can see that it is divided into two situations, one is the case of transactions, the other is the case of no transactions. In the case of transactions: idalsession session = daomanager. begintransaction (); The database connection has been created, so it is no longer created here. If no transaction exists, call daomanager. openconnection (); To create a database connection. The actual creation code is as follows: If (_ Templateconnectionisicloneable)
{
Return(Idbconnection) (icloneable) _ templateconnection). Clone ();
}
Else
{
Return(Idbconnection) activator. createinstance (_ templateconnection. GetType ());
}

The clone method of connection is used here, that is, a new connection is established through the connection during initialization. You do not need to create a new connection. After the connection is created, save it in httpcontext: Public   Override   Void Store (isqlmapsession)
{
Httpcontext currentcontext=Obtainsessioncontext ();
Currentcontext. items [sessionname]=Session;
}

Httpcontext is equivalent to a request static variable. It can save the static information of the same request for future use.

Finally, call the code in the actual accountdao: Idaomanager daomanager = Daomanager. getinstance ( This );
Sqlmapdaosession daosession = Daomanager. localdaosession As Sqlmapdaosession;

_ Sqlmap = Daosession. sqlmap;

Arraylist list = (Arraylist) sqlmap. queryforlist ( " Getallaccountsviaresultmap " , Null );
Return List;

Here, the connection is obtained from httpcontext for database processing.
Only the code in accountbusiness and accountdao needs to be written by ourselves. Others are the key code extracted from the framework.

The above is a regular query operation. Through the above analysis, we found that you can use the following statements to perform database operations directly (DAO is not required, you do not need to configure Dao or skip the processing of the Interceptor-this can improve the performance, but at the same time it will increase the complexity of the code and break the advantages of hierarchical encoding ): Idaomanager daomanager = Daocommon. getdaomanager ();
Daomanager. openconnection ();
Sqlmapdaosession daosession = Daomanager. localdaosession As Sqlmapdaosession;
Arraylist list = (Arraylist) daosession. sqlmap. queryforlist ( " Getallaccountsviaresultmap " , Null );
Daomanager. closeconnection ();

Related Article

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.