[JAVA-1060] Mongodb doesn't work for IPv6 address. Created: 20/Dec/13  Updated: 03/Apr/14  Resolved: 22/Dec/13

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: 2.11.2
Fix Version/s: 2.12.0, 3.0.0

Type: Improvement Priority: Major - P3
Reporter: Dinh Dinh Trong Assignee: Craig Wilson
Resolution: Done Votes: 0
Labels: None
Σ Remaining Estimate: Not Specified Remaining Estimate: Not Specified
Σ Time Spent: Not Specified Time Spent: Not Specified
Σ Original Estimate: Not Specified Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by JAVA-331 Support IP6 address in connection uri Closed
Related
is related to JAVA-1093 JMX Exception when registering a conn... Closed
Sub-Tasks:
Key
Summary
Type
Status
Assignee
JAVA-1061 2.12: Mongodb doesn't work IPv6 address Sub-task Closed Craig Wilson  
JAVA-1062 3.0: Mongodb doesn't work IPv6 address. Sub-task Closed Craig Wilson  

 Description   

In mongodb/mongo-java-driver, class com.mongodb.Mongo.java don't support IPv6.
The issue is when I use string host with ipv6 address and mongo options with method:
@Deprecated
public Mongo( String host , MongoOptions options )
throws UnknownHostException {
this( new ServerAddress( host ) , options );
}
-> It cannot parse IPv6 address.
I try to customize class Mongo as:

public Mongo( String host , MongoOptions options )
throws UnknownHostException {
this( new ServerAddress(InetAddress.getByName(host)) , options );
}
-> I can work with IPv6 address directly.

Please give to your comment and how to fix it in this case.

Thanks



 Comments   
Comment by Dinh Dinh Trong [ 23/Dec/13 ]

Ok, Thanks Jeff,
I used mongo connection with Ipv6 address.

Comment by Jeffrey Yemin [ 23/Dec/13 ]

2.12.0-SNAPSHOT should have been published with this fix as part of our CI process on Jenkins. Let us know how it goes.

Comment by Dinh Dinh Trong [ 23/Dec/13 ]

Thanks Jeff,
I will check it,
Can I use mongo-java-driver snapshoot with maven ?
How about version of mongo-java-driver is ?

Comment by Jeffrey Yemin [ 22/Dec/13 ]

Fixed in master and 3.0.x

Comment by Jeffrey Yemin [ 20/Dec/13 ]

Apologies, you're absolutely right. I don't have a workaround for you for IP literals, so we will have to fix the driver. Other drivers parse the string according to RFC 2732, so the Java driver will probably follow suit.

Comment by Dinh Dinh Trong [ 20/Dec/13 ]

Thanks Jeff,
My problem that I am using config by applicationContext.xml in Spring data Mongo. It will call class com.mongodb.Mongo.java in case:
<mongo:mongo id="mongo" host="2001::125" >
<mongo:options connections-per-host="25"
....
/>
</mongo:mongo>
Do you know resolve this problem ?

Beside that, if I use MongoClient(new ServerAddress("2001::125", 27017), ...) then it will call
public ServerAddress( String host , int port ) throws UnknownHostException {
if ( host == null )
host = defaultHost();
host = host.trim();
if ( host.length() == 0 )
host = defaultHost();

int idx = host.indexOf( ":" );
if ( idx > 0 )

{ if ( port != defaultPort() ) throw new IllegalArgumentException( "can't specify port in construct and via host" ); port = Integer.parseInt( host.substring( idx + 1 ) ); host = host.substring( 0 , idx ).trim(); }

_host = host;
_port = port;
}
It wil parse string host as:
-> host=2001
-> port=:15

Comment by Jeffrey Yemin [ 20/Dec/13 ]

OK, I see the problem. It's because ServerAddress called with a single argument checks to see if the port is included, as in "hostname:27018", Can you try this as a workaround:

new MongoClient(new ServerAddress("2001::125", 27017), ...)

Comment by Dinh Dinh Trong [ 20/Dec/13 ]

When I run with IPv6 address 2001::125

Caused by: java.lang.NumberFormatException: For input string: ":125"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at com.mongodb.ServerAddress.<init>(ServerAddress.java:67)
at com.mongodb.ServerAddress.<init>(ServerAddress.java:46)

Comment by Jeffrey Yemin [ 20/Dec/13 ]

Can you include the exception that is being thrown?

Generated at Thu Feb 08 08:53:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.