在 WebSphere 上定义 hive-jdbc JNDI 数据源

Define hive-jdbc JNDI data source on WebSphere

我正在尝试使用 hive-jdbc.jar 在 WebSphere 8.5.5.11 上设置 JNDI 数据源。 在 WebSphere 中使用控制台并使用表单创建新的 JDBC 提供程序时,有一个用于实现 class 名称的字段。 WebSphere 要求 class 实现 javax.sql.XADataSourcejavax.sql.ConnectionPoolDataSource。但是,hive-jdbc 驱动程序没有实现这些,它只实现了 java.sql.DataSource。 由于这个原因,它不起作用,WebSphere 在尝试保存表单时报告错误。

知道我能做些什么吗?

您可以编写一个简单的 javax.sql.ConnectionPoolDataSource 实现,委托给 javax.sql.DataSource 实现。这是一个例子,

package example.datasource;

import java.sql.*;
import javax.sql.*;

public class HiveConnectionPoolDataSource extends org.apache.hive.jdbc.HiveDataSource implements ConnectionPoolDataSource {
    public PooledConnection getPooledConnection() throws SQLException {
        return new HivePooledConnection(null, null);
    }

    public PooledConnection getPooledConnection(String user, String password) throws SQLException {
        return new HivePooledConnection(user, password);
    }

    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return ConnectionPoolDataSource.class.equals(iface) || super.isWrapperFor(iface);
    }

    public <T> T unwrap(Class<T> iface) throws SQLException {
        return ConnectionPoolDataSource.class.equals(iface) ? (T) this : super.unwrap(iface);
    }

    class HivePooledConnection implements PooledConnection {
        private Connection con;
        private final String user;
        private final String password;

        HivePooledConnection(String user, String password) {
            this.user = user;
            this.password = password;
        }

        public void addConnectionEventListener(ConnectionEventListener listener) {}

        public void addStatementEventListener(StatementEventListener listener) {}

        public void close() throws SQLException {
            if (con != null) {
                con.close();
                con = null;
            }
        }

        public Connection getConnection() throws SQLException {
            if (con == null || con.isClosed()) {
                con = user == null
                        ? HiveConnectionPoolDataSource.this.getConnection()
                        : HiveConnectionPoolDataSource.this.getConnection(user, password);
                return con;
            } else
                throw new IllegalStateException();
        }

        public void removeConnectionEventListener(ConnectionEventListener listener) {}

        public void removeStatementEventListener(StatementEventListener listener) {}
    }
}

将已编译的 class 与 JDBC 驱动程序 JAR 一起打包到 JAR 中,并在 WebSphere Application Server 中配置自定义 JDBC 提供程序以指向此 JAR,就像它是 JDBC 驱动程序的一部分。将实施 class 名称指定为 example.datasource.HiveConnectionPoolDataSource 或您为自己的实施选择的任何 package/name。然后您应该能够使用 JDBC 驱动程序。

如果有人想请求添加对 javax.sql.DataSource 的支持,还要在 WebSphere Application Server request for enhancements page 中添加 link。