2

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

4

3 回答 3

0

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;
}
于 2015-08-31T11:17:08.840 回答
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的编译时耦合

于 2019-11-29T14:36:02.457 回答
-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;
    }
于 2016-04-07T09:31:06.647 回答