2

I am facing a problem, actually I am using c3p0 for connection pooling in my project it also uses Spring,Hibernate and JSF. My problem is that in my web page we have a link named "logout" I want when user click on the logout the connection should be released and www.google.com back to the pool how is it possible.

Thanks in advance Prashant

  • I increase my accept pls answer if you know that – prashant mishra Apr 10 '12 at 10:42
  • I do not quite understand the question, since your c3p0 pool is independant from your users requests. The pool is self-managed (when a connection is not used after a certain amount of time, the connection is either closed, either put back in the pool). – ndeverge Apr 10 '12 at 11:19
  • yes u r right pool is independant from users requests but on our web page we have a link named logout. In one scenario if I have a maxPoolSize 2 and 2 users logs in then when the 3rd user try to login then he should wait for the connection to free.I want to do that if I click my logout link then user logged out and also it release the connection so that the another user waiting to acquire the connection gets the connection and do his work. – prashant mishra Apr 10 '12 at 11:27
  • Have you considered what happens if the user doesn't log out, just closes the browser window? Then the system would have to wait for the session to time out before releasing the connection. Locking resources to anything but a request-scope can have unintended consequences. – pap Apr 10 '12 at 15:09

1 Answers1

1

what you are expecting is to control the number of concurrent users logging into your system

when the 3rd user try to login then he should wait for the connection to free

Now, you can implement this using a concurrent counter

  • create a filter which filters all requests.
  • whenever a new request is created increment the counter
  • when a user logs out decrement the counter
  • when the counter hits max value make that thread wait until the slots are available.

you can control the max number of users via JMX or a seperate admin console. also, a connection should be released when the thread handling it terminates (since the session object doesn't have any references it can be GC'd and after a timeout it will be reused in the pool).

its always better to not create a bottleneck using a DB resource.

Anantha Sharma
  • 9,920
  • 4
  • 33
  • 35
  • No actually I am only trying to do that when any user log out then connection return to the pool – prashant mishra Apr 10 '12 at 11:50
  • i understand, but a connection is a resource linked to a running thread, if the thread is dead then the connection is returned to the pool (sooner or later). – Anantha Sharma Apr 10 '12 at 11:52
  • you mean there is no way to do that forefully? – prashant mishra Apr 10 '12 at 11:56
  • you can, you can store the connection in the http session.. but its not a good idea refer this question for why http://stackoverflow.com/questions/6078843/storing-database-connection-in-a-session-variable – Anantha Sharma Apr 10 '12 at 11:58