Ibatisnet problem summary

Source: Internet
Author: User
1. An unsolved ibatisnet concurrent access problem

Ibatisnet executes SQL statement through the sqlmap instance. A sqlmap instance encapsulates database connections and transactions. What makes it uncomfortable is that the cache is also encapsulated in sqlmap.

As you know, we can obtain the sqlmap instance in the following ways:

A. sqlmap map = Mapper. instance (); // single-piece mode, return a unique instance

B. domsqlmapbuilder builder = new domsqlmapbuilder ();
Sqlmap map = builder. Configure (); // return the new sqlmap instance

In a single-threaded environment, A is normal and the cache works normally. In a multi-threaded environment, because sqlmap encapsulates connection, if you call the same sqlmap instance to access the database concurrently, the following exception is thrown:
Ibatisnet. datamapper. Exceptions. datamapperexception: sqlmap cocould not invoke openconnection (). A connection is already started. Call closeconnection first.

In a multi-threaded environment, B comes in handy. You can use B to create a sqlmap instance at the beginning of each thread, and then use this sqlmap to access the database. The problem is that the cache is encapsulated in sqlmap. Each sqlmap creates its own cache. That is to say, the built-in cache mechanism of ibatisnet is useless.

I am so disappointed that I have to use hashtable to implement the simple cache function.

] A question about returning datatable

There is a lot of code on the Internet to return datatable when ibatisnet executes a query. Below is a simple list:

 

Private idbcommand getdbcommand (string statementname, object paramobject)
{
Istatement Statement = sqlmap. getmappedstatement (statementname). Statement;

Imappedstatement mapstatement = sqlmap. getmappedstatement (statementname );

Idalsession session = new sqlmapsession (sqlmap );

If (sqlmap. localsession! = NULL)
{
Session = sqlmap. localsession;
}
Else
{
Session = sqlmap. openconnection ();
}

Requestscope request = Statement. SQL. getrequestscope (mapstatement, paramobject, session );

Mapstatement. preparedcommand. Create (request, session, statement, paramobject );

Return request. idbcommand;

}


/** // <Summary>
/// Obtain the select result using a datatable (the $ placeholder parameter must be used for parameters in the XML file)
/// </Summary>
/// <Param name = "statementname"> statement id </param>
/// <Param name = "paramobject"> parameters required by the statement </param>
/// <Returns> the resulting able </returns>
Protected datatable executequeryfordatatable (string statementname, object paramobject)
{
Dataset DS = new dataset ();
Bool issessionlocal = false;
Idalsession session = sqlmap. localsession;
If (session = NULL)
{
Session = new sqlmapsession (sqlmap );
Session. openconnection ();
Issessionlocal = true;
}

Idbcommand cmd = getdbcommand (statementname, paramobject );

Try
{
Cmd. Connection = session. connection;
Idbdataadapter adapter = session. createdataadapter (CMD );
Adapter. Fill (DS );
}
Finally
{
If (issessionlocal)
{
Session. closeconnection ();
}
}

Return Ds. Tables [0];

}

Theoretically, there is no problem, but a few days ago I made a stored procedure with the output parameter to return the datatable. If the following executequeryfordatatable method is used, the output parameter will not be obtained, because it does not use ibatisnet internal methods when returning values, the rigorous method above should be changed to the following:

/** // <Summary>
/// Obtain the select result using a datatable (the $ placeholder parameter must be used for parameters in the XML file)
/// </Summary>
/// <Param name = "statementname"> statement id </param>
/// <Param name = "paramobject"> parameters required by the statement </param>
/// <Param name = "htoutputparameter)"> output parameter value hash table </param>
/// <Returns> the resulting able </returns>
Protected datatable executequeryfordatatable (string statementname, object paramobject, out hashtable htoutputparameter)
{
Dataset DS = new dataset ();
Bool issessionlocal = false;
Idalsession session = sqlmap. localsession;
If (session = NULL)
{
Session = new sqlmapsession (sqlmap );
Session. openconnection ();
Issessionlocal = true;
}

Idbcommand cmd = getdbcommand (statementname, paramobject );

Try
{
Cmd. Connection = session. connection;
Idbdataadapter adapter = session. createdataadapter (CMD );
Adapter. Fill (DS );
}
Finally
{
If (issessionlocal)
{
Session. closeconnection ();
}
}

Foreach (idataparameter parameter in cmd. Parameters)
{
If (parameter. Direction = parameterdirection. Output)
{
Htoutputparameter [parameter. parametername] = parameter. value;
}
}

Return Ds. Tables [0];

}

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.