我正在尝试为我的应用程序清理Cassandra连接资源,并且我的应用程序会在一段时间后挂起。我一直在下面得到这个例外。 我不知道如何解决这个问题。我的堆栈跟踪在下面。
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();
}
}