繁体   English   中英

try-with-resources 调用 close() 失败

[英]try-with-resources fails to call close()

我正在使用方便的 try-with-resources 语句来关闭连接。 这在大多数情况下都很有效,但只有在一种非常简单的方法中它才能正常工作。 即,这里:

public boolean testConnection(SapConnection connection) {
  SapConnect connect = createConnection(connection);
  try ( SapApi sapApi = connect.connect() ) {
    return ( sapApi != null );
  } catch (JCoException e) {
    throw new UncheckedConnectionException("...", e);
  }
}

sapApi 对象为非空,该方法返回 true,但从未调用 sapApi 的 close() 方法。 我现在求助于使用一个工作正常的 finally 块。 但这很令人费解。 Java 字节码还包含对 close 的调用。 有没有人见过这种行为?

编辑以澄清情况:

这是 SapApi,当然,它实现了 AutoCloseable。

class SapApi implements AutoCloseable {

  @Override
  public void close() throws JCoException {
    connection.close(); // this line is not hit when leaving testConnection(..)
  }
..
}

下面是与 testConnection(..) 同一个类中的另一个方法。 这里 SapApi.close() 在返回之前被调用。

@Override
public List<Characteristic> selectCharacteristics(SapConnect aConnection,   InfoProvider aInfoProvider) {
  try (SapApi sapi = aConnection.connect()) {
    return sapi.getCharacteristics(aInfoProvider);
  } catch ( Exception e ) {
    throw new UncheckedConnectionException(e.getMessage(), e);
  }
}

编辑2:这是 SapConnect.connect():

SapApi connect() {
  try {
    ... // some setup of the connection
    return new SapApi(this); // single return statement
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

SapApi 没有子类。 上面的 close 方法只有一种实现。 特别是,没有空的 close()。

为了调用 close(),SapApi 必须实现 AutoCloseable 接口,但既然我们在谈论连接,那么您 SapApi 将实现抛出 IOException 的 Closable 接口更为合适。

阅读: http : //tutorials.jenkov.com/java-exception-handling/try-with-resources.html

我不知道你的意思

这在大多数情况下效果很好

1 在 try-with-resources 中使用时,通常SapApi是关闭的

或者

2 它通常适用于SapApi以外的资源

我是根据数字 2 的假设来回答的。

Try-with-resources 只能在 Java 7 中用于实现AutoCloseable接口的资源。 所以我的第一个建议是让您检查SapConnectSapApi (无论它们是什么)的 API,以确定是否是这种情况。

一种猜测:也许connect返回 SapApi 的子类或代理类。 如果没有进行任何更改,则close不执行任何操作,否则只能调用super.close()

我会把它作为答案,因为即使没有调用任何方法,AutoCloseable 也能工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM