As i am developing the web application in J2EE, i have to be very careful about the database calls.
当我在J2EE中开发Web应用程序时,我必须非常小心数据库调用。
I am storing the images in database(BLOB).
我将图像存储在数据库(BLOB)中。
For accessing those images, i created a servlet file.
为了访问这些图像,我创建了一个servlet文件。
I am passing id to that file as GET
method.
我将id作为GET方法传递给该文件。
For showing the image, i needs to call that servlet file.
为了显示图像,我需要调用该servlet文件。
But as that servlet file making another database call, which i am most concern.
但是因为那个servlet文件正在进行另一个数据库调用,这是我最关心的问题。
For example:
If in that particular jsp file, i wants to show 3 rows, i will make 1 call to show those rows AND another 3 calls for showing those images.
如果在那个特定的jsp文件中,我想要显示3行,我将进行1次调用以显示这些行,另外3次调用以显示这些行。
I have 2 options
我有2个选择
- Create a global variable and access that variable in jsp as well as in servlet.
- Instead of creating the global variable, pass the database object to session variable.
创建一个全局变量并在jsp和servlet中访问该变量。
而不是创建全局变量,将数据库对象传递给会话变量。
But for me both are not good. Please suggest somethings.
但对我来说两个都不好。请建议一些事情。
2 个解决方案
#1
3
If you're concerned about the load imposed to your database, you should probably avoid storing images in it. Instead, store the images on the disk, and store their path in the database.
如果您担心数据库的负载,您应该避免在其中存储图像。而是将图像存储在磁盘上,并将其路径存储在数据库中。
This way, when you need to display three images in a single page, you execute a single query to get the paths of the three images, and you generate an HTML page containing the 3 paths.
这样,当您需要在单个页面中显示三个图像时,执行单个查询以获取三个图像的路径,并生成包含3个路径的HTML页面。
If you have no other choice than storing the images as blobs in the database, and if you have load-tested your application, and showed that getting them from the database caused a significant performance problem, then consider using some application cache (like EHCache), to remove load from the database. You could also get the three images at once from the database and store them in the session, but you would have to find a way to cleanup the session, else the memory of your app would climb very fast.
如果除了将图像作为blob存储在数据库中之外别无选择,并且如果您对应用程序进行了负载测试,并且表明从数据库中获取它们会导致严重的性能问题,那么请考虑使用某些应用程序缓存(如EHCache) ,从数据库中删除负载。您还可以从数据库中一次获取三个图像并将它们存储在会话中,但您必须找到一种方法来清理会话,否则应用程序的内存将非常快速地攀升。
#2
0
Instead of hitting the database so many times. Just write a query where you get all the rows which you need or images which you need. And display All the rows of images using ArrayList This would be a much easier method :)
而不是多次击中数据库。只需编写一个查询,您可以获得所需的所有行或所需的图像。并使用ArrayList显示所有图像行这将是一个更容易的方法:)
#1
3
If you're concerned about the load imposed to your database, you should probably avoid storing images in it. Instead, store the images on the disk, and store their path in the database.
如果您担心数据库的负载,您应该避免在其中存储图像。而是将图像存储在磁盘上,并将其路径存储在数据库中。
This way, when you need to display three images in a single page, you execute a single query to get the paths of the three images, and you generate an HTML page containing the 3 paths.
这样,当您需要在单个页面中显示三个图像时,执行单个查询以获取三个图像的路径,并生成包含3个路径的HTML页面。
If you have no other choice than storing the images as blobs in the database, and if you have load-tested your application, and showed that getting them from the database caused a significant performance problem, then consider using some application cache (like EHCache), to remove load from the database. You could also get the three images at once from the database and store them in the session, but you would have to find a way to cleanup the session, else the memory of your app would climb very fast.
如果除了将图像作为blob存储在数据库中之外别无选择,并且如果您对应用程序进行了负载测试,并且表明从数据库中获取它们会导致严重的性能问题,那么请考虑使用某些应用程序缓存(如EHCache) ,从数据库中删除负载。您还可以从数据库中一次获取三个图像并将它们存储在会话中,但您必须找到一种方法来清理会话,否则应用程序的内存将非常快速地攀升。
#2
0
Instead of hitting the database so many times. Just write a query where you get all the rows which you need or images which you need. And display All the rows of images using ArrayList This would be a much easier method :)
而不是多次击中数据库。只需编写一个查询,您可以获得所需的所有行或所需的图像。并使用ArrayList显示所有图像行这将是一个更容易的方法:)