0
I have the function below that makes a request every second to the Mysql database and updates a DIV on the page. But after about 100 requests the "THINK ME" database blocks and returns "An error occurred in the ajax request". What could be?
ajax function:
<script type="text/javascript">
function UpdateValues() {
$(function() {
$.ajax({
data: {
'option': "telaValues"
},
url: './GetValores',
type: 'POST',
success: function(data) {
queryObject = eval('(' + JSON.stringify(data) + ')');
queryObjectLen = queryObject.jsonArray.length;
var varTensao = queryObject.jsonArray[0].jsonTensao;
var varCorrente = queryObject.jsonArray[0].jsonCorrente;
//alert(per);
document.getElementById("tensaoH1").innerHTML = "Tensão: "+varTensao+" V";
document.getElementById("correnteH2").innerHTML ="Corrente: "+varCorrente+" A";
},
error: function(xhr, status, error) {
alert("Error! " + xhr.error);
alert("Ocorreu um erro na requisição ajax");
}
});
});
}
</script>
<script type="text/javascript">
$(document).ready(function(){
setInterval(UpdateValues, 1000);
});
</script>
SERVLET:
.
.
else if(op.equals("telaValues")){
try {
JSONArray jsonArray = new JSONArray();
JSONObject responseObj = new JSONObject();
ResultSet rs = null;
rs = null;
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
PrintWriter out = response.getWriter();
//JSONObject responseObj = new JSONObject();
rs = new GrandezasDAO().rs("SELECT tensao,corrente from consumo1 order by id desc limit 1");// where mes_ref BETWEEN '"+firstDate+"' AND '"+secondDate+"");
//JSONArray jsonArray = new JSONArray();
//JSONObject responseObj = new JSONObject();
while(rs.next()){
JSONObject js = new JSONObject();
js.put("jsonTensao", rs.getInt("tensao"));
//js.put("temp", rs.getInt("temperatura"));
js.put("jsonCorrente", rs.getDouble("corrente"));
jsonArray.put(js);
}
responseObj.put("jsonArray", jsonArray);
out.print(responseObj);
//out.flush();
} catch (JSONException e) {
e.printStackTrace();
}
}
.
.
ALERT RETURN ERROR:(after 100 searches in Mysql)
Error! function() {
if (u) {
var t = u.length;
(function i(t) {
b.each(t, function(t, n) {
var r = b.type(n);
"function" === r ? e.unique && p.has(n) || u.push(n) : n && n.length && "string" !== r && i(n)
})
})(arguments), n ? o = u.length : r && (s = t, c(r))
}
return this
}
RETURN OF Alert status:
200
And what appears on this alert
alert("Error! " + xhr.error);
?– Isac
I added the return of the Error, but it is not possible to understand much to which it refers...
– Jovani
And in Netbeans it returns the error: Grave: com.mysql.jdbc.exceptions.jdbc4.Mysqlnontransientconnectionexception: Data source Rejected establishment of Connection, message from server: "Too Many Connections"
– Jovani