我正在尝试使用Java连接Cassandra。我有一个ConnectionManager类,它的构造函数接受连接所需的所有参数。
应用程序使用JDK 7,现在正计划将项目升级到JDK 11。
我们配置了24个Cassandra数据库IP,并将重试值设置为10。
我们已经将it加载到一个数组列表中,并且我们已经将它循环起来,并且使用Java随机使用程序,我们将根据数组列表大小来选择it。下面的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连接池概念吗?
提前谢谢!!
发布于 2022-09-06 07:31:04
DataStax提供了将遗留应用程序连接到Apache、DataStax企业和阿斯特拉数据库的JDBC和ODBC驱动程序。
但是,使用卡桑德拉Java驱动程序更有意义,因为您无论如何都要对代码进行重要的重构,以便从Java 7升级。
为更新版本的Java重构代码,但我认为继续使用JDBC是一个错误的决定,因为与Java驱动程序中可用的CQL相比,可以访问的Cassandra特性是有限的。干杯!
https://stackoverflow.com/questions/73617857
复制相似问题