简体   繁体   English

在 Spring 引导中处理 Datastax Java 驱动程序 CqlSession

[英]Handling Datastax Java driver CqlSession in Spring Boot

Datastax Java driver documentation says to create a single instance of CqlSession for your Spring Boot application as building a session is a costly operation. Datastax Java 驱动程序文档说,为您的 Spring 引导应用程序创建一个 CqlSession 实例,因为构建 session 是一项昂贵的操作。

I want to manage the CqlSession in an effective way in Spring Boot such that CqlSession instance should be closed and not kept open in order to free underlying resources(TCP connections, thread pools…).我想在 Spring Boot 中以有效的方式管理 CqlSession,以便 CqlSession 实例应该关闭而不是保持打开状态,以释放底层资源(TCP 连接、线程池......)。

Driver documentation URL: https://docs.datastax.com/en/developer/java-driver/4.13/manual/core/驱动程序文档 URL: https://docs.datastax.com/en/developer/java-driver/4.13/manual/core/

Here is my CqlSession Instance class.这是我的 CqlSession 实例 class。 I use the Cqlsession instance to run Cql queries all over my Spring Boot application by injecting it as a Dependency in my @Service and @Repository classes.我使用 Cqlsession 实例在我的 Spring 引导应用程序中运行 Cql 查询,方法是将其作为依赖项注入到我的 @Service 和 @Repository 类中。

Also I never close my Cql session manually but let the Spring handle it using @PreDestory annotation.此外,我从不手动关闭我的 Cql session,而是让 Spring 使用 @PreDestory 注释处理它。 Should session be closed as soon as a cql query is executed or should I allow spring to handle it as mentioned in the below code?是否应该在执行 cql 查询后立即关闭 session 还是应该允许 spring 如下代码中所述处理它? I have realised that doing so, it takes a latency about 1-1.5 seconds for building cqlsession again and queries are ran super slow due to this.我已经意识到这样做,再次构建 cqlsession 需要大约 1-1.5 秒的延迟,因此查询运行速度非常慢。

@Configuration 
public class CqlSessionInstance {
    
        private CqlSession session;
    
        @PostConstruct
        private void initSession() {
            getSession();
        }
    
        public final CqlSession getSession() {
            if(session == null || session.isClosed()) {
                session = CqlSession.builder().build();
            }
            return session;
        }
    
        @PreDestroy
        public final void closeSession() {
            if (session != null && !session.isClosed()) session.close();
        }
    
    }

Let me know if there are better ways to manage a CqlSession in Spring Boot让我知道是否有更好的方法来管理 Spring 引导中的 CqlSession

In my opinion, I suggest that you close the CqlSession yourself, since you know when you are executing as a specific request, and you know when to close it.在我看来,我建议您自己关闭CqlSession ,因为您知道何时作为特定请求执行,并且知道何时关闭它。

If you use the annotation @PreDestroy , you are depending on the Spring Life Cycle Management and as such it is hard to determine "when" exactly your component will be destroyed, and as such your session closed.如果您使用注释@PreDestroy ,您将依赖于 Spring 生命周期管理,因此很难确定“何时”您的组件将被销毁,因此您的 session 已关闭。

I am stuck in same dilemma, when to close session?我陷入了同样的困境,何时关闭 session? at pre destroy or after each request?在销毁前还是在每个请求之后?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM