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.