本文介绍了如何在不打开java连接的情况下获取连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Java中执行此操作?连接字符串中的主机是不可解析的,所以在我能够在连接上调用getMetaData之前,我在DataSource的getConnection()中得到了SQLException。

Is it possible to do this in Java? Host in the connection string is unresolvable so I am getting SQLException in getConnection() of DataSource before I am able to call getMetaData on the connection.

这是我的代码:

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错误:网络适配器无法建立连接。它不会在连接字符串中提供主机名。我想显示主机名(或连接字符串),以便可以诊断它

Because the host is unreachable, connection = ds.getConnection(); would throw exception and causes new ProbeResult(request.getTargetId(), exp) returned. However, the exception only has error message like IO Error: The Network Adapter could not establish the connection. It doesn't give the host name in the connection string. I want to display the host name (or connection string) so that it can be diagnosed

推荐答案

这是来自Oracle的一条愚蠢的错误消息。网络适​​配器不建立连接。 TCP确实如此。并且,正如您所说,它会抑制所有重要信息。

It's a stupid error message from Oracle. Network adapters don't establish connections. TCP does. And, as you say, it suppresses all the important information.

但是通过追逐详细信息,可以进一步查看调用堆栈code>来自原始异常的链。某处应该有一个 ConnectException ,带有你想要的信息。

But have a look further down the call stack, by chasing the detail chain from the original exception. Somewhere there should be a ConnectException with the message you want.

这篇关于如何在不打开java连接的情况下获取连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 12:38