Disadvantages of using threadlocal + filter to process connection pool

Source: Internet
Author: User

Currently, we recommend using servletfilter and threadlocal to process database connection pools or hibernate session factories. The principle is to use threadlocal to make the entire request process use the same connection instance or session instance!

The advantage is that

1. You do not need to fetch connections from the connection pool multiple times, and the performance is better (I am skeptical, because the consumption of retrieving connections from the connection pool is negligible)
2. the lazy load feature of hibernate can be used.
3.CodeMore brief and nice-looking
4. resources can be reliably released

However, I want to remind people using this method, first listing two disadvantages:

1. because the filter must be configured with URL-pattern, you must configure each page in your system that requires database connection separately (this will cause the web. the XML file is too large). Otherwise, some pages with static content will also occupy database connection resources.

Of course, this is also a solution, that is, to allocate resources on demand. When getconnection or getsession is called, allocate resources again.

2. when some pages require long-running operations, such as network operations and processing of large files, database operations only account for a very small part, in this case, the database connection resource is occupied for a long time, but no operation is performed. If this page has a very large number of concurrent accesses, the connection pool will be used up.

Although there are such shortcomings, we can still solve them through some workarounds! Here, I just want to remind developers not to simply considerModeThe model is important, but it is not something that can be used in all directions. Whether a system is good or not is determined by the user, not by the designer or developer, therefore, when considering a design method, we should compare the advantages and disadvantages and their direct consequences for users, and make a trade-off!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.