Java 如何在没有内存泄漏的情况下清理Cassandra资源?

Java 如何在没有内存泄漏的情况下清理Cassandra资源?,java,cassandra-2.0,datastax,Java,Cassandra 2.0,Datastax,我正在尝试清理我的应用程序的Cassandra连接资源,我的应用程序在一段时间后挂起。我一直在下面得到这个例外。 我不知道如何解决这个问题。我的堆栈跟踪在下面 卡桑德拉资源清理的最佳实践是什么?应用程序部署在Tomcat服务器上 SEVERE: RuntimeException while executing runnable com.google.common.util.concurrent.Futures$ChainingListenableFuture@7c435ce7 with exec

我正在尝试清理我的应用程序的Cassandra连接资源,我的应用程序在一段时间后挂起。我一直在下面得到这个例外。 我不知道如何解决这个问题。我的堆栈跟踪在下面

卡桑德拉资源清理的最佳实践是什么?应用程序部署在Tomcat服务器上

SEVERE: RuntimeException while executing runnable com.google.common.util.concurrent.Futures$ChainingListenableFuture@7c435ce7 with executor com.google.common.util.concurrent.MoreExecutors$ListeningDecorator@6358d4ec
java.util.concurrent.RejectedExecutionException: Task com.google.common.util.concurrent.Futures$ChainingListenableFuture@7c435ce7 rejected from java.util.concurrent.ThreadPoolExecutor@96203d77[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 2]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2059)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1383)
at com.google.common.util.concurrent.MoreExecutors$ListeningDecorator.execute(MoreExecutors.java:484)
at com.google.common.util.concurrent.ExecutionList.executeListener(ExecutionList.java:156)
at com.google.common.util.concurrent.ExecutionList.add(ExecutionList.java:101)
at com.google.common.util.concurrent.AbstractFuture.addListener(AbstractFuture.java:170)
at com.google.common.util.concurrent.Futures.transform(Futures.java:608)
at com.google.common.util.concurrent.Futures.transform(Futures.java:717)
at com.datastax.driver.core.SessionManager.toPreparedStatement(SessionManager.java:185)
at com.datastax.driver.core.SessionManager.prepareAsync(SessionManager.java:126)
at com.datastax.driver.core.SessionManager.prepare(SessionManager.java:109)
at com.bofa.ecom.search.dao.ConfigPropertyDao.readProperties(ConfigPropertyDao.java:87)
我的代码

public class CassandraDao implements Dao, AutoCloseable {

  private final Session session;

  private final Cluster cluster;

  private final String keyspace;

  private final String host;


  /**
   * Initializes and validates the session for a given keyspace.
   * 
   * <p>
   * It is the responsibility of the invoking function to close the session and
   * any other resources associated with the database.
   * 
   * @param host host
   * @param keyspace keyspace to connect to and validate
   * @throws UnavailableHostException if the host can not be reached
   */
  public ConfigPropertyDao(String host, String keyspace) throws DatabaseException {
    this.host = Objects.requireNonNull(host, "Cassandra host can not be null.");
    this.keyspace = Objects.requireNonNull(keyspace, "Cassandra keyspace can not be null.");
    LOGGER.debug(
        String.format("Initializing the cluster and session for host :%s and keyspace:%s",
            host, keyspace));
    if (this.cluster == null) {
      this.cluster = Cluster.builder().addContactPoint(Objects.requireNonNull(this.host)).build();
    }
    this.session = cluster.connect(this.keyspace);
    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug(
          String.format(
              "The session has been initialized for host: %s and keyspace %s", host, keyspace));
    }
  }
}

@Override
public void close() throws Exception {
  //**UPDATE**: Check if not closed. Bad Copy paste on my part.
  if (!this.session.isClosed()) {
    this.session.close(); 
  }

  if (!this.cluster.isClosed()) {
    this.cluter.close();
  }
}
公共类Cassandrado实现Dao,可自动关闭{
非公开最后一届会议;
私有最终集群;
私有最终字符串键空间;
私有最终字符串主机;
/**
*初始化并验证给定密钥空间的会话。
* 
*
*调用函数负责关闭会话并
*与数据库关联的任何其他资源。
* 
*@param主机
*@param keyspace要连接并验证的keyspace
*@如果无法访问主机,则抛出UnavailableHostException
*/
公共ConfigPropertyDao(字符串主机,字符串键空间)引发DatabaseException{
this.host=Objects.requirennull(主机,“Cassandra主机不能为空”);
this.keyspace=Objects.requirennull(keyspace,“Cassandra keyspace不能为null”);
LOGGER.debug(
String.format(“初始化主机%s和密钥空间%s的群集和会话”,
主机,键空间);
if(this.cluster==null){
this.cluster=cluster.builder();
}
this.session=cluster.connect(this.keyspace);
if(LOGGER.isDebugEnabled()){
LOGGER.debug(
字符串格式(
“已为主机%s和键空间%s(主机,键空间))初始化会话”;
}
}
}
@凌驾
public void close()引发异常{
//**更新**:检查是否未关闭。在我的部件上粘贴坏的副本。
如果(!this.session.isClosed()){
this.session.close();
}
如果(!this.cluster.isClosed()){
this.cluter.close();
}
}

我不知道cassandra的情况,但是快速浏览会显示(this.session.isClosed())的逻辑是否正确。如果(!this.session.isClosed()),则不应为。只是确认一下是否有错……你是对的。对我来说,这是一个糟糕的复制粘贴。更新