java createstatement,如何重写某些java.sql.connection方法,例如prepareStatement createStatement或prepareCall?...

博主遇到代码库中存在大量资源泄漏的问题,特别是当连接对象的commit被调用时,想在该时刻关闭所有打开的PreparedStatement和ResultSet。由于使用的ojdbc8库升级后出现了资源泄漏问题,而连接对象是一个动态代理。博主考虑修改JDBC库源码,在每次提交时自动关闭这些资源,但担心可能引入新问题。建议是谨慎修改开源JDBC库源码,并考虑更合适的资源管理策略来修复泄漏问题。
摘要由CSDN通过智能技术生成

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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值