Reputation: 12101
I have a problem that thought I will break down to the simplest. There two applications on LAMP stack, one PHP and another Java that do the only and exactly the same thing: run a simple query:
SELECT * FROM test
PHP execution takes 30 ms in total
Java excution takes 230 ms in total
Query run on a local MySQL client takes 10-15 ms in total
Java takes ~200 ms roughly every time only to establish a connection to the db. I understand that PHP uses some kind of built in connection pooling, therefor it doesn't need to establish a new connection every time and only takes 30 ms as a result of it.
Is the same thing possible on Java? So far I failed to achieve that. I tried to use Apache Commons DBCP connection pooling, no change at all, still takes the same time to connect to the database.
UPDATE: This is a separate question where I'm trying to make connection pooling work on Java, for those who are asking for a code example: Java MySQL connetion pool is not working
Upvotes: 3
Views: 1904
Reputation: 29759
You are misunderstanding the concept and purpose of connection pooling.
Connection pooling is meant to maintain a (set of) connections on a single (Java Virtual) machine (typically, an application server). The goal is to allow multiple threads on the same machine to essentially share their connection to the database server without the need to open one everytime they need to query the database.
Stand-alone applications cannot share connections as they run on different Java Virtual Machines, or perhaps even on different physical machines.
Connection pooling would help in your stand-alone application if you had several threads making concurrent access to the database.
However, you can still measure the benefit of connection pooling by wrapping your test (the code inside your try...catch
) in a loop and iterating a few times. At the first iteration, the connection needs to be opened. Don't forget to release the connection (call Con.close()
), then it would be reused at the next iteration.
Upvotes: 5