Java.net. SocketException: Connection reset solution,

Source: Internet
Author: User
Tags connection reset

Java.net. SocketException: Connection reset solution,

Since the SEOTcs system updated the SEO Scoring Algorithm on September 24, November, a problem that has plagued me has emerged: java data job tasks, the following errors are often reported during execution:

"2011-12-03 18:00:32 DefaultHttpClient [INFO] I/O exception (java.net. SocketException) caught when processing request: Connection reset by peer: socket write error
2011-12-03 18:00:32 DefaultHttpClient [INFO] Retrying request "...

For this reason, I searched some Chinese and English websites, searched every corner I could find, and found the principle of this situation. This java exception may occur on both the client and server, there are two reasons for this exception:

1. If the Socket at one end is closed (or closed actively, or closed due to abnormal exit), the other end still sends data, the first packet sent causes this exception (Connect reset by peer ).

2. One End exits, but the Connection is not closed when it exits. If the other end reads data from the Connection, this exception (Connection reset) is thrown ). In short, it is caused by read and write operations after the connection is disconnected.

So I simply think that by setting the timeout time of some sockets, we can solve the problem:

However, this is still the case after the settings.

This problem has been plagued for several days and is being considered and compared every day. In order to find out the cause of code, I can't help but think about it. With the same number of keywords, why are there no errors in batch query of ranking data and frequent errors recently? Is our server ip Address blocked by the requested interface website? This is not a very good reason. It must have been caused by the failure to reasonably release the connection somewhere in the program!

Under the guidance of this idea, after several days of continuous fighting and practice, we finally found the essence of the problem, that is, the timer method! This is the case. In the past few days, I have manually triggered some Batch Tasks and found that java's java.net. socketException: The Connection reset error will always be thrown, and the screen flushing is very powerful. I carefully compared the timer code.

 

Then, I suddenly realized, right! There is a problem here. Let me analyze it myself:

The value returned by a function is a critical value. However, in my timer method, if the returned value is a critical value, it will force it to continue to execute that method within 10 seconds, and this method is to obtain a specific data of the source code in a page, each time this method is executed will consume dozens of milliseconds, that is, it is equivalent to establishing a socket connection during this time, but since it always returns the critical value, this method will establish a socket connection continuously within 10 seconds to obtain data, if the execution time of this method is about 80 ms (after testing, the execution time of each such method is about 80 ms), within 10 seconds, 10*1000/80 = 125 socket connections will be established, that is, 12.5 socket connections will be established every second, plus because this is a filter program, multiple critical values will appear consecutively. Therefore, in a few seconds, the number of socket connections to the same website page will soar to hundreds or even thousands, the number of requests waiting for processing is too high:

At the beginning, why does the timer method be used to execute a method multiple times? The reason is to obtain a stable value of data. But now, the negative impact brought by this method is very high, however, after a few days of comprehensive analysis and testing, we finally found the culprit. After the problem was solved, we suddenly lost our mind, you can go to bed with peace of mind...

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.