Single connection with Oracle

只愿长相守 提交于 2020-02-02 10:40:30

问题


In my project, developers use a single instance of Connection instead of a connection pool on an Oracle 12c.

Using a pool is a common practice and Oracle itself documents it: http://docs.oracle.com/database/121/JJUCP/get_started.htm#JJUCP8120.

But JDBC 4.2 specification says:

13.1.1 Creating Statements

Each Connection object can create multiple Statement objects that may be used concurrently by the program.

Why using a pool of connections instead of a single connection, if it's possible to use statements to manage concurrency?


回答1:


The Oracle Database Dev Team strongly discourages using a single Connection in multiple threads. That almost always causes problems. As a general rule we will not consider any problem report that does this.

A Connection can have multiple Statements and/or ResultSets open at one time but only one can execute at a time. Connections are strictly single threaded and blocking. We try to prevent multiple threads from accessing a Connection simultaneously but there are a few odd cases where it is possible. These are all but guaranteed to cause problems. (It is not practical to fix or prevent these cases mostly for performance reasons. Just don't share a single Connection across multiple threads.)




回答2:


If a client connects to the database via a dedicated server connection then that database session will only serve that client . If the client connects to the database via shared server connection, then a given database session may serve multiple clients over its lifetime.

This is documented here.

Also, at any one point in time, a session can only execute one thing at a time. If that wasn't the case, then running things in parallel wouldn't spawn multiple other sessions!




回答3:


A single connection cannot execute several statements concurrently.




回答4:


Yes one connection can execute more that one statement. It will be the programmer to chose connection pooling setting or multiple statements when executing over more than one thread. Most databases in the market can handle multiple statements in one connection.



来源:https://stackoverflow.com/questions/42790476/single-connection-with-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!