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);
}
そして、あなたのプリペアドステートメントおよび結果セット:
を私は他のビジネスロジック機能を持っていると仮定すると(取得ユーザーのコメントなど)は、これらの関数の接続も閉じませんか? –
その関数の前に、con = template.getDataSource()。getConnection();を呼び出す必要があります。あなたがそれをしたときにあなたの罪を閉めなさい。接続しても決して閉じないと、開いている接続が多数発生することは非常に正常です。あなたがそれらを完了したときにresoursesを閉じます。 http://stackoverflow.com/questions/2225221/closing-database-connections-in-java – alpert