[英]How do I make my Java application identify itself to Oracle on connection?
When my application connects to an Oracle database I want to be able to see by looking at the active sessions in the database that it is connected. 当我的应用程序连接到Oracle数据库时,我希望能够通过查看它所连接的数据库中的活动会话来查看。 Currently it identifies itself as "JDBC Thin Client" because that's the driver that I'm using, but other Java based applications that I have are somehow able to set this value to something more meaningful, like "SQL Developer". 当前,它将自己标识为“ JDBC Thin Client”,因为这是我正在使用的驱动程序,但是我拥有的其他基于Java的应用程序能够以某种方式将此值设置为更有意义的值,例如“ SQL Developer”。 I thought it was a property of the Connection
or the OracleDataSource
, but I've not managed to find one that does the trick. 我以为这是Connection
或OracleDataSource
,但我还没有找到找到解决问题的方法。 Is this possible? 这可能吗? In case it matters, I'm using Java 1.5, with Oracle 10g and the 10g thin driver. 万一有问题,我将使用Java 1.5,Oracle 10g和10g瘦驱动程序。
java.util.Properties props = new java.util.Properties();
props.setProperty("password","mypassword");
props.setProperty("user","myusername");
props.put("v$session.osuser", System.getProperty("user.name").toString());
props.put("v$session.machine", InetAddress.getLocalHost().getCanonicalHostName());
props.put("v$session.program", "My Program Name");
DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
Connection conn=
DriverManager.getConnection("jdbc:oracle:thin:@myhostname:1521:mysid", props);
SQL>select username,osuser,program,machine
from v$session
where username = 'ROB';
USERNAME OSUSER PROGRAM MACHINE
--------- ----------- ------------------ -----------
ROB rmerkw My Program Name machine
At application level you can use the following methods to set client_info , module and action in v$session : 在应用程序级别,您可以使用以下方法在v $ session中设置client_info , module和action :
dbms_application_info.set_client_info
dbms_application_info.set_module
dbms_application_info.set_action
You need to define the connection property v$session.program
in your data source, in such a way that that property will be added to each connection. 您需要在数据源中定义连接属性v$session.program
,以将该属性添加到每个连接中。 How you do that depends on your data source implementation. 如何执行取决于您的数据源实现。 The value you set the property to will appear in oracle's active session table. 您将属性设置为的值将出现在oracle的活动会话表中。
There is also an Oracle function: 还有一个Oracle函数:
dbms_application_info.set_client_info('Client Info');
which sets the ClientInfo column in v$session. 这将在v $ session中设置ClientInfo列。
This might be useful if you only have access to the Connection rather than the underlying DataSource or DriverManager. 如果您只能访问Connection而不是基础DataSource或DriverManager,则这可能很有用。
Since oracle jdbc 12.1 you can set some client-info values via jdbc api, ie you can do 从oracle jdbc 12.1开始,您可以通过jdbc api设置一些客户端信息值,即可以
connection.setClientInfo("OCSID.CLIENTID", "MyClientId");
for properties OCSID... 对于属性OCSID ...
ACTION, CLIENTID, ECID, MODULE, SEQUENCE_NUMBER and DBOP ACTION,CLIENTID,ECID,MODULE,SEQUENCE_NUMBER和DBOP
See https://docs.oracle.com/database/121/JJDBC/jdbcvers.htm#JJDBC29006 参见https://docs.oracle.com/database/121/JJDBC/jdbcvers.htm#JJDBC29006
Setting PROGRAM doesn't work this way, you can do that as described in the accepted answer or somewhat easier by setting the System property "oracle.jdbc.v$session.program". 设置PROGRAM不能以这种方式工作,您可以按照接受的答案中的说明进行操作,也可以通过设置系统属性“ oracle.jdbc.v $ session.program”来简化操作。
Starting with 12.1 the setEndToEndMetrics
is deprecated, you may use setClientInfo
see the documentation for 12.2 here 从12.1开始,不建议使用setEndToEndMetrics
,可以使用setClientInfo
参见此处的12.2文档。
Here a snippet of the usage 这里是用法的摘要
// "conn" is an instance of java.sql.Connection:
conn.setClientInfo("OCSID.CLIENTID", "clientID");
conn.setClientInfo("OCSID.MODULE", "myModule");
conn.setClientInfo("OCSID.ACTION", "myAction");
You may see the setting in V$SESSION
with this query of the relevant session 通过相关会话的查询,您可能会在V$SESSION
看到设置
select MODULE, ACTION, CLIENT_IDENTIFIER from v$session where ...
but only after a next statement is executed with this connection . 但仅在使用此连接执行下一条语句之后 。 The call of setClientInfo
triggers no extra roundtrip this information is passed whit the next call. setClientInfo
的调用不会触发额外的往返,此信息将在下次调用时传递。
Note also that you must use the Oracle (unwrapped) conenction - Check this for reference. 另请注意,您必须使用Oracle(未包装)连接-选中此作为参考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.