Releasing connections from connection pool - java

In our code (which runs as a schedule job via timer), we have threads running in parallel to perform a database operation. Problem here is each thread is initiating a connection via Hibernate factory. These connections are closed after every database action but stil gets stocked in the connection pool(as INACTIVE). All the connections gets released only after the job/main process is killed.Is there any way to release the connection even from connection pool after the database operation. When we use cron job instead of timer, the process gets killed automatically but we dont need cron here.
Kindly help us to resolve this as we are already nearing production release.
Note : We came to know about this when QA tested with heavy load on the job and for each load new connections are pulled.

You need to restrict the number of threads getting created in the thread pool.
dotConnect for Oracle uses connection pooling. The OracleConnection connection string has the Pooling parameter. If Pooling=true (the default value), the connection is not deleted after closing it, it is placed to the pool instead. When a new connection with the same connection string is opened, it is taken from the pool (if there are free connections) instead of the creating a new one. This provides significant performance improvements. If you use 800 connections that are connected for 10-15 seconds each, and there are only few different connection strings, you may not have 800 actual connections. Closed connections will be placed to the pool, and they will be taken from the pool when a new connection with the same connection string will open. No additional connection will open in such case.
You can disable Pooling by adding 'Pooling=false' to the connection string. In such case, a connection will be deleted from memory and free the session. However this may lead to performance loss.
Most likely, pooling should not cause creating too much sessions. Try testing your application with pooling on. If the session number will be too large, you can disable pooling.
For more information, please refer to http://www.devart.com/dotconnect/oracle/docs/FAQ.html#q54

I have found the root cause for the issue and have also found the solution.
The root cause was number of connections set as minimum and maximum and the time out parameter.
The minimum was 5 and max was 20 and timeout was 800 seconds. But out job was scheduled to run every minute. Due to the configuration, the connections were not released properly within minute.
Another issue was our code was not using the session factory as singleton, but was initializing for each thread. Since the resource was not shared, each session factory creates 5 connections by default and extended to 20 max. Since the timeout also was higher before the connections are released, next set of job starts and creates its own set of new connections.
Finally the pool gets full and oracle becomes unavailable.
We fixed this by sharing the session object across and also setting the timeout to lesser value so that connections are getting released from pool.

Related

Should I have a single database connection or one connection per task? [duplicate]

This question already has answers here:
How to manage db connections on server?
(3 answers)
Closed 7 years ago.
I have a Java server and PostgreSQL database.
There is a background process that queries (inserts some rows) the database 2..3 times per second. And there is a servlet that queries the database once per request (also inserts a row).
I am wondering should I have separate Connection instances for them or share a single Connection instance between them?
Also does this even matter? Or is PostgreSQL JDBC driver internally just sending all requests to a unified pool anyway?
One more thing should I make a new Connection instance for every servlet request thread? Or share a Connection instance for every servlet thread and keep it open the entire up time?
By separate I mean every threads create their own Connection instances like this:
Connection connection = DriverManager.getConnection(url, user, pw);
If you use a single connection and share it, only one thread at a time can use it and the others will block, which will severely limit how much your application can get done. Using a connection pool means that the threads can have their own database connections and can make concurrent calls to the database server.
See the postgres documentation, "Chapter 10. Using the Driver in a Multithreaded or a Servlet Environment":
A problem with many JDBC drivers is that only one thread can use a
Connection at any one time --- otherwise a thread could send a query
while another one is receiving results, and this could cause severe
confusion.
The PostgreSQL™ JDBC driver is thread safe. Consequently, if your
application uses multiple threads then you do not have to worry about
complex algorithms to ensure that only one thread uses the database at
a time.
If a thread attempts to use the connection while another one is using
it, it will wait until the other thread has finished its current
operation. If the operation is a regular SQL statement, then the
operation consists of sending the statement and retrieving any
ResultSet (in full). If it is a fast-path call (e.g., reading a block
from a large object) then it consists of sending and retrieving the
respective data.
This is fine for applications and applets but can cause a performance
problem with servlets. If you have several threads performing queries
then each but one will pause. To solve this, you are advised to create
a pool of connections. When ever a thread needs to use the database,
it asks a manager class for a Connection object. The manager hands a
free connection to the thread and marks it as busy. If a free
connection is not available, it opens one. Once the thread has
finished using the connection, it returns it to the manager which can
then either close it or add it to the pool. The manager would also
check that the connection is still alive and remove it from the pool
if it is dead. The down side of a connection pool is that it increases
the load on the server because a new session is created for each
Connection object. It is up to you and your applications'
requirements.
As per my understanding,You should defer this task to the container to manage connection pooling for you.
As you're using Servlets,which will be running in a Servlet container, and all major Servlet containers that I'm aware of provide connection pool management.
See Also
Best way to manage database connection for a Java servlet

c3p0 maxIdleTime is same as wait_timeout of mysql?

I am having an Spring MVC + Mysql (JDBC 4) + c3p0 0.9.2 project.
In c3p0 maxIdleTime value is 240 (i.e 4 mins.) and wait_timeout in my.ini of Mysql to 30 seconds.
According to c3p0
maxIdleTime:
(Default: 0)
Seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire.
According to Mysql
wait_timeout: The number of seconds the server waits for activity on a
noninteractive connection before closing it.
Now i am having some douts on this:(some answers are known to me,Just wated to be sure I am correct or not)
unused connection means the connection which are in sleep state according to mysql(?)
What is interactive and noninteractive connections?
Is unused connections and noninteractive coonections are same? because my DBA set wait_timeout to 30 seconds (he come to this value by observing DB server so that very less amount of connections be in sleep mode) this means an connection can be in sleep mode for 30 seconds after that it will be closed but at the otherhand c3p0's maxIdleTime is set to 240 seconds so whats this maxIdleTime setting playing role in this case.
What is interactive_timeout?
First Let's understand the mysql properties.
interactive_timeout : interactive time out for mysql shell sessions
in seconds like mysqldump or mysql command line tools. connections are in sleep state. Most of the time this is set to higher value because you don't want it to get disconnected while you are doing something on mysql cli.
wait_timeout
: the amount of seconds during inactivity that MySQL will wait before
it will close a connection on a non-interactive connection in
seconds. example: connected from java. connections are in sleep state.
Now let's understand c3po properties and it's relation with DB props.(I am just gonna copy from your question)
maxIdleTime: (Default: 0) Seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never
expire.
This refers to how long a connection object can be usable and will be available in pool. Once the timeout is over c3po will destroy it or recycle it.
Now the problem comes when you have maxIdleTime higher then the wait_timeout.
let's say if the mxIdleTime : 50 secs and wait_timeout : 40 s then there is a chanse that you will get Connection time out exception: Broken Pipe if you try to do any operation in last 10 seconds. So maxIdelTime should always be less then wait_timeout.
Instead of maxIdleTime you can you the following properties.
idleConnectionTestPeriod sets a limit to how long a connection will
stay idle before testing it. Without preferredTestQuery, the default
is DatabaseMetaData.getTables() - which is database agnostic, and
although a relatively expensive call, is probably fine for a
relatively small database. If you're paranoid about performance use a
query specific to your database (i.e. preferredTestQuery="SELECT 1")
maxIdleTimeExcessConnections will bring back the connectionCount back
down to minPoolSize after a spike in activity.
Please note that any of the pool property(eg. maxIdleTime) only affects to connection which are in pool i.e if hibernate has acquired a connection and keeps it idle for than maxIdleTime and then tries to do any operation then you will get "Broken Pipe"
It is good to have lower wait_timeout on mysql but It's not always right when you have an application already built.
You have to make sure before reducing it that in your application you are not keeping connection open for more that wait_time out.
You also have to consider that acquiring a connection is expensive task and if have wait time out too low then it beats the whole purpose of having connection pool, as it will frequently try to acquire connections.
This is especially important when you are not doing connection management manually for example when you use Spring transnational API. Spring starts transaction when you enter an #Transaction annotated method so it acquires a connection from pool. If you are making any web service call or reading some file which will take more time than wait_time out then you will get exception.
I have faced this issue once.
In one of my projects I had a cron which would do order processing for customers. To make it faster I used batch processing. Now once I retrieve a batch of customers and do some processing(no db calls). When I try to save all the orders I used to get broken pipe exception. The problem was my wait_timeout was 1 minute and order processing was taking more time then that. So We had to increase it to 2 minutes. I could have reduced the batch size but that was making the overall processing slower.
unused connection means the connection which are in sleep state according to mysql(?)
According to mysql, this simply means that a connection was established with mysql/db, but there has been no activity here for the past amount of time and due to configuration / settings of mysql(which can be changed), the connection was destroyed.
What is interactive and noninteractive connections?
Interactive connections are when your input hardware(keyboard) interacts using command line with mysql. In short where you write the queries
Non interactive or rather wait_timeout queries are those for which your code establishes connection with mysql.
Is unused connections and noninteractive coonections are same? because my DBA set wait_timeout to 30 seconds (he come to this value by observing DB server so that very less amount of connections be in sleep mode) this means an connection can be in sleep mode for 30 seconds after that it will be closed but at the otherhand c3p0's maxIdleTime is set to 240 seconds so whats this maxIdleTime setting playing role in this case.
MaxIdleTime is done by your code at hibernateJpa Configuration where you ask your code itself to close a hibernate connection(for example) after a connection is unused. You have ownership of this as a coder.
Wait_timeout on other hand is from mysql side. So it is upon the DB administrator to set it up and change.
What is interactive_timeout?
Again, interactive timeout is when you are writing queries after connecting to mysql from keyboard on command line and that time conf in mysql gets up.
If you want to know more about how to change these values, go through this link:
http://www.serveridol.com/2012/04/13/mysql-interactive_timeout-vs-wait_timeout/
Hope now it is clear to you.:)

How to make Oracle UCP always store several available connections

I'm creating connection manager class that would return java.sql.Connection to the customers of the class.
My goal is to always have 2 available connections in pool so I wouldn't loose time for creating connections. When I return the available connection, I need to make Oracle UCP create new available connection, so it would be always 2 connections available.
The problem is Oracle UCP doesn't have an option to control it. I've read the UCP documentation, but hadn't found any solution.
There is setMinPoolSize() method, but it controls available + borrowed connections, not only the available ones.
Also there is a harvestable connection functionality, but it harvests existing (borrowed) connections instead of creating new.
Note:
I'm using Oracle 11.2.0.3 and the latest ucp.jar (for Oracle 11.2.0.3)
There is no such solution. When a connection is requested from a connection pool, it is either checked out from the pool, or a new connection is created. The cost of this is incurred on the Thread that does the request. If you would always want to maintain 2 more connections in the pool than in use, you are simply moving the problem: when a connection is checked out and available < 2 then a Connection will be added. The cost incurred once again on the Thread requesting the connection (yes in theory this could be offloaded to a separate thread/executor, but that would complicate the pool).
A connection pool is intended to reduce the cost of creating connections, by keeping the connection available for re-use and thus 'spreading the cost', it is not intended to do away with the cost entirely.

Close JDBC connections after a set timeout period?

Is there a way to close JDBC connections after a set timeout period? These connections are being created in a GenericObjectPool. I know the pool can close idle connections in the pool, but what about connections that are thought to be active? I am trying to control connections leaks in the event someone doesn’t call close(). From what I have read the only way may be to set a timeout period on the server, but I am hoping to find a way in Java. Thank you!
I agree with Peter Lawrey that I would make sure to close the connection always. But if I still have to ensure that a connection is closed (if someone took it from the pool and forgot to return it), I would do it as follows:
Decorate the java.sql.Connection that is returned by the pool.
Create a timer in the constructor and set it to duration configured, to tolerate with active connections.
If the user calls close on it before the timer fires, I will cancel the timer and return the connection to the pool.
If the timer fires before the connection is closed by the user, I will return the connection to the pool and invalidate the decorated connection so that further calls will throw IllegalStateException.
I was using the old Apache commons pool, but I switched over to the new Apache Tomcat Pool This actually has a feature to remove connections after a timeout period.
removeAbandoned - (boolean) Flag to remove abandoned connections if they exceed the removeAbandonedTimout. If set to true a connection is considered abandoned and eligible for removal if it has been in use longer than the removeAbandonedTimeout Setting this to true can recover db connections from applications that fail to close a connection. See also logAbandoned The default value is false.

Why are the connections not timing out?

Running my application on Websphere Application Server 7 with SQLServer 2008 database. When the SQLServer is at 100% every connection is hanging and filling up the connection pool. This leads to every thread also hanging. And after 10 minutes the log is filled with this:
00000042 ThreadMonitor W WSVR0605W: Thread "WebContainer : 11" (00000049) has been active for 742352 milliseconds and may be hung. There is/are 14 thread(s) in total in the server that may be hung.
The connection pool is using JTDS and has a timeout set to 300 sec.
I would pressume that after 300sec every connection whould throw an exception which would then make all the threads un-hang?
Why would the connection throw an exception after 300 seconds?. If the connection object is in use, it will continue to be alive.
Also specify the exact time out attribute that you are referring to?
Here is the definition for Connection Timeout:
This value indicates the number of seconds that a connection request
waits when there are no connections available in the free pool and no
new connections can be created. This usually occurs because the
maximum value of connections in the particular connection pool has
been reached.
For example, if Connection timeout is set to 300, and the maximum
number of connections are all in use, the pool manager waits for 300
seconds for a physical connection to become available. If a physical
connection is not available within this time, the pool manager
initiates a ConnectionWaitTimeout exception.
So, it does not make 'all the threads un-hang', it only tells you after 300 secs that there were no free connections in the pool, so it can't give you one.
The parameter governing how long an transaction may stay active is called transaction timeout, after which the transaction is marked for rollback. But even this timeout does not cancel out the thread using the connection, it only marks it as rollback-only. In order to free connection you must use either use a third party tool (ITCAM can cancel any threads in the server), or terminate/drop connections from database side.

Categories