如何让我的 Java 应用程序在连接时向 Oracle 标识自己?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1548400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make my Java application identify itself to Oracle on connection?
提问by ninesided
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. 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". I thought it was a property of the Connection
or the OracleDataSource
, but I've not managed to find one that does the trick. Is this possible? In case it matters, I'm using Java 1.5, with Oracle 10g and the 10g thin driver.
当我的应用程序连接到 Oracle 数据库时,我希望能够通过查看它所连接的数据库中的活动会话来查看。目前它将自己标识为“JDBC 瘦客户端”,因为这是我使用的驱动程序,但我拥有的其他基于 Java 的应用程序能够以某种方式将此值设置为更有意义的值,例如“SQL Developer”。我认为这是Connection
or 的一个属性OracleDataSource
,但我还没有找到一个可以解决问题的方法。这可能吗?以防万一,我使用的是 Java 1.5、Oracle 10g 和 10g 瘦驱动程序。
回答by Rob van Laarhoven
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, moduleand actionin v$session:
在应用程序级别,您可以使用以下方法在v$session 中设置client_info、模块和操作:
dbms_application_info.set_client_info
dbms_application_info.set_module
dbms_application_info.set_action
回答by skaffman
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. 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.
您需要v$session.program
在数据源中定义连接属性,以便将该属性添加到每个连接中。您如何做到这一点取决于您的数据源实现。您为该属性设置的值将出现在 oracle 的活动会话表中。
回答by JeeBee
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,这可能很有用。
回答by Manuel
Since oracle jdbc 12.1 you can set some client-info values via jdbc api, i.e. you can do
从 oracle jdbc 12.1 开始,您可以通过 jdbc api 设置一些 client-info 值,即您可以这样做
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”来更容易一些。
回答by Marmite Bomber
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 thisfor reference.
另请注意,您必须使用 Oracle(未包装的)连接 - 检查此内容以供参考。