几乎没有与servlet相关的基本问题 [英] Few basic questions related to servlets

查看:85
本文介绍了几乎没有与servlet相关的基本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Helo专家,


我正在开发我的第一个Web应用程序,如果可能的话,我有一些与servlet有关的基本问题,


我的servlet中有5-6个Java语句和preparedstatements来为不同的表执行大量的MySQL查询,我是否需要为每个查询打开一个新的数据库连接,或者我可以创建一个连接并使用它适用于所有查询?


此外,如果我正在为特定查询使用预备语句和结果集,那么在关闭它们之后,我可以再次将它们重新用于另一个查询相同的代码片段或者更好的是为每个不同的查询创建一个新的statement / preparedstatement对象,并在doGet()/ doPost()方法的末尾关闭所有的结果集和语句等?


我被告知不要将预备声明或声明声明为全局,因为这与多线程有关,因此我在doPost()或d中创建并使用它们oGet()。如果该程序将被多个用户访问,那么不将它们声明为全局的原因是什么?是因为全局副本将被所有或其他东西共享?


在同一程序中打开多个连接并使用多个语句/预备语句会以任何方式影响性能?


为什么关闭destroy()中所有打开的连接是一个好习惯?即使用户关闭了与该servlet链接的浏览器窗口(网页),它们是否仍然保持打开状态?

Helo experts,

I''m working on my first ever web application and I have some basic questions related to servlets if I may,

I have 5-6 Java statements and preparedstatements in my servlet to execute a number of MySQL queries for different tables, do I need to open a new connection to the database for each query or can I just create one connection and use it for all the queries?

Also, if I''m using a preparedstatement and resultset for a particular query, then after closing them both can I again reuse them for another query in the same piece of code or is it better to create a new statement/preparedstatement object for every different query and close all the resultsets and statements etc in the end of the doGet()/doPost() method?

I''ve been told to not declare a preparedstatement or a statement as global since that has something to do with multithreading, hence I create and use them in the doPost() or doGet(). What is the reason behind not declaring them as global if that program is going to be accessed by multiple users? Is it because the global copy will be shared by all or something else?

Does opening multiple connections on a database in the same program and using many statements/preparedstatements affect the performance in any way?

Why is it a good practice to close all the opened connections in destroy()? Will they still remain open even if the user closes the browser window(webpage) to which this servlet is linked?

推荐答案

这些问题的性质可能要求您读一篇文章。即使这样,你也会找到专门讨论这些内容的全书,所以无论我们在这里做出什么样的反应都不会完整。

Naturally the nature of these questions may require you to read a text. Even then you''ll find whole books devoted to these things so whatever responses we are going to give here will not be complete.


我有5个-6我的servlet中的Java语句和preparedstatements为不同的表执行大量的MySQL查询,我是否需要为每个查询打开一个新的数据库连接,或者我可以创建一个连接并将其用于所有查询?
I have 5-6 Java statements and preparedstatements in my servlet to execute a number of MySQL queries for different tables, do I need to open a new connection to the database for each query or can I just create one connection and use it for all the queries?



您不需要为每个语句打开连接。只需使用一个连接

You do not need to open a connection for each statement. Just use one connection


另外,如果我正在为特定查询使用预备语句和结果集,那么在关闭它们之后我可以再次重用它们用于同一段代码中的另一个查询,还是最好为每个不同的查询创建一个新的statement / preparedstatement对象,并在doGet()/ doPost()方法的末尾关闭所有结果集和语句等?
Also, if I''m using a preparedstatement and resultset for a particular query, then after closing them both can I again reuse them for another query in the same piece of code or is it better to create a new statement/preparedstatement object for every different query and close all the resultsets and statements etc in the end of the doGet()/doPost() method?



为每个不同的查询创建一个预准备语句。您不能创建多个结果集。您只能有一个结果集,因此必须重用该结果集。

Create a prepared statement for each different query. You cannot create more than one resultsets. You can only have one resultset so you have to reuse that resultset.


在同一程序中打开多个连接并使用多个语句/ preparedstatements以任何方式影响表现?
Does opening multiple connections on a database in the same program and using many statements/preparedstatements affect the performance in any way?



当多个连接打开时,自然会降低性能。影响的程度取决于所使用的数据库以及某些情况下的操作系统。使用许多语句与在程序中具有更多String声明的效果差不多。它是与数据库的连接以及真正有效的查询。

Naturally perfomance is reduced when multiple connections are open. The extent of the effects depend on the database being used and in some cases the OS. Using many statements has just about as much effect as having more String declarations in a program. It is the connection to the database and the queries that are really of effect.


为什么在销毁时关闭所有打开的连接是一个好习惯()?即使用户关闭与此servlet链接的浏览器窗口(网页),它们仍会保持打开状态吗?
Why is it a good practice to close all the opened connections in destroy()? Will they still remain open even if the user closes the browser window(webpage) to which this servlet is linked?



关闭链接到servlet的页面对servlet没有任何作用,除非页面提交关闭。当应用程序停止或服务器停止时调用Destroy,因此在应用程序退出时而不是在页面退出时关闭连接,因为我们可能有一些其他页面可能仍然打开并且可能想要连接到数据库。如果我们在关闭其中一个页面时关闭了连接,那么我们每次都必须创建另一个连接,效率不高。

Closing a page linked to a servlet does not do anything to the servlet unless the page submits on close. Destroy is called when the application is being stopped or when the server is stopped so the connections are closed on application exit rather than on page exit because we can have some other pages that might still be open and may want to connect to the database. If we had closed the connections on closing one of the pages, then we would have to create another connection everytime which is not efficient.


非常感谢你的详细响应r035198x,它清除了很多我的疑惑。


但有一点,你提到了
Thank you so much for your detailed response r035198x , it clears a lot of my doubts.

One thing though, you mentioned

为每个不同的查询创建一个准备好的语句。您不能创建多个结果集。你只能有一个结果集,所以你必须重用那个结果集..
Create a prepared statement for each different query. You cannot create more than one resultsets. You can only have one resultset so you have to reuse that resultset..



我完全理解这一部分,我发布了几行程序。如果我以正确的方式使用ResultSet,你能告诉我吗?


[请注意,这只是我的代码看起来一般,我无法发布由于某些工作政策原始代码]

I dint quite understand this part, I''m posting a few lines of my program.Can you please tell me if I''m using the ResultSet in the right way?

[Please note that this is just how my code looks in general, I couldn''t post the original code due to some work policies]

展开 | 选择 | Wrap | 行号



非常感谢您的详细回复r035198x,它清除了很多我的怀疑。


但有一件事,你提到了


我完全理解这一部分,我''我发布了几行我的程序。如果我以正确的方式使用ResultSet,请告诉我吗?


[请注意,这就是我的代码看起来如何一般情况下,由于某些工作政策,我无法发布原始代码。

Thank you so much for your detailed response r035198x , it clears a lot of my doubts.

One thing though, you mentioned

I dint quite understand this part, I''m posting a few lines of my program.Can you please tell me if I''m using the ResultSet in the right way?

[Please note that this is just how my code looks in general, I couldn''t post the original code due to some work policies]

展开 | 选择 | Wrap | 行号


这篇关于几乎没有与servlet相关的基本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆