如何避免在 Java 中意外关闭 SQL 连接?
目前我正在为事务管理做的是:
Connection connection = getConnection();
connection.setAutoCommit(false);
updateTableX ( connection, ... );
updateTableY ( connection, ... );
connection.commit();
closeConnection();
我想知道是否可以避免关闭我的“updateTableX”方法中的连接。 因为如果有人意外关闭连接,那么我的 updateTableY 将不会有连接,并且会引发异常。
Currently what i am doing for transaction management is:
Connection connection = getConnection();
connection.setAutoCommit(false);
updateTableX ( connection, ... );
updateTableY ( connection, ... );
connection.commit();
closeConnection();
I would like to know, if it is possible to avoid closing the connection in my 'updateTableX' method. Because if someone accidentally closes the connection then my updateTableY will not be having the connection and it will throw the exception.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只是纪律。 一般来说,方法不应该尝试负责关闭作为参数传递给它们的东西 - 除了创建一个新对象来包装现有对象的情况。
避免在
updateTableX
中关闭连接的方法只是确保您不会在代码中调用close()
。 这实际上与任何其他错误没有什么不同。 如何阻止 updateTableX 任意更新不同的表、抛出异常或执行任何其他不该做的事情? 代码审查、单元测试、集成测试、手动测试等...我的意思是您可以编写一个
Connection
实现,它包装另一个连接并通过代理所有方法exceptclose()
但这听起来像是浪费时间 - 如果您不相信相关开发人员不会关闭连接,您是否相信他们能够获取其余代码正确的?Just discipline. In general, methods shouldn't try to take responsibility for closing things passed into them as parameters - with the exception of situations where you create a new object to wrap an existing one.
The way to avoid closing the connection in
updateTableX
is just to make sure you don't put a call toclose()
into the code. This is no different than any other bug really. How do you stopupdateTableX
from arbitrarily updating a different table, or throwing an exception, or doing anything else it's not meant to? Code reviews, unit tests, integration tests, manual testing etc...I mean you could write a
Connection
implementation which wraps another connection and proxies all the methods through exceptclose()
but it sounds like a waste of time - if you don't trust the developers involved not to close the connection, do you trust them to get the rest of the code right?就像乔恩所说,如果您确实想禁止调用
close()
,您可以编写一个装饰器实现来转发到您的“真实”Connection
对象。 我没有发布代码示例,因为Connection
接口太大。 然而,使用现代 IDE 生成代码是没有问题的。配方(假设您使用的是 Eclipse):
Connection
的类,但不实现方法private Connection delegate;
close()
方法的实现以抛出UnsupportedOperationException
但是,就像乔恩所说,我真的会考虑做类似的事情。 也许您只是使用对象关系映射器(例如 Hiberate)来封装所有数据库访问逻辑。 该领域另一个非常有用的框架是 Spring,特别是如果您不想关心
Connection
和DataSource
处理。Like Jon said, if you really want to forbit to call
close()
you could write a decorator implementation that forwards to your "real"Connection
object. I don't post a code example because theConnection
interface is too big. With modern IDEs however it is no problem to generate the code.Recipe (presuming you're using Eclipse):
Connection
, but do not implement the methodsprivate Connection delegate;
close()
method to throw anUnsupportedOperationException
However like Jon said, I would really think about doing something like that. And maybe you just use a Object-Relational-Mapper (e.g. Hiberate) to encapsulate all of your Database access logic. An additional very helpful framework in this area is Spring, especially if you do not want to care about
Connection
andDataSource
handling.(我对 Java 特别不熟悉)
假设您有某种数据库管理对象,您可以让它在尝试任何操作之前确保它已连接。
您可以尝试限制访问以关闭连接,但是您如何决定是否应该关闭它,或者它是否是“意外”(无论您如何定义)?
(I am unfamiliar with Java specifically)
Assuming you have some sort of database managing object, you could have it make sure it is connected before it attempts any operations.
You could try to restrict access to closing the connection but how would you decide if it should be closed, or if it's "accidental" (however you define that)?
我认为你所要求的不可能。
从技术上讲,您可以复制连接对象,但是如果客户端程序员不关闭连接会发生什么情况?
I don't think what you are asking is possible.
You can technically make a copy of your connection object, but then what happens if the client programmer doesn't close the connection?