我们如何升级使用JDBC连接到Cassandra的Java7代码?

eyh26e7m  于 2022-09-27  发布在  Cassandra
关注(0)|答案(1)|浏览(177)

我正在尝试使用Java JDBC连接到Cassandra数据库。我有一个ConnectionManager类,其构造函数接受连接所需的所有参数。
该应用程序使用JDK 7,现在正计划将项目升级到JDK 11。
我们配置了24个Cassandra数据库IP,并将重试值设置为10。
我们已经将IP加载到一个arraylist中,并对其进行循环,然后使用Java Random utils根据arrayllist大小选择IP。下面的executeQuery方法将循环,直到为给定IP建立连接,并将重试10次。

private Connection getConnection() throws SQLException
{
    if (con != null && !con.isClosed()){
        LOG.debug("","Returning existing connection...");
        return con;
    }
    ArrayList<String> hostsCopy = new ArrayList<String>();
    hostsCopy.addAll(hosts);

    while (!hostsCopy.isEmpty())
    {
        String hostToTry = hostsCopy.get(random.nextInt(hostsCopy.size()));
        try
        {
            LOG.debug("","Before connecting : "+CommonUtils.formatDate(Calendar.getInstance().getTime(),"dd-MM-yyyy HH:mm:ss" ));
            con = DriverManager.getConnection("jdbc:cassandra://" + hostToTry + ":" + thriftPort + "/" + keyspace);
            LOG.debug("","After connecting : "+CommonUtils.formatDate(Calendar.getInstance().getTime(),"dd-MM-yyyy HH:mm:ss" ));
            return con;
        } catch (SQLException e)
        {
           LOG.error("", "Unable to connect to " + hostToTry);
            hostsCopy.remove(hostToTry);
        }

        try {
            Thread.currentThread().sleep(cassandraConnectionSleepforDifferentHost);
        } catch (InterruptedException e) {
            LOG.error("", "Thread interrupted ");
            e.printStackTrace();
        }
    }

    LOG.error("", "No remaining servers to connect to");
    throw new RuntimeException(CommonServicesErrorConstants.CASSANDRA_DA_002);
}

查看执行查询方法:

public Pair<ResultSet, Statement> executeQuery(String query) throws SQLException 
{

    int retryCount = 0;
    while (retryCount < retries)
    {
        try
        {
            Connection con = getConnection();

            Statement stmt = con.createStatement();

            ResultSet rs = stmt.executeQuery(query);

            return new Pair<ResultSet, Statement>(rs, stmt);
        } catch (SQLTransientConnectionException e0)
        {
            LOG.error("","Transient error executing query, retrying. Retry count " + retryCount);
            e0.printStackTrace();
        } catch (SQLNonTransientConnectionException e1)
        {
            // this means the consistency level cannot be satisfied.
            if (e1.getCause() instanceof UnavailableException)
            {
                LOG.error("","Unavailable exception executing query, giving up");
                throw e1;
            } else
            {
                LOG.error("","Exception executing query");
                e1.printStackTrace();
                LOG.error("","Retrying a different host");

                // we can swallow this, we're getting a new connection anyway
                try
                {
                    close();
                } catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
        } catch (SQLSyntaxErrorException e2)
        {
            if (UnconfiguredColumnFamilyException.isUnconfiguredColumnFamilyException(e2))
                throw new UnconfiguredColumnFamilyException(e2);
            else
                throw e2;
        }

        retryCount++;

        try {
            Thread.currentThread().sleep(cassandraConnectionSleepforRetry);
        } catch (InterruptedException e) {
            LOG.error("", "Thread interrupted ");
            e.printStackTrace();
        }
    }

    // Close the connection when all the retries are done
    try
    {
        close();
    } catch (SQLException e)
    {
        e.printStackTrace();
    }
    throw new RuntimeException(CommonServicesErrorConstants.CASSANDRA_DA_004);
}

问题:如何升级这段代码

我们可以使用HikariCp连接池概念吗?
提前谢谢!!

eeq64g8w

eeq64g8w1#

DataStax提供了JDBC and ODBC drivers,用于将遗留应用程序连接到Apache Cassandra、DataStax EnterpriseAstra DB
然而,使用the Cassandra Java driver更有意义,因为您将对代码进行重大重构,以便从Java7升级。
重构代码以适应较新版本的Java,但继续使用JDBC(在我看来)是一个错误的决定,因为与Java驱动程序中可用的CQL API相比,您可以访问的Cassandra功能有限。干杯

相关问题