i have a application where i have to add,delete,edit. i have used hibernates and created seperate methods for each operation. after adding some 80-90 values it is showing following exception.
Data source rejected establishment of connection, message from server: "Too many connections" at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:649) at com.mysql.jdbc.Connection.createNewIO(Connection.java:1786) at com.mysql.jdbc.Connection.<init>(Connection.java:450) at com.mysql.jdbc.NonRegisteringDriver.connect(at java.lang.Thread.run(Unknown Source) 16:53:12,687 INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:mysql://localhost/test 16:53:12,687 WARN JDBCExceptionReporter:57 - SQL Error: 1040, SQLState: 08004 16:53:12,687 ERROR JDBCExceptionReporter:58 - Data source rejected establishment of connection, message from server: "Too many connections"
i think too many connection are created. so i wanted to know how to use a single connection. Should i use singleton pattern while creating SessionFactory object ? and also i need to know when exactly connection gets created. ie 1)while creating SessionFactory object 2)or while creating Session object
so i wanted to know how to use a single connection
I don't think that is the right solution. RDBMS's are designed to handle multiple concurrent connections, so why undermine that functionality by turing it into a one user system?
Before doing anything else I'd check you have configured MySQL to accept the more than the amount of connections your application is likely to use. I'm assuming you are using a connection pool? Check how many connections this can create. I don't know MySQL very well, but if it is like most other RDBMSs you will be able to increase the number of connections that it will allow.
and also i need to know when exactly connection gets created. ie 1)while creating SessionFactory object 2)or while creating Session object
One last thing - do you always close Sessions once you are finished with them?
The message looks like it is comming from MySQL, not Hibenrate, so my assumption would be that 10 connections are more than you are allowed. Like I say, I don't know MySQL very well but I think you use the SHOW PROCESSLIST command to check how many connections actually are being made to your server.