如何避免在 Java 中意外关闭 SQL 连接?

发布于 2024-07-25 10:52:11 字数 322 浏览 2 评论 0原文

目前我正在为事务管理做的是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

━╋う一瞬間旳綻放 2024-08-01 10:52:12

只是纪律。 一般来说,方法不应该尝试负责关闭作为参数传递给它们的东西 - 除了创建一个新对象来包装现有对象的情况。

避免在 updateTableX 中关闭连接的方法只是确保您不会在代码中调用 close() 。 这实际上与任何其他错误没有什么不同。 如何阻止 updateTableX 任意更新不同的表、抛出异常或执行任何其他不该做的事情? 代码审查、单元测试、集成测试、手动测试等...

我的意思是您可以编写一个Connection实现,它包装另一个连接并通过代理所有方法except close() 但这听起来像是浪费时间 - 如果您不相信相关开发人员不会关闭连接,您是否相信他们能够获取其余代码正确的?

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 to close() into the code. This is no different than any other bug really. How do you stop updateTableX 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 except close() 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?

清欢 2024-08-01 10:52:12

就像乔恩所说,如果您确实想禁止调用 close(),您可以编写一个装饰器实现来转发到您的“真实”Connection 对象。 我没有发布代码示例,因为 Connection 接口太大。 然而,使用现代 IDE 生成代码是没有问题的。

配方(假设您使用的是 Eclipse):

  1. 创建一个实现 Connection 的类,但不实现方法
  2. 创建一个字段 private Connection delegate;
  3. 选择字段名称 -> ; 来源(菜单)-> “使用字段生成构造函数”-> 确保选择该字段并按确定
  4. 选择字段名称 -> 来源(菜单)-> “生成委托方法...”-> 检查字段中的每个方法
  5. 更改 close() 方法的实现以抛出 UnsupportedOperationException

但是,就像乔恩所说,我真的会考虑做类似的事情。 也许您只是使用对象关系映射器(例如 Hiberate)来封装所有数据库访问逻辑。 该领域另一个非常有用的框架是 Spring,特别是如果您不想关心 ConnectionDataSource 处理。

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 the Connection interface is too big. With modern IDEs however it is no problem to generate the code.

Recipe (presuming you're using Eclipse):

  1. Create a class that implements Connection, but do not implement the methods
  2. Create a field private Connection delegate;
  3. Select the field name -> Source (Menu) -> "Generate Constructor using fields" -> make sure the field is selected and press ok
  4. Select the field name -> Source (Menu) -> "Generate Delegate Methods..." -> check every method on you field
  5. Change the implementation of the close() method to throw an UnsupportedOperationException

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 and DataSource handling.

无尽的现实 2024-08-01 10:52:12

(我对 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)?

千纸鹤 2024-08-01 10:52:12

我认为你所要求的不可能。

从技术上讲,您可以复制连接对象,但是如果客户端程序员不关闭连接会发生什么情况?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文