【问题标题】:Cassandra Connection Session.execute blocks infinitelyCassandra Connection Session.execute 无限阻塞
【发布时间】:2018-01-03 09:32:02
【问题描述】:

我想使用以下代码连接到我的本地 cassandra 数据库:

public class StartDriver {
public static void main(String[] args) {

    Cluster cluster = null;
    try {
        cluster = Cluster.builder() // (1)
                .addContactPoint("127.0.0.1").withPort(9042).build();
        SocketOptions socketOptions = cluster.getConfiguration().getSocketOptions();
        socketOptions.setConnectTimeoutMillis(1000);

        System.out.println("Connection established");
        Session session = cluster.connect(); // (2)
        System.out.println(session.getState().getConnectedHosts());
        System.out.println("Try to execute Query");
        ResultSet rs = session.execute("SELECT * FROM htw.student");
        System.out.println("Query executed");// (3)
        Row row = rs.one();

    //  System.out.println(row.getString("release_version"));
        if (cluster != null) cluster.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

可以建立连接,但是在 session.execute() 程序会永远挂起,没有任何错误消息。我在 windows 下使用 Cassandra 3.8.0。可以通过 cqlsh 和 devCenter 进行连接,并且效果很好。有人可以帮忙吗?我需要更改 cassandra.yaml 中的任何配置吗?

我得到的输出如下:

 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Connection established
[/127.0.0.1:9042]
Try to execute Query

【问题讨论】:

  • 你关闭连接了吗?
  • 在代码中,连接将被关闭的部分永远不会到达,并且在测试时重新启动了 cassandra 几次。
  • 请告诉我例外情况。并清除问题。
  • 问题是eclipse中也不例外。程序执行挂在“session.execute()”。
  • 帮我一个忙。执行这个语句而不是你的语句,然后告诉我你的结果。 "SELECT count(*) FROM htw.student ;"

标签: java cassandra driver


【解决方案1】:

运行这段代码,请告诉我你的输出。

public class StartDriver {
public static void main(String[] args) {

Cluster cluster = null;
try {
    cluster = Cluster.builder() // (1)
            .addContactPoint("127.0.0.1").withPort(9042).build();
    SocketOptions socketOptions = cluster.getConfiguration().getSocketOptions();
    socketOptions.setConnectTimeoutMillis(1000);

    System.out.println("Connection established");
    Session session = cluster.connect(); // (2)
    System.out.println(session.getState().getConnectedHosts());
    System.out.println("Try to execute Query");
    ResultSet rs = session.execute("SELECT count(*) FROM htw.student");
    System.out.println("Query executed");// (3)
    Row row = rs.one();

    System.out.println(row.getLong("count"));
    if (cluster != null) cluster.close();
} catch (Exception e) {
    e.printStackTrace();
}

}

【讨论】:

  • 我添加了您的代码行并将输出添加到第一篇文章中。
【解决方案2】:

问题是在类路径中有一些像番石榴一样的库,它们是从cassandra-driver-core 使用的。我将项目转换为 Maven 项目,从这一点开始,lib 似乎相互阻碍。我从 libs 文件夹中删除了 lib',只使用了 maven 依赖项,而不是像魅力一样使用它。

【讨论】:

    猜你喜欢
    • 2018-03-13
    • 1970-01-01
    • 2011-02-25
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    相关资源
    最近更新 更多