c3p0 maxIdleTime is same as wait_timeout of mysql? - java

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.:)

Related

Releasing connections from connection pool

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.

Oracle Active Session Status on a Client's Abnormal Shutdown

Good time.
Some days ago our DB team has detected that there are sessions with an 'ACTIVE' status for clients that are not active any more. An investigation showed that there are two main sources for such issues:
remote SQL Developer connections (actually, this case is not very interesting),
abnormal tomcat (where our application was running) shutdowns (like 'kill -9')
It is strange for me that all that sessions are in the 'ACTIVE' status. Could somebody, please, clarify how could this be (maybe there is something with underlying processes that wait on the corresponding sockets or ... As far as everything is ok upon a proper tomcat shutdown, it seems like a root cause is with transactions...)?
Would it help if we set the 'IDLE_TIME' (for all connections) and 'EXPIRE_TIME' (for all our RAC instances)?
Am I right that the following scenarios should take place (with the above parameters set up):
When a client connects it's session is marked as 'ACTIVE'
Without respect to the 'ACTIVE' status, there is a 'ping' process, originated by the 'EXPIRE_TIME' parameter that pings a client.
If a ping process fails for the EXPIRE_TIME time period, even though a session is 'ACTIVE', the session is being killed by oracle.
If a client responds on pings, but does not do any processing, after an IDLE_TIME time period it's session becomes 'INACTIVE' and (if the 'IDLE_TIME' parameter is set) after some time - 'SNIPED'. After that a 'SMON' process starts a housekeeping activity for this session (and others with the 'SNIPED' status).
UPDATE:
It seems that the only way to work with such a situation is to configure Oracle instances.
There are results of my investigation:
https://community.oracle.com/thread/873226?start=0&tstart=0
For Dead Connection Detection is used server side sqlnet.ora file
parameter SQLNET.EXPIRE_TIME= <# of minutes>
The other option would be implement idle_time in profile setting. And then with some job kill SNIPED sessions (when idle_time will be
reached, session will become from INACTIVE to SNIPED).
If I open up a connection and go off to lunch, an IDLE_TIME limit will
cause my session to be terminated after 15 minutes of inactivity. An
EXPIRE_TIME of 15 minutes will merely have Oracle send a packet to my
client application to verify that the client has not failed. If the
client is up, it will answer the ping, and my session will stay around
indefinitely. EXPIRE_TIME only causes sessions to be killed if the
client application fails to respond to the ping, implying that the
client process has failed or that the client system has failed.
IDLE_TIME will kill sessions that don't have activity for a period of
time, but that generally doesn't work well with applications that
maintain a connection pool, since the assumption there is that the
connection pool will have a fair number of connections that are idle
for periods of the day and since applications that use connection
pools tend to react poorly to connections in the pool getting killed.
https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:2233109200346833212
TCP/IP doesn't interrupt things by default, by design. When a
connection goes away, the client and/or server do not immediately get
notified. So, if your client connects to the database and does nothing
and you unplug your client (blue screen it, kill it, pull out the network cable, crash the computer, whatever) the odds are the session
will stay in the database. The server will not know that it will never
receive a message from you. We have dead client detection for that if
it becomes an issue:
http://download.oracle.com/docs/cd/B12037_01/network.101/b10776/sqlnet.htm#sthref476
As for the active session, that is really easy. You open a connection,
you submit a request over this connection like "lock table T". Table t
is locked by your transaction in your session. You then submit a block
of code like:
begin loop dbms_lock.sleep(5); end loop; end; /
your session will be active for as long as that code is running - the
client process is blocked on a socket read waiting for the server to
send back a result - a response (which of course will never come). The
server isn't touching the network at all right now - it is active with
your code. So, if your client 'dies' right now - the block of code
will continue to run, and run, and run - and since it never commits -
it'll just hang in there and run and of course any locks you have will
remain in place.
http://www.databaseskill.com/4267817/
http://www.dba-oracle.com/t_connect_time_idle_expire_timeout.htm
The sqlnet.expire_time parameter is used to set a time interval, in
minutes, to determine how often a probe should be sent verifying that
client/server connections are active. If you need to ensure that
connections are not left open indefinitely (or up to the time set by
operating system-specific parameters), you should set a value that is
greater than 0. This protects the system from connections left open
due to an abnormal client termination.
https://asktom.oracle.com/pls/apex/f?p=100:11:0::NO::P11_QUESTION_ID:453256655431
If the session is waiting for a resource locks or latches, and if this
wait time exceeds idle_time setting in th profile, does the session
gets sniped, even if the session is in the middle of a transaction and
waiting for lock etc.
If so, will there be any entries in the alert log.
Followup
if waiting for a lock, you are active -- not idle.
These page get my attention, six month before I had an Oracle Support
for a Data Guard issue, so one of the Oracle guys, notice that I use
the Idle_Time and he told me that this parameter dont work very well
because Oracle dont release the resource of sessions that were marked
as snipped, until the next time the user try to use it (waiting to
tell your session was killed, to clear the session resources)
Followup
... after an investigation... the "session" is there, that
will not go away until the client acknowledges it, but the
"transaction" is gone.
Tom, I've altered a profile to have IDLE_TIME=240(4 hours) and made
sure my resource_limit parameter is set to TRUE. When I query
v$session I see some "snipped" sessions, but also "inactive" ones that
have been idle for more than a day. All those users have this profile
assigned to them. If the user session was connected before idle_time
was set, would those sessions be affected by this change or not? I've
made a change quite some time ago. Is there anything else I should
have done?
Followup
If the user session was connected before idle_time was set,
they are "grandfathered" in -- they will not be sniped. it only
affects new sessions.
http://agstamy.blogspot.ru/2011/03/profile-and-resource-limit.html
additional stuff and recommendations: https://rebby.com/blog.php?detail=32
We have checked the parameters, listed in the above investigation and everything worked correctly!

handling/force timeout stale connections in connection pool in case of DB connectivity issues

I am facing RDS connectivity issues sometimes. So when this happen, all the connections in my connection pool become stale. With stale connections in the pool i am facing some high latency issues.
With testConnectionOnCheckout = true, How can set a timeout for getting a connection from c3p0's connection pool? Because when my application tries to obtain the connection from the pool, all the connections in the pool are first checked if they are stale and after that when pool is exhausted, then i get an exception after checkoutTimeout interval. SO if my checkout interval is 1000 ms and it takes around 100 ms to check stale connection and i have 30 connections in pool, my request would be stuck for 100*30+1000=4000 ms. Is there a way I can put a timeout to obtain a connection (doesn't matter if pool is exhausted or not).
With periodic connection checking, I face a weird issue in latency. I have set JDBC read timeout to 2000 ms. When I make calls to my API, some calls do throw an exception within 2000 ms but a few calls take around 15 sec or more which keeps my thread busy and make my service unavailable for those APIs which don't depend on database. The behavior about this high latency is every 6th call to the API takes longer time. I am not sure how could that happen.

What happens when you do not close an HBase table?

I am considering creating a HBase table when my application starts up and leaving it open as long as my application is running. My application may run indefinitely.
What happens if I never close the HBase table?
Is there a maximum time the connection can be open/idle before it need to be reinitialized?
How is the connection closed if the system crashed?
I have HBase The Definitive Guide but I have not found the information I am looking for in there. If there are any online references for this then please provide them.
This was extracted from "HBase in Action" page 25:
"Closing the table when you’re finished with it allows the underlying
connection resources to be returned to the pool."
This blog post is about timeouts in HBase. Generally speaking, there is a lot of them:
ZK session timeout (zookeeper.session.timeout)
RPC timeout (hbase.rpc.timeout)
RecoverableZookeeper retry count and retry wait (zookeeper.recovery.retry, zookeeper.recovery.retry.intervalmill)
Client retry count and wait (hbase.client.retries.number, hbase.client.pause)
You may try to raise them a bit and set a really high value for retry count. This can make your sessions be alive for a very long period of time.
When the system of HBase client crashed, the connection is closed by timeout.

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