Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

  1. unused connection means the connection which are in sleep state according to mysql(?)
  2. What is interactive and noninteractive connections?
  3. 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.
  4. What is interactive_timeout?
like image 664
Amogh Avatar asked Jul 04 '14 00:07

Amogh


People also ask

What is c3p0 used for?

c3p0 is a Java library that provides a convenient way for managing database connections. In short, it achieves this by creating a pool of connections. It also effectively handles the cleanup of Statements and ResultSets after use.

What is c3p0 jar?

c3p0 is an easy-to-use library for augmenting traditional (DriverManager-based) JDBC drivers with JNDI-bindable DataSources, including DataSources that implement Connection and Statement Pooling, as described by the jdbc3 spec and jdbc2 std extension.

What is c3p0 in Hibernate?

C3p0 is an open-source JDBC connection pooling library, with support for caching and reuse of PreparedStatements. Hibernate provides support for Java applications to use c3p0 for connection pooling with additional configuration settings.

What is hibernate c3p0 Min_size?

min_size This is the minimum number of JDBC connections that C3P0 keeps ready at all times. hibernate. c3p0. max_size This is the maximum number of connections in the pool.


1 Answers

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.

like image 133
bitkot Avatar answered Oct 22 '22 15:10

bitkot