简体   繁体   中英

Too many connections using c3p0 and hibernate in MySql

I'm using c3p0 for my connection pooling. I've configured min connections as 100 and max size as 2000. I'm just writing a simple insert program to check how many connections are active in workbench. But, I'm getting the following error

java.sql.SQLException: Data source rejected establishment of connection,  message from server: "Too many connections"
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:650)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1808)
at com.mysql.jdbc.Connection.<init>(Connection.java:452)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)

My Hibernate.cfg.xml is as follows

<!-- c3p0 Connection pool config -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">100</property>
<property name="hibernate.c3p0.max_size">2000</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>  
<property name="hibernate.c3p0.validate">true</property>

My Java program is

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Transaction tx = null;
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.openSession();
    tx = session.beginTransaction();
    System.setProperty("net.sf.ehcache.skipUpdateCheck", "true");
    Employee e = new Employee(2,"Richard");
    session.save(e);

    try {
        Thread.sleep(20000);

    } catch (Exception e2) {
        e2.printStackTrace();
    }
    tx.commit();
    session.close();
    System.out.println("Great! Student was saved");

}

It works fine when the min size is 5 and max size is 20. Do I need to do any changes in MySQL workbench?

The whole purpose of a database connection pool (like c3p0 ) is to optimise the use of resources versus the database.

If you had 6000 users with 6000 connections, you would quickly exhaust the available connections and errors would result.

Instead, a connection pool allows your application to "borrow" database connections from the pool, and return them after use.

So even though you have potentially 6000 users, the moments of time when multiple users are doing operations that operate versus the database concurrently at that moment in time would be a small fraction of that.

I would suggest to try this as a more reasonable value:

<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">100</property>

After setting it up like this, I would run the application and watch the number of connections versus the database. If the 100 connections are at any time exhausted, you could look at tuning it upwards.

But I suspect that 100 concurrent connections will be enough - remember that they are "borrowed" from the c3p0 connection pool.

Documentation: What is c3p0? (connection pooling)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM