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?
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.
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.
Related
I am really getting confused between these 2 now as below :
1. is the returning of only a Singleton instance to a db connection during the entire run time of a JAVA app
2. is the concept of Thread pool connections in general... I mean if we are planning to have only a singleton instance to a db connection obj, why even have a concept of pools, though i do get what exactly a pool is used for
Are these 2 not very opposite concepts, or am i mixing up anything here...?
is the returning of only a Singleton instance to a db connection during the entire run time of a JAVA app?
you may not want to return a Singleton object for a database connection. You can choose to do if database concurrency not required. In a multi threaded environment best option is to go for Connection pool.
is the concept of Thread pool connections in general..
Establishing a database connection is a very resource-intensive process and involves a lot of overhead. Moreover, in a multi-threaded environment, opening and closing a connection can worsen the situation greatly.
Creating JNDI in server and use it in your web app.
Context context=new InitialContext();
DataSource dataSource=(DataSource)
context.lookup("jdbc/test_jndi");
so when DataSource uses connection pooling, the lookup return a connection from the pool of available connection objects. If there is no available connection, the lookup creates a new connection.
Connection connection=dataSource.getConnection
("testuser","testpwd");
// ...
connection.close();
Once the application is done with database processing, it explicitly closes the connection. This makes the connection available again for reuse again. The closing event of the pooled connection signals the pooling module to restore back to the connection pool.
Any resource shared requires overhead of handling concurrent access so you want to reduce that by not having singletons always. Moreover to reduce resource-intensive process connection pooling is preferred.
I am facing some use related to GET_LOCK in MySQL. I am using c3p0 connection provider. Does getConnection() method in c3P0 create new connection every time or reuse the connection?
getConnection on DataSource gets the connection from connection pool. If there isn't any available and maxPoolSize isn't reached then it opens new connection. If the maxPoolSize is reached then it waits for some connection to return to the pool. (it must be released by the thread which is using it)
After the thread releases the connection then it is returned to the pool and might be reused by some other thread.
The documentation is http://www.mchange.com/projects/c3p0/
c3p0 maintains a pool of Connections, which are reused for multiple clients.
however, you cannot make any assumption that a Connection you use will be the same Connection you will see again. you might receive any Connection from c3p0's pool, there's no guarantee or likelihood you'll see one you've already seen. and, depending on your configuration, c3p0 tests, expires, and replaces Connections behind the scenes.
if you are trying to associate a lock with a Connection in one client session and then release it in another, well, that won't work and will break things fast. really, with any explicit locking, your acquisition and release should be done in one client session, using try / finally semantics to ensure lock release.
Which one is better approach?
a)Get connection from Connection Pool at the beginning of doXXX() method and close at the end.
Use this connection throughout doXXX() method. This way it will fetch connection only once from the connection pool but Connection will be open for the whole time it takes to execute doXXX().
b)Get connection from Connection Pool for each db operation and close.
It will fetch connection from the connection pool for every db operation but will be closed immediately.
It depends. Getting a connection from the pool, worst case could result in the expensive creation of a new connection which might be slow. Between DB calls, how long is it going to be doing stuff? In general, releasing after each operation will slow things down for the specific process, but speed things up for other processes, and vice versa. You only really have to worry about this under massive load.
I am trying to use HikariCP connection pool. I was able to get it to work and get a connection that I could use. I am not sure what is the best approach for returning the connection to the pool.
I have the following questions:
Should I close the connection when I am done, rely on idleTimeout
and maxLifetime settings or is there another call that I can use so
as not to hog the connections from the pool?
If I close the connections (instead of returning to the pool), would
that not result in additional connection objects being created
to meet the requirements of the connection pool size?
Looking for helpful suggestions.
As with most connection pools, Hikari doesn't give you an actual JDBC Connection when you ask for one. What it does instead is give you a proxy that implements the Connection interface. In the case of Hikari - it's a ConnectionProxy object.
This proxy serves a few purposes, the main of which is - take the control of opening/closing connections and statements away from you and into the connection pool. This happens automagically and you should be using your connections as usual. This includes closing them after use.
If you look at the source code for Hikari, at the ConnectionProxy class in particular, you will see that the close() method is very different from the standard one. The code reads as:
Mark the connection as closed, do cleanup, reset underlying connection state and params.
Hence, simply calling close() will just clean and return the connection to the pool.
Example code in "http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html" explicitly closes a connection after it is used.
However according to my understanding, should not it be the connection pool's responsibility to manage active and idle connections?
Why would not I want a connection to be re-used by another transaction?
You must close the connection so that you can release it back to the pool. The "Connection" you get from the pool does not, per se, represent the actual, physical connection to the DB. Rather it's a wrapper. So, closing the connection informs the pool that it is free for use by other clients of the pool.
You need to call Connection.close() to return the connection to the pool, it doesnt actually close the underlying connection.