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.
这个问题之å‰ä¹Ÿè¯•è¿‡ã€‚
还有一个问题就是,使用hibernateè‡ªå¸¦çš„è¿žæŽ¥æ± ï¼Œç¢°åˆ°äº†“8å°æ—¶”问题:在连接空闲8å°æ—¶åŽï¼Œå†æƒ³é€šè¿‡hibernate访问数æ®åº“时,会出错。
åŽæ¥ç”¨äº† c3p0 解决æ¤é—®é¢˜