在Spring Boot Data JPA + Hibernate中,我需要手动关闭连接吗?

huangapple go评论75阅读模式
英文:

Do I need to close connection manually in spring boot data jpa + hibernate

问题

我使用 Spring Boot 开发了一个应用程序,还使用了 Spring Data JPA 和 Hibernate,同时我也使用了 HikariCP 来进行连接池管理。我想知道在每次 CRUD 操作后是否需要手动关闭数据库连接?

应用程序有三层:模型(Model)、仓库(Repository)、服务(Service)和控制器(Controller)。

@Override
public void delete(int id) {
    try {
        notificationRepository.deleteById(id);
    }
    finally {

        // 这段代码无效,仅为解释目的,我需要知道是否需要手动关闭连接,如果是,我该如何操作

        notificationRepository.close();
    }

}
英文:

I've developed an application using spring boot and I also use spring data jpa hibernate I also use hikaricp for connection pooling. I need to to know do I need to manually close the connection after every crud operation ?

there are three layers model , repository, service and controller

 @Override
 public void delete(int id) {
        try {
            notificationRepository.deleteById(id);
        }
        finally {

            //This code not working this is for explanation purpose and I need to know if I need to 
            //manually close connection then how can I do it 
           
            notificationRepository.close();
        }

    }

答案1

得分: 1

很高兴见到一个SLIIT的本科生,对你的问题的回答是关闭连接将会自动处理。如果你在每次操作后都关闭连接,那么你的应用程序性能会严重降低。你唯一需要确保的是在你的业务(Service)层使用@Transactional注解,除此之外,你不需要手动做任何事情。

英文:

Well nice to meet a SLIIT undergraduate , Answer to your question is closing the connection will handle automatically .If you close your connection after every operation then your application performance will be decrease heavily.Only thing you want to ensure is usage of @Transactional annoation in your business(Service) layer,apart from that you don't wan't do anything manually.

huangapple
  • 本文由 发表于 2020年8月27日 15:02:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63610768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定