java - Problem with not closing db connection while debugging? -


I have a Java app that initially opens a connection to a database, and closes it at the end though, The program does not always end, because the exception is thrown or I'm debugging it and it closes in halfway.

Is it open connection database to slow down and slow down, or is it automatically cleaned?

A database connection is managed by the ownership and database, the class only gives you access to that database If you do not close the connection for processing, then Java class garbage can be collected, but the database can not tell that the connection is no longer in use, resulting in waste of database resources (time-consuming on the database side)

Therefore, when you are using your connection , you should make sure Close your code () The method will allow this garbage collector to remind the memory as soon as possible, and more important , it can handle any other database resources (cursor, handles , Etc.), which may be on connection

In Java, this is the way to do this The traditional way to close your ResultSet , statement , and connection (in that order)) a finally blocks When you are done with them and a secure pattern looks like this:

  connection conn = null; Prepared place PS = null; Results set rs = null; {// Do stuff ...} hold (SQLException pre) {// Exception handling stuff ...} Finally {if (rs! = Null) {try {rs.close (); } Hold (SQLException e) {/ * ignored *}} if (ps! = Null) {try {ps.close (); } Hold (SQLException e) {/ * ignored *}} if (conn! = Null) {try {conn.close (); Catch (SQLException e) {/ * ignored *}}}  

ultimately can be slightly improved in the block (to avoid empty probe): Finally {try {rs.close ()}} hold (exception e) {/ * ignored * /} try {ps.close (); } Hold (exception e) {/ * ignored * /} try {conn.close (); } Hold (Exception E) {/ * ignored *}}

But, nevertheless, it is highly executed, so you usually have to open an object in an empty square Helper classes are helpful methods and finally blocks something like this:

 } Finally {DbUtil.closeQuietly (RS); DbUtil.closeQuietly (PS); DbUtil.closeQuietly (Conn); }  

And, actually, there is a class that is doing this properly so that you do not have to write your own.

In your case, this is an exception problem, but not debugging (and you will ruin the database resources until the timeout database is on the side). Therefore 1. Do not debug your code using the production database 2. Try to execute your debug session until the end.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -