Hibernate MySql "Too many Connection" error solution

Source: Internet
Author: User
Tags connection pooling

Use Cxf+hibernate+spring+mysql to do WebService project, access to the database for some time, has been "Too many Connection" error, and then the database can not be accessed (connection failure, of course, can not operate)


into the MySQL command window, type the command:

Show Processlist;

There are hundreds of connections in the discovery database, and all are sleep states. By default, the maximum connection for MySQL is 100 or 150 (see your MySQL version), so many sleep connections will immediately fill up the connection.


To search the Internet for n long, are talking about using C3P0 or other connection pool, or with other temporary solutions, kill off the connection.


In fact, the analysis of the reason is nothing more than dbconnect created without closing. The problem is that, for webserver, the principle of connection pooling is that you want to be able to keep a long connection and not be closed manually.

Solution One:

Manually modifying the maximum number of MySQL connections to 500 to 1000 will not have this error.

The problem is that by observing it, the number of connections has been increasing, and soon it is 300+ and then stabilized. Of course, adding a server increases the number of connections. This hidden danger is too big, if later connect a dozens of server, this MySQL still don't hang off. Min

Solution Two:

Analysis, it is probably the reason for their own code. Look at the code, and finally find out why: Each DAO has a sessionfaction instance.

Database connection is sessionfactory to be responsible, you are debugging, each creation of a sessionfactory, will increase n connections (n is the min_size you configured in the Cfg.xml file), this can be very big, Each project has more than n DAO, so the number of connections is soon full. The best way to do this is to use one sessionfaction instance for a service, so that the number of connections is controlled between Min_size and Max_size.

The final solution is to use singleton mode to generate Sessionfactory instances, so DAO uses the same factory. Testing down, a service with only a few connections.



Hibernate MySql "Too many Connection" error solution

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.