After sqlconnection calls the dispose method, can it be reused in the connection pool?

Source: Internet
Author: User

In the previous article, I briefly discussed the correct use of sqlconnection. by talking about the connection pool, I accidentally encountered a problem: After the close and dispose methods of sqlconnection are executed, is this connection object actually stored in the pool and can be reused?

This morning I took the time to read the sqlconnection close and dispose Il.CodeAfter reading it, I did not directly dispel my doubts, but made me more uncertain.

I have carefully read the replies below tonight, and I feel very ashamed. I feel that my foundation is not very solid, and I have a little doubt at the same time.

Thank you, especially, for choosing yuanyou.Rdlc2AndMilitary flag on the wastelandThey did not give me any further inspiration, and wrote a simple test code tonight:

Static void execute () {sqlconnection conn = new sqlconnection (sqlconnstring); Conn. open (); console. writeline ("current connection state: {0}", Conn. state); Conn. close (); console. writeline ("current connection state: {0}", Conn. state); thread. sleep (100); Conn. open (); console. writeline ("current connection state: {0}", Conn. state); Conn. close (); console. writeline ("current connection state: {0}", Conn. state); thread. sleep (100); Conn. open (); console. writeline ("current connection state: {0}", Conn. state); Conn. dispose (); console. writeline ("current connection state: {0}", Conn. state); console. writeline ("current connection string: {0}", Conn. connectionstring); thread. sleep (100); try {// Conn. connectionstring = sqlconnstring; // with this line added, open can execute conn normally. open (); // exception: connectionstring does not initialize console. writeline ("current connection state: {0}", Conn. state);} catch (exception ex) {console. writeline (ex. tostring ();} finally {If (Conn. state = connectionstate. open) {Conn. close ();} console. writeline ("current connection state: {0}", Conn. state );}}

The execution result is as written in a comment. After calling dispose, The connectionstring attribute of sqlconnection is changed to string. Empty.

On msdn, the connection pool creation and allocation are described as follows:

When the connection is opened for the first timeAlgorithmCreate a connection pool. This algorithm associates the pool with the connection string in the connection. Each connection pool is associated with a different connection string. When a new connection is opened, if the connection string does not exactly match the existing pool, a new pool is created. By Process and ApplicationProgramDomain, connection string, and Windows identity (when using integrated security) to establish a pool connection. The connection string must also be completely matched. The keywords provided for the same connection in different order are allocated to a separate pool.

That is to say, an important feature of the connection pool isWhich pool does the connection belong?. Which pool will a connection object with a null connection string after dispose is executed belong? Maybe this object is actually stored in a certain pool, but what kind of internal processing is done by Microsoft's connection pool? For example, how is the connection string re-allocated? Is the execution of dispose really the same as that of the close method?

I am still skeptical.

Let's say what you think.

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.