John Smith
John Smith

Reputation: 45

How to verify that connection pooling is working

I have set up connection pooling in my Tomcat configuration, but now I want to verify that it is actually working.

Is there a way to dump out some sort of ID of the active connection so that I can verify the same one is being used between requests? I have checked Oracle's Connection Documentation but to no avail.

Thanks in advance!

Upvotes: 3

Views: 5024

Answers (3)

DuncG
DuncG

Reputation: 15136

A simple way to check pool members are re-used: If your JDBC vendor is using the standard toString from Object you should see the same values printed when you print the connection:

System.out.println("Connection="+conn);

If this changes each pool get call, then the connection is not the same as before. However this may not help you at all if your DataSource is wrapping a pooled connection each time with it's own handler class - typically done to make close() return to DataSource and keeps the underlying Connection open.

If your JDBC vendor has not used standard toString() you can make your own string to use in debug / test statements:

public String toString(Connection conn) {
    return conn.getClass().getName() + "@" + Integer.toHexString(conn.hashCode());
}

System.out.println("Connection="+toString(conn));

Note that the above approach does not guard against rogue code changing elements of the Connection or leaving it in in-determinate state. For example I've seen: altered auto-commit modes, selecting another default database database schema (Sybase), not committing the previous transaction!

For some DBs you can mitigate with a test query before use but this incurs an overhead.

Upvotes: 2

Olaf Kock
Olaf Kock

Reputation: 48057

There's a simple answer and one that makes more work: If you configure a connection pool and don't explicitly open a connection to your database anywhere in your code, the mere nonexistence of manual connection creation should be a clue that something in your connection pool works.

As the connection pool comes from Tomcat, it will also be contained in the data that you can tap into through JMX - enable JMX and connect with your jconsole. This will give you information about the exact load (used connections, free connections, pre-allocated connections) of your connection pool any time you look.

Upvotes: 0

Husqvik
Husqvik

Reputation: 5809

Simple check would be

SELECT SID, SERIAL# FROM V$SESSION WHERE SID = SYS_CONTEXT('USERENV', 'SID')

if your pool size is 1 you will get the same values from any connection object. If your pool size is greater (it also depends if you have fixed pool size or if it is set to grow when needed) and you have many active connections at the same time you should get up the pool size number of distinct twins.

If the connection is non-pooled creating and opening a new connection object will every time return different values.

Upvotes: 0

Related Questions