I want to override some of the methods in connection interface to close preparedStatements and resultsSets when commit on the connection object gets called. There is a lot of resource-leak in my codebase and as a failsafe I wanted to implement this solution, where at every commit, I could look for all open statements and resultsets and close them.
In this case however, I don't have a class to override and call super() on all other methods. The object returned as a connection is a dynamic proxy - com.sun.proxy.$Proxy. Not sure of how to have my own methods called for this object. Any lead is highly appreciated.
PS: I am using an ojdbc8 jar in the project, which was recently upgraded from ojdbc7. To the best of my knowledge, we never came across any resource leakage issues (like maximum open cursors exceeded) in the previous version.
解决方案
Is your JDBC library open source? Then download the sources of the JDBC version, make this one code change, generate JAR again and use that instead. When you do this, the problem is that all 'close' calls in your code no longer are legit close class, but redundant calls, in such a case I don't know if the JDBC is going to throw one more exception (something like 'connection is already closed') or not, but if yes, you need to suppress the exception or something.
But the basic idea here is that you are modifying the sources and that's kind of risky.
Now, even if you find a common fix, the next problem is about the impact, where nobody including you can say the impact of this, and someone else should approve this change (assuming this is large code and is some project).
So, instead of doing things like this, and inviting new issues, it is better to accept the fact that someone in the past did some lousy job and we fix it now the right way.
I know that is exactly what you are trying to avoid here, then I think you should try modifying the JDBC sources and give it a shot. All the best!