[英]how to get connection string without opening a connection in java?
Is it possible to do this in Java? 用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.
连接字符串中的主机无法解析,因此在能够在连接上调用getMetaData之前,我在DataSource的getConnection()中获取了SQLException。
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();
因为主机不可访问,所以
connection = ds.getConnection();
would throw exception and causes new ProbeResult(request.getTargetId(), exp)
returned. 会引发异常并导致返回
new ProbeResult(request.getTargetId(), exp)
。 However, the exception only has error message like IO Error: The Network Adapter could not establish the connection
. 但是,该异常仅具有诸如
IO Error: The Network Adapter could not establish the connection
类的错误消息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. 这是来自Oracle的愚蠢错误消息。 Network adapters don't establish connections.
网络适配器不建立连接。 TCP does.
TCP。 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. 但是,通过追踪原始异常中的
detail
链,进一步了解调用堆栈。 Somewhere there should be a ConnectException
with the message you want. 在某个地方应该有一个
ConnectException
与您想要的消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.