我正在使用cassandra1.2和datastax java client 1.0.3 从datastax使用java客户端的示例我创建了一个类,它将返回会话
public class DataStaxPlugin {
public static final Logger LOGGER = Logger.getLogger(DataStaxPlugin.class.getName());
private static Cluster cluster;
private static Session session = null;
private static String node;
private static String port;
private static DataStaxPlugin instance = null;
protected DataStaxPlugin() {
// Exists only to defeat instantiation.
}
public void connect() {
node = ReadPropertiesFile.getProperty("db.server.name");
port = ReadPropertiesFile.getProperty("db.server.port");
LOGGER.debug("Connecting to DB server: " + node);
LOGGER.debug("port: " + port);
cluster = Cluster.builder().withPort(Integer.parseInt(port))
.addContactPoint(node).build();
Metadata metadata = cluster.getMetadata();
System.out.printf("Connected to cluster: %s\n",
metadata.getClusterName());
for (Host host : metadata.getAllHosts()) {
System.out.printf("Datatacenter: %s; Host: %s; Rack: %s\n",
host.getDatacenter(), host.getAddress(), host.getRack());
}
session = cluster.connect();
}
public void close() {
cluster.shutdown();
}
public static Session getDataStaxSession() {
synchronized (DataStaxPlugin.class) {
try {
if (instance == null) {
instance = new DataStaxPlugin();
instance.connect();
}
return session;
} finally {
}
}
}
}
我使用getDatastaxSession函数来获取会话并在数据库中执行一次插入。 前几个插入工作正常,但有一段时间后我得到nullpointer异常,如下所示:
Exception in thread "Cassandra Java Driver worker-5" java.lang.NullPointerException
at com.datastax.driver.core.Connection$Future.onException(Connection.java:602)
at com.datastax.driver.core.RequestHandler.setFinalException(RequestHandler.java:219)
at com.datastax.driver.core.RequestHandler.sendRequest(RequestHandler.java:103)
at com.datastax.driver.core.RequestHandler$1.run(RequestHandler.java:170)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
由于Session为null / timedout而导致此异常吗?如果我以错误的方式创建或管理会话,请告诉我。或者如果我的代码有任何问题,请告诉我。
答案 0 :(得分:0)
我已经对获取会话的代码进行了更改
public void connect() {
node = ReadPropertiesFile.getProperty("db.server.name");
port = ReadPropertiesFile.getProperty("db.server.port");
LOGGER.debug("Connecting to DB server: " + node);
LOGGER.debug("port: " + port);
cluster = Cluster.builder().withPort(Integer.parseInt(port))
.addContactPoint(node).build();
Metadata metadata = cluster.getMetadata();
System.out.printf("Connected to cluster: %s\n",
metadata.getClusterName());
for ( Host host : metadata.getAllHosts() ) {
System.out.printf("Datatacenter: %s; Host: %s; Rack: %s\n",
host.getDatacenter(), host.getAddress(), host.getRack());
}
}
public void close() {
cluster.shutdown();
}
public static Session getDataStaxSession() {
synchronized (DataStaxPlugin.class) {
try {
if(instance == null)
{
instance = new DataStaxPlugin();
instance.connect();
}
session = cluster.connect();
return session;
} finally {
}
}
}
public static void closeDataStaxSession() {
synchronized (DataStaxPlugin.class) {
try {
if(session != null)
{
session.shutdown();
session = null;
}
} finally {
}
}
}
现在,每次我为每个请求创建一个session = cluster.session,并在完成所有处理后关闭会话。
我认为以前当我正在进行一次会话并尝试再次使用它时,经过一段时间会出现超时,当会话空闲一段时间导致异常 这是实现Datastax java驱动程序会话的正确方法吗? 感谢您的任何意见和反馈