web analytics
Home > Java > JDBC Connections and v$session

JDBC Connections and v$session

When connecting to a database from Java, it’s very handy to tag your connections. When tracking down performance issues and monitoring at a database level, being able to seperate out your program’s connections (which could come from a variety of machines). Most database drivers allow you to specify a program name when creating your connection. The defaults for osuser,terminal and
machine are usually correct. By querying the database’s v$session dictionary view we can see each connection from our application.

Java Code snippet

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);

Check v$session table
select username,osuser,program,machine
from v$session
where username = 'ROB'; 

USERNAME  OSUSER       PROGRAM             MACHINE
--------- -----------  ------------------  -----------
ROB       rlaarhoven   My Program Name     dto.tud.tudelft.nl

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by sweetCaptcha


Trackbacks:0

Listed below are links to weblogs that reference
JDBC Connections and v$session from De Roeptoeter
TOP
Wordpress Code Snippet by Allan Collins