'How to get JDBC connection id?
I need to get the JDBC connection id(UUID) with Connection object. Is there is any way to get the connection id?
Solution 1:[1]
Well, if you meen "sql-connection to sql-server", then jdbc has no standard instruments for this. Here is handmade example for jdbc:mysql (beware - Reflection and restricted characters):
private long getJdbcConnectionId(Connection conn) {
long cid = 0;
try {
Field f_conn__conn = conn.getClass().getSuperclass().getDeclaredField("_conn");
f_conn__conn.setAccessible(true);
Object o_conn__conn = f_conn__conn.get(conn);
Field f_conn__conn__conn = o_conn__conn.getClass().getSuperclass().getDeclaredField("_conn");
f_conn__conn__conn.setAccessible(true);
Object o_conn__conn__conn = f_conn__conn__conn.get(o_conn__conn);
Field f_connectionId = o_conn__conn__conn.getClass().getSuperclass().getDeclaredField("connectionId");
f_connectionId.setAccessible(true);
cid = f_connectionId.getLong(o_conn__conn__conn);
f_connectionId.setAccessible(false);
f_conn__conn__conn.setAccessible(false);
f_conn__conn.setAccessible(false);
} catch (Exception e) {
e.printStackTrace();
}
return cid;
}
Solution 2:[2]
EDIT:
ok this way to avoid compile-time coupling, using reflection
import java.sql.Connection;
import java.sql.DriverManager;
...
public String getMysqlConnectionId()
{
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://host:3306/schema", "myuser", "mypassword");
Class<?> clazz = Class.forName("com.mysql.jdbc.MySQLConnection");
Object o = connection.unwrap(clazz);
return (String) clazz.getMethod("getId").invoke(o).toString();
} catch ( Exception e ) {
return e.getMessage();
}
}
By casting your java.sql.Connection object to com.mysql.jdbc.MySQLConnection and using getId()
import java.sql.Connection;
import java.sql.DriverManager;
import com.mysql.jdbc.MySQLConnection;
...
public long getMysqlConnectionId()
{
Connection connection = DriverManager.getConnection("jdbc:mysql://host:3306/schema", "myuser", "mypassword");
// would throw a ClassCastException in case this is not a mysql one, of course
return ((MySQLConnection) connection).getId();
}
But this is sort of terribly coupling your project to MySQL at compile time
note: I won't be offended when I get down-voted for this, it is sort of deserved ;-)
<!-- pom.xml -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>x.y.z</version>
</dependency>
Some other people apparently are using connection.unwrap(com.mysql.jdbc.MySQLConnection) which might feels a bit more appropriate, yet does not remove the compile-time coupling to com.mysql
https://github.com/prestodb/presto/issues/9425
Solution 3:[3]
Here is my example how to get Oracle SID:
public int getConnectionSid() {
try {
if (connection == null || connection.isClosed())
return -1;
if (connectionSid == -1) {
try {
Method method = connection.getClass().getDeclaredMethod("getSessionId", null);
method.setAccessible(true);
connectionSid = (Integer)method.invoke(nativeConnection, null);
method.setAccessible(false);
} catch (NoSuchMethodException e) {
return -1;
} catch (IllegalAccessException e) {
return -1;
} catch (InvocationTargetException e) {
return -1;
}
}
return connectionSid;
} catch (SQLException e) {
return -1;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | vssk |
