12

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.

ninesided
  • 23,085
  • 14
  • 83
  • 107

5 Answers5

23
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:

dbms_application_info.set_client_info
dbms_application_info.set_module
dbms_application_info.set_action
Peter Lang
  • 54,264
  • 27
  • 148
  • 161
Rob van Laarhoven
  • 8,737
  • 2
  • 31
  • 49
  • Beware of [arbitrary length limits](https://stackoverflow.com/a/35072449/6730571) for those `v$session.*` properties – Hugues M. Dec 11 '18 at 21:05
3

There is also an Oracle function:

dbms_application_info.set_client_info('Client Info');

which sets the ClientInfo column in v$session.

This might be useful if you only have access to the Connection rather than the underlying DataSource or DriverManager.

JeeBee
  • 17,476
  • 5
  • 50
  • 60
3

Since oracle jdbc 12.1 you can set some client-info values via jdbc api, i.e. you can do

connection.setClientInfo("OCSID.CLIENTID", "MyClientId");

for properties OCSID...

ACTION, CLIENTID, ECID, MODULE, SEQUENCE_NUMBER and DBOP

See 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".

Manuel
  • 649
  • 8
  • 13
2

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
2

Starting with 12.1 the setEndToEndMetrics is deprecated, you may use setClientInfo see the documentation for 12.2 here

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

 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.

Note also that you must use the Oracle (unwrapped) conenction - Check this for reference.

Marmite Bomber
  • 19,886
  • 4
  • 26
  • 53
  • Thanks! I didn't realize the change is performed after any statement processing. I do not understand why the connection need to be unwrapped if the method is part of JDBC API, not the internal Oracle connection only (even the application servers pool connection wrapper is supporting Connection interface)? – Marek-A- Nov 23 '20 at 17:22