Com. MySQL. JDBC. Exceptions. jdbc4.communicationsexception: communications link failure, caused byno B

Source: Internet
Author: User

The following error occurs when data is inserted into MySQL using Java (tens of millions of records, which takes a long time:

Com. MySQL. JDBC. Exceptions. jdbc4.communicationsexception: communications link failure

Last packet sent to the server was 0 MS ago.


At sun. Reflect. generatedconstructoraccessor10.newinstance (unknown source)
At sun. Reflect. delegatingconstruct%cessorimpl. newinstance (delegatingconstruct%cessorimpl. Java: 27)
At java. Lang. Reflect. constructor. newinstance (constructor. Java: 513)
At com. MySQL. JDBC. util. handlenewinstance (util. Java: 406)
At com. MySQL. JDBC. sqlerror. createcommunicationsexception (sqlerror. Java: 1074)
At com. MySQL. JDBC. connectionimpl. createnewio (connectionimpl. Java: 2103)
At com. MySQL. JDBC. connectionimpl. <init> (connectionimpl. Java: 718)
At com. MySQL. JDBC. jdbc4connection. <init> (jdbc4connection. Java: 46)
At sun. Reflect. generatedconstructoraccessor8.newinstance (unknown source)
At sun. Reflect. delegatingconstruct%cessorimpl. newinstance (delegatingconstruct%cessorimpl. Java: 27)
At java. Lang. Reflect. constructor. newinstance (constructor. Java: 513)
At com. MySQL. JDBC. util. handlenewinstance (util. Java: 406)
At com. MySQL. JDBC. connectionimpl. getinstance (connectionimpl. Java: 302)
At com. MySQL. JDBC. nonregisteringdriver. Connect (nonregisteringdriver. Java: 282)
At java. SQL. drivermanager. getconnection (drivermanager. Java: 582)
At java. SQL. drivermanager. getconnection (drivermanager. Java: 185)
At decompress. decompressview. jbutton2actionreceivmed (decompressview. Java: 734)
At decompress. decompressview. Access $1400 (decompressview. Java: 43)
At decompress. decompressview $7. actionreceivmed (decompressview. Java: 416)
At javax. Swing. abstractbutton. fireactionshortmed (abstractbutton. Java: 1995)
At javax. Swing. Sort actbutton $ handler. actionshortmed (export actbutton. Java: 2318)
At javax. Swing. defaultbuttonmodel. fireactionreceivmed (defaultbuttonmodel. Java: 387)
At javax. Swing. defaultbuttonmodel. setpressed (defaultbuttonmodel. Java: 242)
At javax. Swing. plaf. Basic. basicbuttonlistener. mousereleased (basicbuttonlistener. Java: 236)
At java. AWT. component. processmouseevent (component. Java: 6263)
At javax. Swing. jcomponent. processmouseevent (jcomponent. Java: 3267)
At java. AWT. component. processevent (component. Java: 6028)
At java. AWT. Container. processevent (container. Java: 2041)
At java. AWT. component. dispatcheventimpl (component. Java: 4630)
At java. AWT. Container. dispatcheventimpl (container. Java: 2099)
At java. AWT. component. dispatchevent (component. Java: 4460)
At java. AWT. lightweightdispatcher. retargetmouseevent (container. Java: 4574)
At java. AWT. lightweightdispatcher. processmouseevent (container. Java: 4238)
At java. AWT. lightweightdispatcher. dispatchevent (container. Java: 4168)
At java. AWT. Container. dispatcheventimpl (container. Java: 2085)
At java. AWT. Window. dispatcheventimpl (window. Java: 2478)
At java. AWT. component. dispatchevent (component. Java: 4460)
At java. AWT. eventqueue. dispatchevent (eventqueue. Java: 599)
At java. AWT. eventdispatchthread. pumponeeventforfilters (eventdispatchthread. Java: 269)
At java. AWT. eventdispatchthread. pumpeventsforfilter (eventdispatchthread. Java: 184)
At java. AWT. eventdispatchthread. pumpeventsforhierarchy (eventdispatchthread. Java: 174)
At java. AWT. eventdispatchthread. pumpevents (eventdispatchthread. Java: 169)
At java. AWT. eventdispatchthread. pumpevents (eventdispatchthread. Java: 161)
At java. AWT. eventdispatchthread. Run (eventdispatchthread. Java: 122)
Caused by: java.net. socketexception: no buffer space available (maximum connections reached ?) : Connect

Judging from the red text,

It should be the maximum number of connections. I have been searching online for a long time and have the same symptoms, but the problem is caused by timeout, and their solutions fail to solve my problem.

I think I should start with solving the maximum number of links. Finally, my code is modified as follows to solve the problem:

String url = "JDBC: mysql: // localhost/weblog ";
String user = "root ";
String Password = "";
Class. forname ("com. MySQL. JDBC. Driver ");
Connection conn = drivermanager. getconnection (URL, user, password );
Statement STM = conn. createstatement ();

While (line! = NULL)
{

.......
String insql = "insert into Log Values (null, '" + ipaddr + "', null )";
Try
{
Cmd.exe cute (insql );
}
Catch (sqlexception E)
{
E. printstacktrace ();
}
} // End while
STM. Close ();
Conn. Close ();
}
The original cause is that

Connection conn = drivemanager. getconnection (URL, user, password );

Statement STM = conn. createstatement ();

Written in the while loop, and no

STM. Close ()

Conn. Close. Of course there will be problems.

Later, I added two sentences to close the connection in the WHILE LOOP, but the same problem still exists.

Later, the conn and STM statements and the closed statements are all placed outside the while clause.

 

The problem with the same symptoms on the internet is caused by timeout. The solution is as follows:

Http://blog.csdn.net/leizhendong/archive/2007/04/16/1566484.aspx

Http://www.wangzhongyuan.com/archives/610.html

Http://www.svn8.com/mysql/200906036013.html

 

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.