简体   繁体   English

如何在Java Servlet过滤器中管理数据库连接?

[英]How to manage DB connections in a Java Servlet Filter?

I'm writing a traffic auditing servlet filter. 我正在编写一个流量审核Servlet过滤器。

I need to query the database for every HTTP request. 我需要查询每个HTTP请求的数据库。 There is no connection pool. 没有连接池。 (I'm adding this filer to the existing application). (我正在将此文件管理器添加到现有应用程序中)。

I created a JDBC connection object in init() method. 我在init()方法中创建了一个JDBC连接对象。 And made synchronized methods that doing DB query. 并做了做数据库查询的同步方法。 In 'doFiler()`, I call these synchronized methods 1~2 times. 在“ doFiler()”中,我将这些同步方法称为1〜2次。

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. 我认为它正在提供数百KB的媒体文件,传出带宽为100Mbps,因此最大同时用户数最多为100。

I just wondering.. what's the recommended approach for servlet filters using DB connections. 我只是想知道..使用DB连接的servlet过滤器的推荐方法是什么?

我建议使用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. 只需在servlet中获得连接并在完成连接后释放它-它足以满足您的负载并且足够简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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