I have configured c3p0 data source in my application using following code.
<bean id="globalDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="${DbUrl}/${DbName}?useOldAliasMetadataBehavior=true&useSSL=false" />
<property name="user" value="${DbUserId}" />
<property name="password" value="${Password}" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="100" />
<property name="idleConnectionTestPeriod" value="14400" />
<property name="preferredTestQuery" value="select 1" />
</bean>
i have five different datasources with same configuration. On mysql side i have configured max_connections as 300. After 10 days with this live code mysql process list looks as follows with all sleeping connections:
Database1: 70 connections
Database2: 70 connections
Database3: 70 connections
Database4: 50 connections
Database5: 40 connections
these all connections are in sleep state and mysql have reached to max limit of around 290 to 300 connections. Is there any way to release connections so that number of connections can come to at least min pool size?
for further info about it my application is very old and connections are not closed in many of hibernate transactions because its very hard to find impact points if i use following line
<property name="hibernate.connection.release_mode">after_transaction</property>
so i can not use above line in my hibernate-cfg.xml and for that reason i am also keeping alive my connections by "idleConnectionTestPeriod" to avoid timeout issue.
Please suggest best possible solution. i am thinking to use "maxIdleTimeExcessConnections" property of c3p0.
The first thing you should do is check for Connection leaks (which seem very likely to be your issue). See e.g. C3P0 Spring Hibernate: Pool maxed out. How to debug?
The best thing to do is to eliminate the Connection leaks. First you should set unreturnedConnectionTimeout and debugUnreturnedConnectionStackTraces. See also Configuring To Debug and Workaround Broken Client Applications.
If this is an old and crufty thing and you can't or don't want to mess with it, you can workaround the issue by setting an unreturnedConnectionTimeout and turning off debugUnreturnedConnectionStackTraces. Collecting stack traces on every Connection checkout is a drag on performance. But first, to understand the issue, you should set both. If you decide you cannot or don't want to actually fix the problem, but wish to work around it, then unset or set to false debugUnreturnedConnectionStackTraces.
Related
I have a Java Spring application connecting to a SQL Server database.
The connection settings are:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:jtds:sqlserver://${db.host}:1433/TestDB" />
<property name="user" value="${db.user}" />
<property name="password" value="${db.pass}" />
<!-- these are connection pool properties for C3P0 -->
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="100" />
<property name="acquireIncrement" value="5"/>
<property name="maxIdleTime" value="30000" />
</bean>
everything works fine but sometimes I got the following error:
Could not open JDBC Connection for the transaction; nested exception is java.sql.SQLException: I/O Error: Read timed out
I have searched a lot but can't find any clue, any idea or help?
I'm using
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
in my spring-config xml to get my sqlSession, and in the DAO I use:
#Autowired
SqlSession sqlSession;
and then I execute the queries I want. Is it possible that because the connection is not closed that this error exists?
In my case connections were getting dropped when the nightly db backup job triggered. I'm using jtds/sql-server as well. Here is what I did to fix it:
Create/setup a health-check cron job that executes a simple query from within your application, like a short select from. Call it every 10 minutes or so and log the result. It will give you some feedback about when and why this is happening.
Reduce the idle time parameter (maxIdleTime) in your configuration so that old connections get automatically discarded.
Keep in mind that if you don't change the maxIdleTime and you keep multiple connections open, some of them may remain in a bad state even if you are using the health-check function. Quoting from c3p0 documentation:
By default, pools will never expire Connections. If you wish Connections to be expired over time in order to maintain "freshness", set maxIdleTime and/or maxConnectionAge. maxIdleTime defines how many seconds a Connection should be permitted to go unused before being culled from the pool. maxConnectionAge forces the pool to cull any Connections that were acquired from the database more than the set number of seconds in the past.
Another way to setup a health-check call is by using the idleConnectionTestPeriod parameter. Also check this answer which can give you more ideas on how to set it up.
I am using EclipseLink JPA version 2.6 and I wanted to use connection pool properties to set minimum and maximum connection in persistence.xml file. But I have two different set of properties as follows
<property name="eclipselink.connection-pool.default.initial" value="1" />
<property name="eclipselink.connection-pool.default.min" value="64" />
<property name="eclipselink.connection-pool.default.max" value="64" />
And
<property name="eclipselink.connection-pool.default.initial" value="1" />
<property name="eclipselink.connection-pool.node2.min" value="16"/>
<property name="eclipselink.connection-pool.node2.max" value="16"/>
I have doubt that which set I have to use in persistence.xml file. Anyone suggest me a best possible way for handling multiple connections.
Check the documentation about partitioning your connection pool. If you not plan to partition your pool, the first settings-block is fine. Check the Connection Pool settings for eclipselink.
I have a basic c3p0 configuration as below. What I am asking is when there is no traffic on the application (connections stay idle), could c3p0 shrink the pool when maxIdleTime is reached for each connection since min and max connection numbers are the same?
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="jdbcUrl" value="#[jdbc.url]"/>
<property name="user" value="#[jdbc.username]"/>
<property name="password" ref="DbPassword"/>
<property name="minPoolSize" value=25/>
<property name="maxPoolSize" value=25/>
<property name="acquireIncrement" value= 1/>
<property name="idleConnectionTestPeriod" value="100"/>
<property name="maxIdleTime" value="120"/>
<property name="preferredTestQuery" value ="select 1 from dual"/>
maxIdleTimeExcessConnections is about minimizing the number of Connections held by c3p0 pools when the pool is not under load. By default, c3p0 pools grow under load, but only shrink if Connections fail a Connection test or are expired away via the parameters described above. Some users want their pools to quickly release unnecessary Connections after a spike in usage that forces a large pool size. You can achieve this by setting maxIdleTimeExcessConnections to a value much shorter than maxIdleTime, forcing Connections beyond your set minimum size to be released if they sit idle for more than a short period of time.
I have a mysql running on 3306 port in my local system. and also there is another mysql is running on 13000. while im trying to connect to 13000 instance still it is connecting to 3306 instance. c3p0 datasource is taking the default port and it is simply ignoring the port. so could some one help on this???
Spring bean:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:13000/dbname?autoReconnect=true" />
<property name="user" value="user" />
<property name="password" value="pwd" />
<property name="idleConnectionTestPeriod" value="55" />
<property name="initialPoolSize" value="10" />
<property name="maxIdleTime" value="60" />
<property name="maxPoolSize" value="15" />
<property name="minPoolSize" value="5" />
</bean>
Edit:
Even simple JDBC program is connecting to 3306.
c3p0 doesn't parse JDBC urls or make any assumptions at all about what's in them. It just asks your JDBC driver (via DriverManager, or via a directly instantiated driver if you configure forceUseNamedDriverClass) to return Connections for the supplied URL. The issue is definitely not that c3p0 is ignoring the nondefault port. c3p0 is ignoring it all. It has no idea and doesn't care.
I'd try to verify that the JDBC URL you think you are configuring is the one that is actually getting through to the pools you are making. If you have multiple MySQL instances on the machine, maybe you have conflicting config somewhere. c3p0 dumps its configuration to your application's logs at INFO level on pool initiation. Look for that, and verify that the URL your c3p0 DataSource is using is the URL you intended to configure.
Otherwise, try to check to see whether a call to DriverManager.getConnection( myJdbcUrl ) gets you to the DBMS you want. That's basically what is calling. (It's actually calling DriverManager.getConnection( myJdbcUrl, info ) where info is a Properties object. Unless you've set forceUseNamedDriverClass to true.)
Finally found the issue. The reason is i am using mysql-connector-java.5.1.9.jar, so there is a bug in that connector. So i changed it to latest version then my issue got resolved.
Thanks.
I use spring jdbc template for app.. and deploy it in tomcat.. I want to use connection pool with tomcat jdbc. My connection configuration is
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3310/mydb" />
<property name="username" value="***" />
<property name="password" value="***" />
<property name="maxWait" value="10000" />
<property name="removeAbandonedTimeout" value="60" />
<property name="removeAbandoned" value="true" />
<property name="logAbandoned" value="false" />
<property name="initialSize" value="10" />
<property name="maxActive" value="100" />
<property name="minIdle" value="10" />
</bean>
I don't know how, but when I run some test, and check the max thread in mysql, it show that the active thread is more than the maxActive configured in the configuration. So, why the maxActive in the configuration not working? And how to make it work? For example, the maxActive is 100 but when I check in mysql, the active thread is more than the maxActive.
maxActive (int) The maximum number of active connections that can be
allocated from this pool at the same time. The default value is 100
maxIdle (int) The maximum number of connections that should be kept in
the pool at all times. Default value is maxActive:100 Idle connections
are checked periodically (if enabled) and connections that been idle
for longer than minEvictableIdleTimeMillis will be released. (also see
testWhileIdle)
So i should recommend you to use maxIdle too, example:
<property name="maxIdle" value="100">
But maybe there is a problem, if you could show the code of your connection management, it would be helpful.
Here is an interesting link of a connection problem with declarative and programmatic transaction management with Spring: Connection pool problem with Spring and programmatic transaction management
I got the same problem with you and my tomcat version is tomcat 9.
Here is my solution: you should set maxTotal value 100 instead of maxActive.
maxTotal: Maximum number of database connections in pool. Make sure you configure your mysqld max_connections large enough to handle all of your db connections. Set to -1 for no limit.
FROM http://tomcat.apache.org/tomcat-9.0-doc/jndi-datasource-examples-howto.html.