简体   繁体   中英

How to manage DB connections in a Java Servlet Filter?

I'm writing a traffic auditing servlet filter.

I need to query the database for every HTTP request. There is no connection pool. (I'm adding this filer to the existing application).

I created a JDBC connection object in init() method. And made synchronized methods that doing DB query. In 'doFiler()`, I call these synchronized methods 1~2 times.

Can this be big problem?

-- UPDATE --

Not a heavy loaded server. It runs on single machine. It is serving hundreds KB media files and outgoing bandwidth is 100Mbps, so maximum simultaneous users could be at most 100, I think.

I just wondering.. what's the recommended approach for servlet filters using DB connections.

我建议使用JNDI资源-您只需要注册JDBC资源 ,以后就可以获取连接。

why you want to do this in filter? just get the connection in the servlet and release it after you done with it -- it is enough for your load, and simple enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM