2016-07-28 14 views
0

私は単純な春のブートアプリケーションがあります。春のブートサーバーがPostgresの接続制限を打ち続ける

  • ユーザーが
  • 戻る

私は取得しています問題の周りに97の要求を行った後、私が得ることであるが、その結果、データベースに見上げてRESTクエリ

  • を作る:そのようには動作しメッセージ:

    私のPostgresサーバは100の接続制限を持っているので、スーパーユーザでは3で、97の意味があります。私が混乱しているのは、Spring Bootが自動的に接続を閉じることができない理由です。ただし、他のS/O投稿に従うべきだと私は信じています。ここで

    は私のRESTエンドポイントのコードです:これに基づき

    @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; 
        } 
    } 
    

    、なぜ私を閉じて自動的にブート春ません。

    @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); 
        } 
    } 
    

    そしてここでは、私のビジネス・ロジックのコードです接続?また

    @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();を呼び出す必要があります。あなたがそれをしたときにあなたの罪を閉めなさい。接続しても決して閉じないと、開いている接続が多数発生することは非常に正常です。あなたがそれらを完了したときにresoursesを閉じます。 http://stackoverflow.com/questions/2225221/closing-database-connections-in-java – alpert