Bonjour,
dans une application web, après un certain nombre de requete, j'ai cette exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
Je sais que cela vient du fait que je ne ferme pas le Statement ou le ResultSet mais quand je les ferme, j'ai l'erreur suivante:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed.
Je pense que c'est un problème dans mon code, donc je vous donne quelques sources:
AgentDaoImpl.java:
j'instancie cette classe à chaque fois que je lance ma servlet ce qui créer une nouvelle instance de Bdd donc je ne comprends pas pourquoi ne pas la fermer pose problème et pourquoi la fermer en pose aussi.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 public class AgentDaoImpl implements AgentDaoInter { private ArrayList agents = new ArrayList(); private Bdd bdd; private Statement stm; private ResultSet rep; public AgentDaoImpl() { bdd = new Bdd(); stm = bdd.getStm(); rep = null; } @Override public Collection getAll() { try { rep = stm.executeQuery("SELECT ..."); agents.clear(); while (rep.next()) { agents.add(new Agent(rep.getInt("id_ag"), rep.getString("nom_ag"), rep.getString("prenom_ag"), rep.getString("mail_ag"))); } return agents; } catch (SQLException ex) { Logger.getLogger(AgentDaoImpl.class.getName()).log(Level.SEVERE, null, ex); } finally { bdd.close(); } return null; } @Override public Collection getWithCommune(int id) { try { rep = stm.executeQuery("SELECT ..."); agents.clear(); while (rep.next()) { agents.add(new Agent(rep.getInt("id_ag"), rep.getString("nom_ag"), rep.getString("prenom_ag"), rep.getString("mail_ag"))); } return agents; } catch (SQLException ex) { Logger.getLogger(AgentDaoImpl.class.getName()).log(Level.SEVERE, null, ex); } finally { bdd.close(); } return null; } @Override public Collection getWithSession(int id) { try { rep = stm.executeQuery("SELECT ..."); agents.clear(); while (rep.next()) { agents.add(new Agent(rep.getInt("id_ag"), rep.getString("nom_ag"), rep.getString("prenom_ag"), rep.getString("mail_ag"))); } return agents; } catch (SQLException ex) { Logger.getLogger(AgentDaoImpl.class.getName()).log(Level.SEVERE, null, ex); } finally { bdd.close(); } return null; } @Override public Agent getOne(int id) { try { rep = stm.executeQuery("SELECT ..."); agents.clear(); while (rep.next()) { agents.add(new Agent(rep.getInt("id_ag"), rep.getString("nom_ag"), rep.getString("prenom_ag"), rep.getString("mail_ag"))); } return (Agent) agents.get(0); } catch (SQLException ex) { Logger.getLogger(AgentDaoImpl.class.getName()).log(Level.SEVERE, null, ex); } finally { bdd.close(); } return null; } @Override public void saveOne(Agent agent) { // ajout ou modification ? if (agent.getId() == -1) { // ajout insertAgent(agent); } else { //modification updateAgent(agent); } } // supprimer un agent @Override public void deleteOne(int id) { try { stm.executeUpdate("DELETE ..."); stm.executeUpdate("DELETE ..."); } catch (SQLException ex) { Logger.getLogger(AgentDaoImpl.class.getName()).log(Level.SEVERE, null, ex); } finally { bdd.close(); } } // ajouter un agent private void insertAgent(Agent agent) { try { stm.executeUpdate("INSERT ..."); } catch (SQLException ex) { Logger.getLogger(AgentDaoImpl.class.getName()).log(Level.SEVERE, null, ex); } finally { bdd.close(); } } // modifier un agent private void updateAgent(Agent agent) { try { stm.executeUpdate("UPDATE ..."); } catch (SQLException ex) { Logger.getLogger(AgentDaoImpl.class.getName()).log(Level.SEVERE, null, ex); } finally { bdd.close(); } } }
GestionAgentServlet.java:
Voila le code de la classe Bdd.java:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class GestionRespVilleServlet extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ // services AgentServiceImpl agentService = null; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { // on récupère la méthode d'envoi de la requête String méthode = request.getMethod().toLowerCase(); // on récupère l'action à exécuter String action = request.getPathInfo(); // on initialise les services AgentDaoImpl agentDao = new AgentDaoImpl(); agentService = new AgentServiceImpl(); agentService.setDao(agentDao); // affichage liste des agents ArrayList agents = (ArrayList) agentService.getAll(); request.setAttribute("agents", agents); // affichage de la vue [list] getServletContext().getRequestDispatcher("/agent/list.jsp").forward(request, response); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class Bdd { private static final String pilote = "com.mysql.jdbc.Driver"; private Connection connexion; private Statement stm; public Bdd() { try { Class.forName(pilote); } catch (ClassNotFoundException ex) {Logger.getLogger(Bdd.class.getName()).log(Level.SEVERE, null, ex);} try { connexion = DriverManager.getConnection("jdbc:mysql://localhost/MyApp", "MyLogin", "MyPassword"); this.stm = connexion.createStatement(); } catch (SQLException ex) {Logger.getLogger(Bdd.class.getName()).log(Level.SEVERE, null, ex);} } public Statement getStm() { return stm; } public void close(){ try { connexion.close(); } catch (SQLException ex) { Logger.getLogger(Bdd.class.getName()).log(Level.SEVERE, null, ex); } } }
Merci de votre aide.
Partager