简体   繁体   English

从 jdbc 连接到 oracle 服务器获取活动主机和端口(不解析 url)

[英]Get active Host and Port from jdbc connection to oracle server (don't parse url)

is there a way to get connected host and port of a JDBC connection to a Oracle DB Server?有没有办法将 JDBC 连接的主机和端口连接到 Oracle DB 服务器?

I know, i could parse the URL.我知道,我可以解析 URL。 But we use failover and i want to know, which server i'm actually connected with.但是我们使用故障转移,我想知道我实际连接的是哪个服务器。 Parsing of URLs is static and error prone because of the different formats we us.由于我们使用不同的格式,URL 的解析是静态的并且容易出错。

Couldn't find it in the connection metadata.在连接元数据中找不到它。

With 'select * from global_name' i could get the servicename.使用 'select * from global_name' 我可以获得服务名称。 But i haven't found a way to get the host and port, we are connected to.但是我还没有找到获取主机和端口的方法,我们已连接到。

Any idea?任何的想法?

here are some info you can get from a Connection:以下是您可以从 Connection 获得的一些信息:

            Connection dbConnection = null;
        try {
            dbConnection = dataSource.getConnection();
            DatabaseMetaData dbMetaData = dbConnection.getMetaData();
            getLogger().debug("DB Product Name   = " + dbMetaData.getDatabaseProductName());
            getLogger().debug("DB Product Version= " + dbMetaData.getDatabaseProductVersion());
            getLogger().debug("DB Driver Name    = " + dbMetaData.getDriverName());
            getLogger().debug("DB Driver Version = " + dbMetaData.getDriverVersion());
            getLogger().debug("DB Username         = " + dbMetaData.getUserName());
            getLogger().debug("DB URL            = " + dbMetaData.getURL());
        } catch (Exception e) {
            getLogger().debug("Failed to recover DatabaseMetaData: "+e.getMessage(), e);
        } finally {
            if (dbConnection != null) {
                try {
                    dbConnection.close();
                } catch (Exception ex) {
                    getLogger().error("Failed to close the DB connection: "+ex.getMessage(), ex);
                }
            }
        }

Check the DatabaseMetaData if you need more/different info.如果您需要更多/不同的信息,请检查 DatabaseMetaData。 Example of output:输出示例:

DB Product Name   = Oracle
DB Product Version= Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
DB Driver Name    = Oracle JDBC driver
DB Driver Version = 11.2.0.2.0
DB Schema         = XXXXX
DB URL            = jdbc:oracle:thin:@XXX.XXX.XXX.XXX:11010:MYSID

Not sure about the port (which is really the port on the listener), but for host you might try:不确定端口(实际上是侦听器上的端口),但对于主机,您可以尝试:

select utl_inaddr.get_host_name, utl_inaddr.get_host_address from dual;

There are other options on db side (such as querying v$instance), see here . db 端还有其他选项(例如查询 v$instance),请参见此处 For example, the SID you might use:例如,您可能使用的 SID:

select sys_context('userenv','instance_name') from dual;

Hope that helps.希望有帮助。 Also note that I use utl_inaddr, but only for logging and email/alerting purposes.另请注意,我使用 utl_inaddr,但仅用于日志记录和电子邮件/警报目的。

EDIT:编辑:

Also, be aware that there are some concerns with spoofing Oracle session info, partly why I noted that I only use this to add context to alerts and logs.另外,请注意欺骗 Oracle 会话信息存在一些问题,部分原因是我注意到我仅使用它来向警报和日志添加上下文。 I don't pretend to know all the ins/outs, but basically this issue concerns a client pretending to be someone (or from somewhere) else.我不会假装知道所有的来龙去脉,但基本上这个问题涉及假装是其他人(或来自其他地方)的客户。 Since you're controlling the connection via your java app, it shouldn't be a concern, but see here for more if you care.由于您通过 Java 应用程序控制连接,因此不必担心,但如果您关心,请参阅此处了解更多信息。

这应该为您提供正在运行的实例的主机名:

select host_name from v$instance

try one of:尝试以下之一:

select sys_context('userenv','instance_name') from dual;
select sys_context('userenv','instance') from dual;
select sys_context('userenv','server_host') from dual;

Documentation: https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions165.htm文档: https : //docs.oracle.com/cd/B19306_01/server.102/b14200/functions165.htm

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

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