How to manage db connections on server?

前端 未结 3 1490
一个人的身影
一个人的身影 2020-11-30 13:51

I have a severe problem with my database connection in my web application. Since I use a single database connection for the whole application from singleton Database class,

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 14:14

    Singleton should be the JNDI pool connection itself; Database class with getConnection(), query methods et al should NOT be singleton, but can be static if you prefer.

    In this way the pool exists indefinitely, available to all users, while query blocks use dataSource.getConnection() to draw a connection from the pool; exec the query, and then close statement, result set, and connection (to return it to the pool).

    Also, JNDI lookup is quite expensive, so it makes sense to use a singleton in this case.

提交回复
热议问题