0

I have to implement my own connection pool, and I want the connection will automatically return to the pool after some CONNECTION_TIMEOUT. How can I achive that?

Everything that comes to mind is to use ScheduledExecutorService in a separate thread and replan it each time the connection is used.

Any other ideas?

VB_
  • 45,112
  • 42
  • 145
  • 293

2 Answers2

1

You can use the client petition for a pooled connection, for triggering some actions, for example, check the timeout for currently used connections, and realease the currently used connections which timeout has expired. You will avoid using a new thread.

  • So I need to write some wraper on Connection and lead in field that contain the last time of last_connection_usage_date. If (currendDate - last_connection_usage_date) > TIMEOUT than return connection to the pool. But in such case if noone trigger the connection's method, connection never be returned to the pool. For example, we have pool that contains 10 connections. If they all was taken and never used after TIMEOUT appears situation: we have 10 TIMEOUTED connections, but zero in pool. – VB_ Jun 19 '13 at 10:25
  • You are fully right, but in my opinion, you need the connections when a client ask to the pool for a connection. So, in the scenario you are mentioning, when client ask to the pool for a connection, the client will get a connection. But you are right, the release of connections by TIMEOUT, is not a "synchronous" process. –  Jun 19 '13 at 10:31
  • Do you mean that I should check connection TIMEOUT when new client acquire the connection from pool? In your words each time client acquire connection, I should iterate all issued connections and check TIMEOUT. But I suggest that my pool is a stack, and it doesn't hold references to issued connections. Thank you for your responses ) – VB_ Jun 19 '13 at 10:41
  • Am I properly understand you? – VB_ Jun 19 '13 at 10:59
  • Yes, you properly understood it. If you don't rely that user is responsible for returning connections to the pool, and, trying to avoid using a thread, you could use an approach as I mentioned. But you would need hold references to issued connections. –  Jun 19 '13 at 12:50
  • I'll act in a such way. Thanks – VB_ Jun 19 '13 at 13:09
0

If a user of your connection pool borrowed a connection then it's the user responsibility to return it to the pool. The connection cannot return itself to the pool.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Yes, but if it is very loaded resource we can't rely on the user, because it will lead to DoS. And even if it is user responsibility, I want to find out the best way to implement it in such way (connection returns automatically). – VB_ Jun 19 '13 at 10:53
  • I can only imaging this: on timeout you call Connection.abort (available since 1.7 only) and replace it with a new connection – Evgeniy Dorofeev Jun 19 '13 at 11:02
  • Yes, I even can write my own wrapper over Connection. But the main problems is how to organize triggering abort function on timeout. I mean that it seams to me a bad idea to run separate thread via ScheduledExecutor. And I can't imagine another way to solve this problem. – VB_ Jun 19 '13 at 12:42