简体   繁体   中英

how to get connection string without opening a connection in java?

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.

This is my code:

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);
    }
  }
}    

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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