Share Two little Java secrets you may not know

Source: Internet
Author: User

Introduction

LZ's work has undergone a major change recently, the update speed of post may be back to warm, hope that the apes can continue to pay attention.

LZ recently quit the project manager's work, but did not leave the company, is transferred to the basic research and Development department to do more basic research and development, for the vast number of technical personnel services. This will let LZ have more time to study some technical aspects of things, LZ intends to toss the spring source, look forward to one day can become the spring code contributors.

All right, let's talk about it, let's share two solutions to the small problem today, and maybe you'll come across it later.

dbcp Data source Pit Daddy's Place

A few days ago the system appeared a mistake, more wonderful. The Chinese explanation is "unable to read more data from the socket" because connection reset. First it is obvious that this is a problem with the database connection, because this error is in the SQL execution times of the fault.

From the TCP principle, this error is due to the fact that the connection was shut down for no reason, causing the connection to be reset. After a simple analysis, the suspicion is that the connection has not been used for a long time, but the connection pool is still connected to the application, resulting in the use of a connection that has failed.

So LZ a simple search, found that a lot of people say there is a property can be controlled before the connection to the data source, the first test of usability. So LZ opened the source code, looked at the initial value of this property, the results of a look, found to be true.

1     /** 2 * The indication of whether objects would be a      validated before being3     * borrowed from the poo L.  If the object fails to validate, it'll be a4     * Dropped from the pool, and we'll attempt to Bor Row another. 5      */ 6     protected Boolean true;

This is strange, is already true, that means that the borrowing should have been verified before, for the hair will also appear above the error?

LZ is not convinced, so in the local launch of the application, tracking the connection acquisition process, found that there is no verification. So LZ again the entire data source initialization code to read it again, only to find dbcp the most pit dad. Look at the code below.

 1  //  Can ' t test without a validationquery  2  if  (validationquery = null   3  Settestonborrow (
     
      false 
      );  4  Settestonreturn (false  ); 5  settestwhileidle (false  ); 6 }  

LZ saw when the TM wanted to explode the foul language, this Nima is not the pit father is what? Validationquery default is empty, this is not the equivalent of Testonborrow default is False, but also write a true attribute to mislead me to wait for rookie program ape. If LZ is not idle to look at the initialization of the source code, it is estimated to have been the problem of egg pain, but also face the contempt of business colleagues.

Finally, after setting the Validationquery attribute, this small, incurable illness is solved. The ape friends should also note that when using DBCP, it is best to set a validationquery.

  

abuse of high-end SPRINGMVC

The next story, is the LZ abuse SPRINGMVC story, fortunately LZ on the line before the discovery of this problem, not in front of the partners embarrassing.

With the development of the company, the need for a system docking with partners, so LZ needs to write a service on the Internet. On a It LZ simple introduction of the process of encryption, this time is the subsequent LZ in the unit testing process found problems.

Because the service side of the call will be more frequent, so LZ in the unit test, the special write the test of concurrent access, expect to be able to easily get a concurrency limit. The result is unexpectedly, the concurrency limit is not available, but found in the process of running, the service ended up with some null pointer error.

In fact, NULL pointer error is one of the best solution in Java, as long as you find the location of the stack hint, analysis of which expression may be empty can basically solve the problem. But this time the difference is that after the LZ analysis, found that the result is "impossible to appear null pointer exception."

How could it not have happened? Take a look at the simple code below, which is not real code, but it does.

    User.setname ("Xiaolongzuo");     // After a bunch of user-independent code    User.setsex ("1");

The error is setsex that line of NULL pointers, then from the code, only the user is empty when the null pointer, but assuming that the user is empty, then in the first row should have been reported null pointer error, how can it be reported to the third line? So the conclusion is that "null pointer anomaly cannot occur".

Later LZ carefully analyzed later found that LZ's conclusion is correct, but the premise of the conclusion is that the program in accordance with the code written in the order of execution. Obviously, this is due to concurrency caused, in the final analysis, because the LZ has never used SPRINGMVC, this write service side, because want to release restful style of service, so chose Springmvc, abandoned struts.

The request context for SPRINGMVC is a method, and the request context for struts is action. LZ in the code of the wrong request as a property of action, so when the concurrent access, the parameters of the request may be chaotic, resulting in the first line when the user is not empty, to the third row, because the request was updated by another thread, causing the user to be empty, There is a wonderful empty pointer, more image of the code as follows.

    (user) Request.getattribute ("User")). SetName ("Xiaolongzuo");     // After a bunch of user-independent code    (user) Request.getattribute ("User")). Setsex ("1");

There are two workarounds, the first of which is to remove the request attribute from the action and ensure thread safety, but there are a lot of code that compiles incorrectly. The second is to use threadlocal, which is relatively simple, and there is no compilation error, just a few lines of code to change.

Eventually, the LZ took a second approach, and again, the problem never arose. In order to ensure that the error really solved, LZ also deliberately increased the concurrency, more testing several times, still did not appear the problem.

Summary

This article does not have the big on the technology, more is the LZ own one question record, the ape friends next time see!

Share Two little Java secrets you may not know

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.