繁体   English   中英

如何在不打开Java连接的情况下获取连接字符串?

[英]how to get connection string without opening a connection in java?

用Java可以做到吗? 连接字符串中的主机无法解析,因此在能够在连接上调用getMetaData之前,我在DataSource的getConnection()中获取了SQLException。

这是我的代码:

DataSource ds = null;
Connection connection = null;
DataSourceProbeRequest request = null;

try {
  request = (DataSourceProbeRequest) probeRequest;

  ds = request.getDataSource();

  connection = ds.getConnection();
  final boolean validConnection = connection != null && connection.isValid(5000);
  if (validConnection) {
    LOGGER.info("Connection is ok: " + request.getTargetId());
    return new ProbeResult(request.getTargetId(), buildDatasourceMsg(connection, true));
  } else {
    return new ProbeResult(request.getTargetId(), new Exception(buildDatasourceMsg(connection, false)));
  }
} catch (Exception exp) {
  return new ProbeResult(request.getTargetId(), exp);
} finally {
  if (connection != null) {
    try {
      connection.close();
    } catch (SQLException e) {
      LOGGER.warn("Failed to close database connection", e);
    }
  }
}    

因为主机不可访问,所以connection = ds.getConnection(); 会引发异常并导致返回new ProbeResult(request.getTargetId(), exp) 但是,该异常仅具有诸如IO Error: The Network Adapter could not establish the connection类的错误消息IO Error: The Network Adapter could not establish the connection 它没有在连接字符串中给出主机名。 我想显示主机名(或连接字符串)以便可以对其进行诊断

这是来自Oracle的愚蠢错误消息。 网络适​​配器不建立连接。 TCP。 而且,正如您所说,它隐藏了所有重要信息。

但是,通过追踪原始异常中的detail链,进一步了解调用堆栈。 在某个地方应该有一个ConnectException与您想要的消息。

暂无
暂无

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

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