How do I make my Java application identify itself to Oracle on connection?

前端 未结 5 1342
深忆病人
深忆病人 2020-12-24 09:33

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 i

相关标签:
5条回答
  • 2020-12-24 09:35

    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.

    0 讨论(0)
  • 2020-12-24 09:38
    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
    
    0 讨论(0)
  • 2020-12-24 09:42

    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.

    0 讨论(0)
  • 2020-12-24 09:50

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

    0 讨论(0)
  • 2020-12-24 10:01

    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.

    0 讨论(0)
提交回复
热议问题