Java MySQL connetion pool is not working

后端 未结 4 989
猫巷女王i
猫巷女王i 2021-01-26 03:46

I\'ve written a function in Java that runs a MySQL query and returns results. I\'ve implemented connection pooling using this method here: http://www.kodejava.org/how-do-i-creat

相关标签:
4条回答
  • 2021-01-26 04:08

    You create a connection pool, but you don't put anything into it. You connection pool is empty when it is created, so your first request to it is guaranteed to to create a new connection and will be just as slow as getting a connection manually.

    Try putting your code into a loop, where you repeatedly get a connection from the pool. Try it once, five times, ten times and fifteen times. Note how the results change.

    Some connection pools support automatically creating and holding a minimum number of connections ready for use as well as a maximum. When the pool is initialised it will pre-fetch connections so the first few calls aren't delayed.

    0 讨论(0)
  • 2021-01-26 04:12

    You will not see a difference with a single request to the DB. You have to test this this with multiple (concurrent) requests.

    And BTW: You you obeye the Java naming convention and start all objects with lower case.

    0 讨论(0)
  • 2021-01-26 04:19

    Your use case does not warrant a connection pool. Its just one connection you need. The pool class you are using GenericObjectPool has a method addObject();. This will create and add the objects to the pool. This will avoid creating connection later.

    0 讨论(0)
  • 2021-01-26 04:22

    Your can pre-load your pool by calling connectionPool.addObject(). At the start it is empty and creates a connection only after you've posted a query.

    0 讨论(0)
提交回复
热议问题