Solve Hibernate "Too many connections" issue in MySQL
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.
A good way to know that your network speed is slow is when you find that you YouTube video needs to be buffered.