简体   繁体   English

我可以在jsp / servlet或控制台应用程序中打开多少个数据库连接?

[英]How many database connections I can open in jsp/servlet or in console application?

I am developing software For SMS Gateway that: - do many select, insert, update and delete statements like more than one million hit on database every day. 我正在为SMS Gateway开发软件: - 每天都有超过一百万的数据库选择,插入,更新和删除多个语句。 - I have some difficulties in managing resources like DB connection, so I do not know where to close the connection to minimize the number of created connections. - 我在管理数据库连接等资源时遇到一些困难,所以我不知道在哪里关闭连接以最小化创建的连接数。 - I use Driver Manager. - 我使用Driver Manager。 - When I keep the connection unclosed I get memory leak in tomcat. - 当我保持连接未闭合时,我在tomcat中出现内存泄漏。 - In the console app there are threads using DAOs and the have static connection so I do not close it. - 在控制台应用程序中有使用DAO的线程和静态连接,所以我不关闭它。

1) Is it good idea not to close the connection of DB after the query done, and leave it open? 1)在查询完成后不关闭DB的连接并保持打开状态是否是个好主意?

2) I have servlet running to receive SMS on http connection and insert them in DB, and I expect thousands of http connections on this servlet (maybe per minutes), shall I close and create DB connection for every request for the servlet? 2)我有servlet运行在http连接上接收SMS并将它们插入到DB中,我希望这个servlet上有数千个http连接(可能每分钟),我是否应该为每个servlet请求关闭并创建数据库连接?

3) what is the best practice for using DB Connections in this case? 3)在这种情况下使用DB Connections的最佳做法是什么?

Is it good idea not to close the connection of DB after the query done, and leave it open? 在查询完成后不关闭数据库连接并将其保持打开状态是否是个好主意?

It is a good idea to use a connection pool, acquire a connection from it when you need it, and close it to release it back to the pool as soon as possible. 最好使用连接池,在需要时从中获取连接,然后关闭它以尽快将其释放回池中。

I have threads using static connection, so is it possible if I closed the connection the other threads gets exception? 我有使用静态连接的线程,所以如果我关闭连接,其他线程获得异常是否可能?

Definitely. 当然。 This is worst practice. 这是最糟糕的做法。 Don't do it. 不要这样做。 See above. 往上看。

I have servlet running to receive SMS on http connection and insert them in DB, and I expect thousands of http connections on this servlet (maybe per minutes), shall I close and create DB connection for every request for the servlet? 我有servlet运行在http连接上接收SMS并将它们插入到DB中,我希望这个servlet上有数千个http连接(可能每分钟),我是否应关闭并为每个servlet请求创建数据库连接?

Yes, but via a connection pool, see above. 是的,但是通过连接池,请参见上文。

what is the best practice for using DB Connections in this case? 在这种情况下,使用数据库连接的最佳做法是什么?

See above. 往上看。

As for the question in your title, it depends entirely on the database configuration. 至于标题中的问题,它完全取决于数据库配置。

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

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