GeeCON Prague 2014*
The moose likes Object Relational Mapping and the fly likes Too many Connections Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Too many Connections Exception" Watch "Too many Connections Exception" New topic
Author

Too many Connections Exception

sure nandu
Greenhorn

Joined: Oct 16, 2006
Posts: 18
hai friends,

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
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


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

Both.

One last thing - do you always close Sessions once you are finished with them?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
sure nandu
Greenhorn

Joined: Oct 16, 2006
Posts: 18
thanks Paul Sturrock for your detailed reply.

in my hibernate-cfg.xml i have mentioned connection pool as

<property name="hibernate.connection.pool_size">10</property>
so please tell me whether i have to increase this value.



and secondly i have closed all the sessions in each of add, delete,edit methods. i also need to know doing like this is wrong.


regards
nandu
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
Are you sure you handing the Connections back to the pool ? (by closing the Hibernate Session ?)
Do you close it in a finally clause ?

I assume your still struggling with the same things you posted earlier ?

So please show us a piece of code where you deal with hibernate.

Pascal
 
GeeCON Prague 2014
 
subject: Too many Connections Exception