Key returned for inserted row: 2
At times, it can be tricky to use the
is scoped to a connection. So, if some other query happens on the same connection, the value is
overwritten. On the other hand, the
instance, so it can be used even if other queries happen on the same connection, but not on the same
Statement
20.3.7. Connection Pooling with Connector/J
Connection pooling is a technique of creating and managing a pool of connections that are ready for
use by any
Java application, while reducing overall resource usage.
How Connection Pooling Works
Most applications only need a thread to have access to a JDBC connection when they are actively
processing a transaction, which often takes only milliseconds to complete. When not processing a
transaction, the connection sits idle. Connection pooling enables the idle connection to be used by
some other thread to do useful work.
In practice, when a thread needs to do work against a MySQL or other database with JDBC, it requests
a connection from the pool. When the thread is finished using the connection, it returns it to the pool, so
that it can be used by any other threads.
When the connection is loaned out from the pool, it is used exclusively by the thread that
requested it. From a programming point of view, it is the same as if your thread called
DriverManager.getConnection()
pooling, your thread may end up using either a new connection or an already-existing connection.
Benefits of Connection Pooling
The main benefits to connection pooling are:
• Reduced connection creation time.
Although this is not usually an issue with the quick connection setup that MySQL offers compared to
other databases, creating new JDBC connections still incurs networking and JDBC driver overhead
that will be avoided if connections are recycled.
• Simplified programming model.
When using connection pooling, each individual thread can act as though it has created its own
JDBC connection, allowing you to use straightforward JDBC programming techniques.
• Controlled resource usage.
If you create a new connection every time a thread needs one, rather than using connection pooling,
your application's resource usage can be wasteful and lead to unpredictable behavior under load.
Using Connection Pooling with Connector/J
Sun has standardized the concept of connection pooling in JDBC through the JDBC 2.0 Optional
interfaces, and all major application servers have implementations of these APIs that work with MySQL
Connector/J.
Generally, you configure a connection pool in your application server configuration files, and access it
through the Java Naming and Directory Interface (JNDI). The following code shows how you might use
a connection pool from an application deployed in a J2EE application server:
Connection Pooling with Connector/J
instance.
thread
that needs them. Connection pooling can greatly increase the performance of your
SELECT LAST_INSERT_ID()
getGeneratedKeys()
every time it needed a JDBC connection. With connection
2096
query, as that function's value
method is scoped by the
Statement
Need help?
Do you have a question about the 5.0 and is the answer not in the manual?
Questions and answers