Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

leemz avatar image
leemz asked Erick Ramirez answered

是否需要显示调用CqlSession的close()方法

我正在使用datastax-java的驱动操作cassandra数据库,我看datastax驱动文档写创建CqlSession代价很高,一个集群共用一个即可,所以我在程序中使用工具类来创建单例的CqlSession对象,每次通过session执行execute方法的时候,我都会获取这个单例session,当execute方法执行完毕会调用session.close()关闭链接。但是我发现当我第一个操作执行完毕,调用了close()方法后,后续的相关方法通过工具类获取的单例session对象是关闭状态:调用session.isClose()返回true,导致程序报错:java.lang.IllegalStateException: Session is closed

所以我想问下,是否每次使用Cqlsession完毕后,无需手工调用close()方法,但是我在datastax驱动文档上看到示例代码有提示:您应该每次使用完毕后显示调用close()方法。所以我到底该如何做


我使用的cassandra版本为3.11.11,datastax java驱动版本为4.11.0,我也查找了一些资料,按照我现在的理解,如果我在一个springboot项目中使用驱动,是不是我无需手工管理CqlSession的关闭,只需要监听一下web容器的销毁事件,在容器停止时调用一次关闭即可。


期待您的答复。十分感谢。


希望能明确告知客户端的代码是否需要显示调用CqlSession.close()方法。

下面是我的测试代码截图:

1.png2.png3.png


java driver
1.png (67.3 KiB)
2.png (79.5 KiB)
3.png (136.2 KiB)
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Erick Ramirez avatar image
Erick Ramirez answered

Just letting you know that I'm looking into this and will provide you an answer soon. Cheers!

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.