1

I'm currently writing a program in java where I often get data from the MySQL database, with a JDBC driver. At first I tried to close the connection every time I ran the program to test it, but it gave an error at that line. I left the 'connection.close()' line out and it worked, which at the time was good enough for me.

Unfortunatly that backfired, because now I can't run the program anymore. It gives the following error.

Data source rejected estabilishment of connection, message from server: "Too many connections" 

I honestly have no idea how to fix this. Another related app in Visual Studio rebuild the database every time I run that, so I don't think that's a problem. I deleted the databse in my java editor, Netbeans, and re-imported it, but that didn't solve it either. My googling skills have failed me as well, even the question on stackoverflow didn't seem to work.

Any help would be much appreciated

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • 5
    *Close your connections*. – user2864740 Apr 15 '14 at 21:28
  • On an different note wrt how to "reset" the error: make sure there are no background processes running with open connections (connections will be closed *when* the programs are terminated, but background programs are still running) and, if all else fails, forcibly have MySQL eject it's connections (I believe this means ["restart"](http://stackoverflow.com/questions/4932503/how-to-kill-mysql-connections) in MySQL terms). – user2864740 Apr 15 '14 at 21:30
  • What is the error that `connection.close()` is throwing? – Matt K Apr 15 '14 at 21:32
  • as a practice, you should close the connection if it does not requires to be open. btw check your mysql configuration to see how many concurrent connections do you have. – maxx777 Apr 15 '14 at 21:33
  • Yeah, if I knew how to close the connections, we wouldn't be here huh? I tried to use the SHOW PROCESSLIST command in MySQL, but I only saw 1 connection in there, mine. Also I think I'm desperate enough to try the last option, go to 'Services' of my computer and restart the MySQL service? – I_Am_Not_user3185054 Apr 15 '14 at 21:34
  • @I_Am_Not_user3185054 If the error happens within the program "at some point" when it is running, then it might simply too many unclosed connections *in* that program without any external help. Use a debugger and catch the exception. Then, *before* allowing the Java program to terminate (thanks Mr. Debugger!), view the connections in MySQL. – user2864740 Apr 15 '14 at 21:36
  • mmm that does seem likely indeed, though my debugger seems to be acting weirdly atm :/ trying to find out why it's not doing it's damn job – I_Am_Not_user3185054 Apr 15 '14 at 21:47

2 Answers2

2

You can use connection pooling.
A cache of database connections maintained in the database's memory so that the connections can be reused when the database receives future requests for data.
Connection pools are used to enhance the performance of executing commands on a database.
Refer this : http://en.wikipedia.org/wiki/Connection_pool

Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
1

Since the connections were never closed by your program, they are staying open and using up the connection limit for the MySQL server. Even resetting the database won't necessarily eject or end those connections to the MySQL service, because the connections are attached to the server, not the database file. I would just restart the MySQL server, which will eject and close all current connections, as user2864740 suggested.

In order to fix your problem with the program throwing an error every time you try to close a connection, like Matt K said, you are going to need to post the error you are getting. You certainly need to close the connections in your program.

  • mmm that doesn't seem to do the trick tbh. I restarted the MySQL services, but I still get the error. I'm going to try and debug the program, but it for some reason my java editor seems to be acting weird with that :/ – I_Am_Not_user3185054 Apr 15 '14 at 22:07