caused by:java.net.BindException:Address already in Use:connect solution

Source: Internet
Author: User

Let's talk about the specifics of my code first. My code is to collect the shares of 2,400 Chinese companies every hour. Then the results of the collection into a database, each acquisition of a company will be inserted directly into the table, so there will be many connections, the code is run under Tomcat, in the native test, Error caused by:java.net.BindException:Address already in Use:connect each time you insert four hundred or five hundred records in the table, as shown in the title. Of course, I didn't specifically get a connection pool in my code, but I wrote the fetch database connection object in a method, as in the method
Conntection conn = null;
.
.
conn = Dbutil.getconntection ();
So, each time you insert a table, the original database connection object reference is empty. And then create a new database connection object, so I think in my function should be satisfied with the demand, but did not think of processing hundreds of when the error, and then stopped for a few minutes and automatically ran.

--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------


Due to the usual connection MySQL do data insertion will appear caused by:java.net.BindException:Address already in use:connect, many times not because there are other programs to occupy the port, But because the program in a short period of time to create the connection data too much, a short time new socket operation too much and socket.close () operation does not immediately release the bound port but set the port to time_wait state over time (the default 240s) before release (with Netstat- NA can see) the last System resource exhaustion (Windows is depleted of pool of ephemeral ports in this interval between 1024-5000). So we can modify the time-wait to increase the number of connections and solve the problem.
How to modify the Time_wait timeout to increase the number of connections. This setting defines the length of time to keep the time_wait state before closing the connection. The default value is 240, which limits the maximum number of connections to approximately 200/sec on a busy server. Reducing this value can increase the limit of the maximum number of connections.


The specific steps are as follows:




Open Registry Editor and locate the items shown in the following table.

Set the following table to create or modify an existing DWORD value called "TcpTimedWaitDelay".
Quit Registry Editor, restart, or log off Windows for the changes to take effect.
Set up:
Item (System): [Hkey_local_machine/system/currectcontrolset/services/tcpip/parameters]
Right margin new key name: TcpTimedWaitDelay
Type: REG_DWORD (DWORD value)


Value: 30-300 seconds (decimal)


--------------------------------------------------------------------------------------------------------------- --------------------------------------


After I modified the registry in accordance with the above method, on the machine rerun code, no more error

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.