<small id='WPZI9'></small><noframes id='WPZI9'>

    1. <legend id='WPZI9'><style id='WPZI9'><dir id='WPZI9'><q id='WPZI9'></q></dir></style></legend>
    2. <i id='WPZI9'><tr id='WPZI9'><dt id='WPZI9'><q id='WPZI9'><span id='WPZI9'><b id='WPZI9'><form id='WPZI9'><ins id='WPZI9'></ins><ul id='WPZI9'></ul><sub id='WPZI9'></sub></form><legend id='WPZI9'></legend><bdo id='WPZI9'><pre id='WPZI9'><center id='WPZI9'></center></pre></bdo></b><th id='WPZI9'></th></span></q></dt></tr></i><div id='WPZI9'><tfoot id='WPZI9'></tfoot><dl id='WPZI9'><fieldset id='WPZI9'></fieldset></dl></div>
    3. <tfoot id='WPZI9'></tfoot>
          <bdo id='WPZI9'></bdo><ul id='WPZI9'></ul>

        MySQL - 持久连接与连接池

        时间:2023-06-25

        <tfoot id='AdSDj'></tfoot>
          <legend id='AdSDj'><style id='AdSDj'><dir id='AdSDj'><q id='AdSDj'></q></dir></style></legend>

            <i id='AdSDj'><tr id='AdSDj'><dt id='AdSDj'><q id='AdSDj'><span id='AdSDj'><b id='AdSDj'><form id='AdSDj'><ins id='AdSDj'></ins><ul id='AdSDj'></ul><sub id='AdSDj'></sub></form><legend id='AdSDj'></legend><bdo id='AdSDj'><pre id='AdSDj'><center id='AdSDj'></center></pre></bdo></b><th id='AdSDj'></th></span></q></dt></tr></i><div id='AdSDj'><tfoot id='AdSDj'></tfoot><dl id='AdSDj'><fieldset id='AdSDj'></fieldset></dl></div>
              <bdo id='AdSDj'></bdo><ul id='AdSDj'></ul>

                <tbody id='AdSDj'></tbody>
              • <small id='AdSDj'></small><noframes id='AdSDj'>

                • 本文介绍了MySQL - 持久连接与连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  为了避免每次需要针对 MySQL 触发查询时建立新连接的开销,有两个选项可用:

                  In order to avoid the overhead of establishing a new connection each time a query needs fired against MySQL, there are two options available:

                  1. 持久连接,即请求新连接,检查是否已打开相同"连接,如果已打开,则使用它.
                  2. 连接池,客户端维护一个连接池,以便每个需要使用连接的线程都会从池中检出一个,并在完成后将其返回到池中.

                  那么,如果我有一个多线程服务器应用程序预计每秒处理数千个请求,并且每个线程都需要对数据库进行查询,那么什么是更好的选择?

                  So, if I have a multi-threaded server application expected to handle thousands of requests per second, and each thread needs to fire a query against the database, then what is a better option?

                  根据我的理解,对于持久连接,我的应用程序中的所有线程都将尝试使用相同的持久连接到数据库,因为它们都使用相同的连接.所以它是一个跨多个应用程序线程共享的连接——因此请求很快就会在数据库端阻塞.

                  From my understanding, With persistent connections, all the threads in my application will try and use the same persistent connection to the database because they all are using identical connections. So it is one connection shared across multiple application threads - as a result the requests will block on the database side soon.

                  如果我使用连接池机制,我将使所有应用程序线程共享一个连接池.所以阻塞请求的可能性较小.但是,对于连接池,应用程序线程是否应该等待从池中获取连接,还是应该以循环方式向池中的连接发送请求,并让队列(如果有)发生在数据库上?

                  If I use a connection pooling mechanism, I will have all application threads share a pool of connections. So there is less possibility of a blocking request. However, with connection pooling, should an application thread wait to acquire a connection from the pool or should it send a request on the connections in the pool anyway in a round-robin manner, and let the queuing if any, happen on the database?

                  推荐答案

                  拥有持久连接并不意味着所有线程都使用相同的连接.它只是说"您保持连接打开(与每次需要连接时打开连接相矛盾).打开连接是一项代价高昂的操作,因此 - 一般而言 - 您应尽量避免不必要地频繁打开连接.

                  Having persistent connections does not imply that all threads use the same connection. It just "says" that you keep the connection open (in contradiction to open a connection each time you need one). Opening a connection is an expensive operation, so - in general - you try to avoid opening connections more often than necessary.

                  这就是多线程应用程序经常使用连接池的原因.池负责打开和关闭连接,每个需要连接的线程都会从池中请求一个.重要的是要注意线程尽快将连接返回到池中,以便另一个线程可以使用它.

                  This is the reason why multithreaded applications often use connection pools. The pool takes care of opening and closing connections and every thread that needs a connection requests one from the pool. It is important to take care that the thread returns the connection as soon as possible to the pool, so that another thread can use it.

                  如果您的应用程序只有几个需要连接的长时间运行的线程,您还可以为每个线程打开一个连接并保持打开状态.

                  If your application has only a few long running threads that need connections you can also open a connection for each thread and keep this open.

                  仅使用一个连接(如您所描述的那样)等于最大大小为 1 的连接池.这迟早会成为您的瓶颈,因为所有线程都必须等待连接.这可能是序列化数据库操作的一个选项(按特定顺序执行它们),尽管有更好的选项来确保序列化.

                  Using just one connection (as you described it) is equal to a connection pool with the maximum size one. This will be sooner or later your bottleneck as all threads will have to wait for the connection. This could be an option to serialize the database operations (perform them in a certain order), although there are better options to ensure serialisation.

                  这篇关于MySQL - 持久连接与连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:MySQL LEFT JOIN 3 个表 下一篇:如何使用 MySQL 设置 ASP.NET MVC 2?

                  相关文章

                  1. <legend id='iZ5i7'><style id='iZ5i7'><dir id='iZ5i7'><q id='iZ5i7'></q></dir></style></legend>
                    • <bdo id='iZ5i7'></bdo><ul id='iZ5i7'></ul>
                    <tfoot id='iZ5i7'></tfoot>
                    <i id='iZ5i7'><tr id='iZ5i7'><dt id='iZ5i7'><q id='iZ5i7'><span id='iZ5i7'><b id='iZ5i7'><form id='iZ5i7'><ins id='iZ5i7'></ins><ul id='iZ5i7'></ul><sub id='iZ5i7'></sub></form><legend id='iZ5i7'></legend><bdo id='iZ5i7'><pre id='iZ5i7'><center id='iZ5i7'></center></pre></bdo></b><th id='iZ5i7'></th></span></q></dt></tr></i><div id='iZ5i7'><tfoot id='iZ5i7'></tfoot><dl id='iZ5i7'><fieldset id='iZ5i7'></fieldset></dl></div>

                    <small id='iZ5i7'></small><noframes id='iZ5i7'>