如何获取JDBC连接ID?

时间:2012-11-15 12:55:17

标签: jdbc database-connection

我需要使用Connection对象获取JDBC连接ID(UUID)。有没有办法获得连接ID?

3 个答案:

答案 0 :(得分:0)

好吧,如果你对sql-server"进行sql-connection,那么jdbc没有标准的工具。这是jdbc的手工制作示例:mysql(注意 - 反射和受限制的字符):

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

答案 1 :(得分:0)

编辑:

这样可以避免使用反射的编译时耦合

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

通过将java.sql.Connection对象投射到com.mysql.jdbc.MySQLConnection并使用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();
}

但这是在编译时将您的项目与MySQL紧密耦合的一种方式

注意:当我为此投票时,我不会感到生气,这是应得的;-)

<!-- pom.xml -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>x.y.z</version>
</dependency>

其他人显然正在使用connection.unwrap(com.mysql.jdbc.MySQLConnection),这可能会觉得更合适,但并没有消除与com.mysql的编译时耦合。 https://github.com/prestodb/presto/issues/9425

答案 2 :(得分:-1)

以下是如何获取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;
    }