Today's Question:  What are you most afraid of as a programmer?        GIVE A SHOUT

Technical Article => Database =>  MySQL

Solve Hibernate "Too many connections" issue in MySQL

  sonic0002      2013-09-04 22:20:49      13,690    2    1

When working with Hibernate and MySQL, sometimes some exceptions will be thrown after sometime. The exception may seem like :

java.sql.SQLException: Data source rejected establishment of connection, message from server: "Too many connections"

This means there are too many active connections on the MySQL, you can use

SHOW STATUS LIKE '%Threads_connected%';

to check the active connections to MySQL. If you want to change the maximum connections allowed to MySQL. You can execute:

set global max_connections = [num];

You may forget to manually call session.close() methods iin your codes. If you have done this in all methods. Then you should not forget to call sessionFactory.close() when you have done all the requests. Most of the time, it's because you forget to call sessionFactory.close(). So please don't forget to call sessionFactory.close().

Also Hibernate's own connection pooling algorithm is, however, quite rudimentary. It is intended to help you get started and is not intended for use in a production system, or even for performance testing. You should use a third party pool for best performance and stability. Just replace the hibernate.connection.pool_size property with connection pool specific settings. This will turn off Hibernate's internal pool. For example, you might like to use c3p0.

MYSQL HIBERNATE CLOS

  SAVE AS PDF   MARK AS READ   MARK AS IMPORTANT

Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious

  RELATED


  2 COMMENTS


集成显卡 [Reply]@ 2013-09-05 21:04:56

这个问题之前也试过。

还有一个问题就是,使用hibernate自带的连接池,碰到了“8小时”问题:在连接空闲8小时后,再想通过hibernate访问数据库时,会出错。

后来用了 c3p0 解决此问题

qwd [Reply]@ 2014-09-24 00:20:10

wwsd


  WRITE ARTICLE

Successful or unsuccessful?

By sonic0002