java - Error: Data source rejected establishment of connection, message from server: "Too many connections" -
i created application writes data database every 5 minutes.
however after time error appears:
error: data source rejected establishment of connection, message server: "too many connections"
i've been searching around , tells close connection database after every request side.
i tried this:
conexao.close();
but gives me error:
no operations allowed after conection closed.
i apologize if question not formulated.
thanks help
---------------------what tried didn't work---------------------------
add
finally{ if(conexao!=null) conexao.close(); }
class.forname("com.mysql.jdbc.driver"); connection conexao = (connection) drivermanager.getconnection("jdbc:mysql://localhost/bdteste", "root", "root"); statement stm = conexao.createstatement(); bufferedreader reader = new bufferedreader(new filereader("c:/users/rpr1brg/desktop/test.txt")); string dados[] = new string[6]; string linha = reader.readline(); while (linha != null) { stringtokenizer st = new stringtokenizer(linha, ";\""); dados[0] = st.nexttoken(); dados[1] = st.nexttoken(); dados[2] = st.nexttoken(); dados[3] = st.nexttoken(); dados[4] = st.nexttoken(); dados[5] = st.nexttoken(); dateformat dateformat = new simpledateformat("d-m-yy"); preparedstatement stmt = (preparedstatement) conexao.preparestatement("replace registos" + " (data_registo, hora_registo, idsensor, temperatura, humidade, pt_orvalho) values (?,?,?,?,?,?)"); try { stmt.setdate(1, new java.sql.date(dateformat.parse(dados[0]).gettime())); stmt.setstring(2, dados[1]); stmt.setstring(3, dados[2]); stmt.setstring(4, dados[3]); stmt.setstring(5, dados[4]); stmt.setstring(6, dados[5]); } catch (java.text.parseexception ex) { exceptions.printstacktrace(ex); } stmt.executeupdate(); linha = reader.readline(); printwriter writer = new printwriter("c:/users/rpr1brg/desktop/test.txt"); writer.print(""); writer.close(); verifica(); } } catch (classnotfoundexception | sqlexception | ioexception e) { system.err.println("erro: " + e.getmessage()); }finally{ if(conexao!=null) conexao.close(); }
this kind of problem arises when not closing connection after usage.
please use
finally
block aftercatch
close connections appropriately. because ensure connection gets closed when there unexpected exception or error. please note statements insidefinally
block gets executed always. allows programmer avoid having cleanup code accidentally bypassed return, continue, or break
note: if jvm exits while try or catch code being executed, block may not execute. likewise, if thread executing try or catch code interrupted or killed, block may not execute though application whole continues.
as have asked in comment, have added code sample demonstrate practically!
connection con = null try{ //establishing connection datasource con = dbconnection.getconnection(); //perform db operations ... ... ... }catch(sqlexception sqlex){ /*to catch sqlexception thrown during db *operations , continue processing sending alert admin *that exception occurred. */ }finally{ /*this block should added code * need release resources connections */ if(con!=null) con.close(); }
please note declaration of connection
variable should in proper scope close in finally
block.
hope helps!
Comments
Post a Comment