0

I have a very large Java web application, close to a thousand classes and code. I use a database connection in almost every one of these classes. In Java JDBC I open the connection in a try block and then simply do conn.close() to close the connection.

Lately I have had many problems with "too many connection" errors. This seems to be a problem with the connection not closing properly. I learned the best way to do this is in a finally block to close the connection. However I have not done this through the life of this application and it would take me hours and days to go through every one of these classes and try blocks.

Is there any way in Eclipse to batch-fix every class and add a finally block to each try block so I can properly close the connection?

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Ethan
  • 146
  • 2
  • 14

1 Answers1

0

I would recommend to make the creation of and usage of Java JDBC connections a bit more centralized and use a new class for it that does this for you in any other context. That raises the maintainability of your code enormously. You might also be happy with a connection pool library that handles the number of open connections to your database. That prevents the "too many connections' errors enormously. Just google it, there are many tutorials on how to add a connection pool.

About your question, you can pack your code snippet (provided that it is always the same snippet), into one line and then Search/Replace it as proposed here:

Replace String in all files in Eclipse

If the code snippet is always a bit different, then you would need a good pattern matching algorithm and I am afraid that there is no such thing in eclipse. So if you have to do it by hand, then please try to refactor your application as proposed above. The next developer of the application already thanks you here and now.

Community
  • 1
  • 1
Ben
  • 2,314
  • 1
  • 19
  • 36
  • Thank you for all the info. That link helped to an extent but I have looked into HikariCP and it seems to be very well suited for my needs. I have also began to centralize things more as it has gotten out of control. Thanks for the help! – Ethan Aug 06 '15 at 18:56
  • No problem! We like to help. – Ben Aug 06 '15 at 20:08