2016-07-28 2 views
0

나는 간단한 스프링 부팅 응용 프로그램을 가지고 있습니다.스프링 부트 서버가 계속 Postgres 연결 제한을 누름

  • 사용자는
  • 돌아

의 I는 무엇입니까 문제를 해결 (97) 요청을 한 후, 나는를 얻을 수 있다는 것입니다 결과 데이터베이스에서 좀 봐 REST 쿼리

  • 한다 : 같은 그것은 작동 메시지 : 97 의미가 슈퍼 사용자에 대한 3 있도록

    FATAL: remaining connection slots are reserved for non-replication superuser connections 
    

    내 포스트 그레스 서버, 100 연결 제한이 있습니다. 내가 혼란스러워하는 이유는 Spring Boot가 다른 S/O 게시물에 따라야한다고 생각하게되었지만 자동으로 연결을 닫지 않는 이유입니다. 여기

    내 REST 엔드 포인트에 대한 코드입니다 :

    @RestController 
    @RequestMapping(value = "/myEndpoint") 
    public class CommentController { 
    
        @Autowired 
        // Where the business logic is 
        private CommentJdbcService commentJdbcService; 
    
        @Autowired 
        private JdbcTemplate template; 
    
        @ApiOperation(value = "Get a comment by ID") 
        @RequestMapping(value = "/{comment_id}", produces = "application/json", method = RequestMethod.GET) 
        public ResponseEntity getComment(@PathVariable String commentId) { 
         List<Comment> result; 
         try { 
          result = commentJdbcService.get(comment_id, template.getDataSource().getConnection()); 
         } catch (Exception e) { 
          return log(e, HttpStatus.INTERNAL_SERVER_ERROR, slf4jLogger, Error.Levels.ERROR); 
         } 
         return new ResponseEntity<>(result, HttpStatus.OK); 
        } 
    } 
    

    을 그리고 여기 내 비즈니스 로직 코드입니다 :이를 바탕으로

    @Service 
    public class CommentJdbcService { 
        public List<Comment> get(String commentId, Connection connection) throws Exception { 
         String getSql = "SELECT * FROM comment WHERE comment_id=?::UUID"; 
         PreparedStatement statement = connection.prepareStatement(getSql); 
         statement.setString(1, commentId); 
         List<Comment> comments = new ArrayList<>(); 
         ResultSet assetResult = statement.executeQuery(); 
         while (assetResult.next()){ 
          comments.add(build(assetResult, connection)); 
         } 
         return comments; 
        } 
    } 
    

    , 왜 자동으로 닫 부팅 봄하지 않습니다 내 사이? 또한

    @ApiOperation(value = "Get a comment by ID") 
    @RequestMapping(value = "/{comment_id}", produces = "application/json", method = RequestMethod.GET) 
    public ResponseEntity getComment(@PathVariable String commentId) { 
        List<Comment> result; 
        Connection con; 
        try { 
         con = template.getDataSource().getConnection(); 
         result = commentJdbcService.get(comment_id, con); 
        } catch (Exception e) { 
         return log(e, HttpStatus.INTERNAL_SERVER_ERROR, slf4jLogger, Error.Levels.ERROR); 
        } finally { 
         try { 
          if (con != null) 
           con.close(); 
         } catch(SQLException se) { 
          se.printStackTrace(); 
         } 
        } 
        return new ResponseEntity<>(result, HttpStatus.OK); 
    } 
    

    그리고 당신의 준비된 문 및 결과 집합 :

  • 답변

    0

    가장 좋은 방법은 당신이 함께 할 때 당신이 당신의 연결을 닫아야입니다

    @Service 
    public class CommentJdbcService { 
        public List<Comment> get(String commentId, Connection connection) throws Exception { 
         PreparedStatement statement; 
         ResultSet assetResult; 
         try { 
          String getSql = "SELECT * FROM comment WHERE comment_id=?::UUID"; 
          statement = connection.prepareStatement(getSql); 
          statement.setString(1, commentId); 
          List<Comment> comments = new ArrayList<>(); 
          assetResult = statement.executeQuery(); 
          while (assetResult.next()){ 
           comments.add(build(assetResult, connection)); 
          } 
          return comments; 
         } catch (Exception e) { 
          // do exception handling 
         } finally { 
          try { 
           if (statement != null) 
            statement.close(); 
          } catch(SQLException se) { 
           se.printStackTrace(); 
          } 
          try { 
           if (assetResult != null) 
            assetResult.close(); 
          } catch(SQLException se) { 
           se.printStackTrace(); 
          } 
         } 
        } 
    } 
    
    +0

    을 내가 다른 비즈니스 로직 기능을 가지고 가정하면 (수 사용자 의견 등)를 사용하면 해당 기능에 대한 연결이 닫히지 않습니까? –

    +1

    이 함수를 사용하기 전에 con = template.getDataSource(). getConnection();을 호출해야합니다. 네가 그걸 할 때 죄책감을 닫아라. 연결을 설정하고 절대로 닫지 않으면 많은 연결이 끊어지지 않는 것이 일반적입니다. 너가 그 (것)들을 할 때 자원을 닫으 십시요. http://stackoverflow.com/questions/2225221/closing-database-connections-in-java – alpert

    관련 문제