Best way to manage database connection for a Java servlet

前端 未结 9 497
南方客
南方客 2020-12-02 10:28

What is the best way to manage a database connection in a Java servlet?

Currently, I simply open a connection in the init() function, and then close it

9条回答
  •  庸人自扰
    2020-12-02 10:57

    Pool it.

    Also, if you are doing raw JDBC, you could look into something that helps you manage the Connection, PreparedStatement, etc. Unless you have very tight "lightweightness" requirements, using Spring's JDBC support, for instance, is going to simplify your code a lot- and you are not forced to use any other part of Spring.

    See some examples here:

    http://static.springframework.org/spring/docs/2.5.x/reference/jdbc.html

提交回复
热议问题