Prepared Statements along with Connection Pooling

后端 未结 5 1357
走了就别回头了
走了就别回头了 2021-01-02 14:33

I have a question regarding general use of Prepared Statement along with connection pooling.

Prepared Statements are generally tied to one connection only.In our app

5条回答
  •  南方客
    南方客 (楼主)
    2021-01-02 14:58

    Assuming that this is a multi-threaded application, Connection objects are typically associated with a single thread at any instant of time. Connection objects acquired by a thread are not returned to the pool until they are closed. This applies both to the logical connection wrapper (that is typically returned by a DataSource managed by an application server) to the application, as well as to the physical connection. Also, physical connections can be shared across multiple logical connections as long as they are part of the same transaction.

    This means that if a logical connection handle is returned to your application, it is not necessary that the underlying physical connection is the same and is being contended for (unless it is part of the same transaction). If your application is expected to handle concurrent users without any hassle, a Connection object would be created in every thread starting a transaction, and this object would not be contended for, across threads. Under the hood, different physical connections in the pool would be executing the SQL queries associated with the prepared statements, across multiple threads, again without any contention.

提交回复
热议问题