Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 850
ninesided Avatar asked Oct 10 '09 16:10

ninesided


People also ask

How is Oracle and Java related?

Oracle provides enterprise application developers an end-to-end Java solution for creating, deploying, and managing Java applications. The total solution consists of client-side and server-side programmatic interfaces, tools to support Java development, and a JVM integrated with the Oracle Database.

Which JDBC objects can be used to establish a connection to a database?

The main objects of the JDBC API include: A DataSource object is used to establish connections. Although the Driver Manager can also be used to establish a connection, connecting through a DataSource object is the preferred method. A Connection object controls the connection to the database.


2 Answers

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.

like image 117
JeeBee Avatar answered Oct 11 '22 16:10

JeeBee


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
like image 43
Rob van Laarhoven Avatar answered Oct 11 '22 16:10

Rob van Laarhoven