WSO2连接池处理机制咨询:JNDI数据源下JDBC连接每次开闭的行为
Great question—this is a common point of confusion when working with connection pools, so let’s break down exactly what’s happening in your WSO2 setup:
When your colleague’s code calls conn.close() on a connection obtained from your configured JNDI pooled datasource, the physical database connection is NOT actually closed. Instead, WSO2’s connection pool manager (which typically uses libraries like Apache Commons DBCP or HikariCP under the hood, depending on your WSO2 product version) intercepts this close() call.
Here’s the step-by-step behavior:
- The pool cleans up the connection: resets auto-commit settings, clears any pending statements or result sets, and ensures it’s in a clean state for reuse.
- It marks the connection as "idle" and returns it to the pool’s available connection pool.
- This idle connection is then ready to be handed out to another client request that needs a database connection, avoiding the overhead of establishing a new physical connection each time.
Importantly, your colleague’s pattern of opening and closing connections per query is actually the correct practice when using connection pools. Even though the physical connection isn’t closed, calling close() tells the pool that the connection is no longer needed by that specific request, preventing connection leaks. If the code didn’t call close(), the connection would stay marked as "in use" and might eventually exhaust the pool.
Just to confirm: WSO2’s pooled datasources follow standard connection pool conventions here—so you don’t need to modify your colleague’s code to avoid closing connections. The pool handles the heavy lifting of reusing physical connections efficiently.
内容的提问来源于stack exchange,提问作者sercasti

