Bug #63811 Connector-J calls bind() needlessly
Submitted: 20 Dec 2011 18:03 Modified: 21 Dec 2011 15:23
Reporter: David Leadbeater Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version: OS:Linux
Assigned to: CPU Architecture:Any

[20 Dec 2011 18:03] David Leadbeater
Description:
Connector-J calls bind() even if it is not needed.

See http://aleccolocco.blogspot.com/2008/11/ephemeral-ports-problem-and-solution.html for some further background on why this is bad.

How to repeat:
Hard to reproduce resource exhaustion; but seeing a call to bind() is traceable via strace or similar tools.

Suggested fix:
=== modified file 'src/com/mysql/jdbc/StandardSocketFactory.java'
--- src/com/mysql/jdbc/StandardSocketFactory.java	2010-08-18 18:30:33 +0000
+++ src/com/mysql/jdbc/StandardSocketFactory.java	2011-10-19 09:41:47 +0000
@@ -318,11 +318,11 @@
 										.newInstance(new Object[] {
 												possibleAddresses[i],
 												new Integer(port) });
-								// bind to the local port, null is 'ok', it
-								// means
-								// use the ephemeral port
-								socketBindMethod.invoke(rawSocket,
-										new Object[] { localSockAddr });
+								// bind locally only if we have an address set
+								if (localSockAddr != null) {
+									socketBindMethod.invoke(rawSocket,
+											new Object[] { localSockAddr });
+								}
 
 								connectWithTimeoutMethod.invoke(rawSocket,
 										new Object[] { sockAddr,
[21 Dec 2011 15:24] Mark Matthews
Fixed for 5.1.19, thanks for the bug report!
[22 Mar 2012 20:25] John Russell
Added to changelog for 5.1.19: 

An unnecessary call to bind() during socket operations could limit
scalability on some platforms.