在WebSphere

时间:2017-02-05 10:22:41

标签: jdbc hive websphere

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

enter image description here

知道我该怎么办?

1 个答案:

答案 0 :(得分:2)

您可以编写一份代表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) {}
    }
}

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

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

相关问题