ConnectionPoolDataSource

ConnectionPoolDataSource

本文介绍了在WebSphere上定义hive-jdbc JNDI数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

,如果任何人想要请求支持添加对javax.sql.DataSource的支持。 / p>

I am trying to setup a JNDI Data Source on WebSphere 8.5.5.11, using hive-jdbc.jar.When using the console in WebSphere, and using the form to create a new JDBC provider, there is a field for the implementation class name. WebSphere requires that the class implements javax.sql.XADataSource or javax.sql.ConnectionPoolDataSource. However, the hive-jdbc driver implements non of those, it implements only java.sql.DataSource.For this reason, it doesn't work, WebSphere reports an error when trying to save the form.

Any idea what can I do about this?

解决方案

You can write a trivial implementation of javax.sql.ConnectionPoolDataSource that delegates to the javax.sql.DataSource implementation. Here is an example,

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) {}
    }
}

Package your compiled class in a JAR alongside the JDBC driver JAR(s), and configure your custom JDBC provider in WebSphere Application Server to point at this JAR as though it were a part of the JDBC driver. Specify the implementation class name as example.datasource.HiveConnectionPoolDataSource or whatever package/name you chose for your own implementation. You should then be able to use the JDBC driver.

Also adding a link to the WebSphere Application Server request for enhancements page if anyone wants to request that support for javax.sql.DataSource be added.

这篇关于在WebSphere上定义hive-jdbc JNDI数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 18:04