The database connection pool is where connection is put into threadlocal.

Source: Internet
Author: User

I have a few points that do not quite understand, look at you under the guidance of the Warrior.
1, the application of the Java EE, there is a user request will start a thread. And if we put connection in threadlocal, then our program only needs a connection to connect to the database, each thread is a copy of the connection, then why is it necessary to database connection pool?
2. In this case, the connection in a replica performs a close operation, and none of the other is executed. So you want to ask when the real connection connected to the database is closed?

3, obviously above the first question is not set up, but I hope that a hero to help me explain. Explain when to fetch connection from the database connection pool and when did you get a threadlocal copy of connection?

Answer 1:

Because one transaction in the request involves multiple DAO operations, the Connection in these DAO
cannot be obtained from the connection pool, if obtained from the connection pool, two DAO will use two
Connection, there is no way to complete a transaction.

If the Connection in DAO is obtained Connection from ThreadLocal, then
These DAO will be included under the same Connection. Of course, in that case,
DAO can not turn off the Connection, turn off, the next user will not be able to use.

As the 11 floor says, a transaction involves multiple DAO operations.

Reply 2:

... First, the LZ is a conceptual error. What is a thread pool and what is Threadlocal???
    thread pool, in order to avoid unnecessary creation, destroy connection and exist, including activities, wait, the minimum properties, Cop3,proxy connection pool can be configured these things;
   
    Why use threadlocal? This has nothing to do with the connection pool, and I think it's more about the program itself, for a clearer explanation, I'll give you an example
Get a connection in    servlet. First, is the servlet thread-safe?
     class myservlet extends httpservlet{
          private Connection conn;
     }
     ok, sorry to tell you, this conn is not secure, all requests for this servlet connection, use is a connection, This is deadly. Multiple people use the same connection, counting the delay what, God knows what the data will be like.
      so we want to make sure that connection is unique to every request. This time you can use threadlocal to ensure that each thread has its own connection.
      Change to  private threadlocal<connection> ct = new  ThreadLocal<Connnection> ();
      then get connection,set from the connection pool to the CT, get on the line, as to which connection is the connection pool problem, you can not control.

In the database connection pool is the connection put into threadlocal

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.