[JAVA-729]  com.mongodb.ReplicaSetStatus$Node update java.net.SocketException: Connection reset Created: 14/Jan/13  Updated: 12/Jul/13  Resolved: 12/Jul/13

Status: Closed
Project: Java Driver
Component/s: Cluster Management
Affects Version/s: 2.7.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: huangzili Assignee: Unassigned
Resolution: Incomplete Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

windows7



 Description   

I have readed the Issue JAVA-376,but I found the problem seems till exist.
I use the driver in spring.
I must to connect two mongoDB datasource .here is the code:
public class MongodbDataSource implements InitializingBean{
private final static Log log = LogFactory.getLog(MongodbDataSource.class);
/*mongodb链接对象,相当于数据库连接池/
private Mongo mongo = null;
/*mongodb数据库链接对象/
private DB db = null;
/*保存服务端的地址列表/
private List<ServerAddress> addressList = null;
private boolean isSave = true;
/*每台主机连接数,可通过配置文件进行修改/
private int connectionPerHost = 50;
/*链接的超时时间,可通过配置文件进行修改/
private int connectionTimeout = 10000;
/*数据库用户名/
private String userName;
/*数据库密码/
private String password;
/*数据库名称/
private String dbName;

private boolean isTest;

/**

  • 第一次实例化的时候调用这个函数
    */
    @Override
    public void afterPropertiesSet() {
    boolean address = addressList != null && addressList.size() > 0;
    if (address)
    Unknown macro: { try { /**初始化mongo连接*/ initMongo(); } catch (Exception e) { log.error("初始化mongodb链接失败!", e); } }

    else

    { log.error("没有mongodb服务地址!"); }

    }

/**

  • 初始化函数
    */
    private void initMongo() { MongoOptions op = new MongoOptions(); op.safe=isSave; op.connectionsPerHost=connectionPerHost; op.connectTimeout=connectionTimeout; mongo = new Mongo(addressList,op); }

/**

  • 得到数据库链接,这个连接数是根据mongo连接池进行控制的
    */
    public DB getTheDB(){
    if(mongo == null) { log.error("mongo初始化失败!"); return null; }

    db = mongo.getDB(dbName);
    if(db == null)

    { log.error("根据数据库名称获取数据库失败!"); return null; }

    if(!db.isAuthenticated())

    Unknown macro: { boolean auth = db.authenticate(userName, password.toCharArray()); if(!auth){ log.error("数据库用户名和密码验证失败!"); return null; } }

    return db;
    }

/**

  • 关闭所有活动的数据库链接,在容器关闭的时候需要调用这个方法
    */
    public synchronized void destory()
    Unknown macro: { if(mongo != null){ mongo.close(); mongo = null; } }

public void setDbName(String dbName)

{ this.dbName = dbName; }

public void setAddressList(List<ServerAddress> addressList)

{ this.addressList = addressList; }

public void setIsSave(boolean isSave)

{ this.isSave = isSave; }

public void setConnectionPerHost(int connectionPerHost)

{ this.connectionPerHost = connectionPerHost; }

public void setConnectionTimeout(int connectionTimeout)

{ this.connectionTimeout = connectionTimeout; }

public void setUserName(String userName)

{ this.userName = userName; }

public void setPassword(String password)

{ this.password = password; }

}

I config two instance ,here is the config:

<bean id="serverAddress1" class="com.mongodb.ServerAddress">
<constructor-arg index="0" type="java.lang.String" value="10.28.168.38" />
<constructor-arg index="1" type="int" value="5281"/>
</bean>
<bean id="serverAddress2" class="com.mongodb.ServerAddress">
<constructor-arg index="0" type="java.lang.String" value="10.28.168.38" />
<constructor-arg index="1" type="int" value="5281"/>
</bean>
<bean id="serverAddress3" class="com.mongodb.ServerAddress">
<constructor-arg index="0" type="java.lang.String" value="10.28.168.38" />
<constructor-arg index="1" type="int" value="5281"/>
</bean>
<bean id="serverAddress4" class="com.mongodb.ServerAddress">
<constructor-arg index="0" type="java.lang.String" value="10.28.168.38" />
<constructor-arg index="1" type="int" value="5281"/>
</bean>

<bean id="mongodbDataSource" class="MongodbDataSource" destroy-method="destory">
<property name="isSave" value="true" />
<property name="connectionPerHost" value="100" />
<property name="connectionTimeout" value="12000" />
<property name="userName" value="sa"/>
<property name="password" value="sa"/>
<property name="dbName" value="collocate"/>
<property name="addressList">
<list>
<ref bean="serverAddress1"/>
<ref bean="serverAddress2"/>
<ref bean="serverAddress3"/>
<ref bean="serverAddress4"/>
</list>
</property>
</bean>

<!-- collocate end -->
<bean id="serverAddress5" class="com.mongodb.ServerAddress">
<constructor-arg index="0" type="java.lang.String" value="10.12.212.196" />
<constructor-arg index="1" type="int" value="20011"/>
</bean>
<bean id="serverAddress6" class="com.mongodb.ServerAddress">
<constructor-arg index="0" type="java.lang.String" value="10.12.212.196" />
<constructor-arg index="1" type="int" value="20011"/>
</bean>

<bean id="mongodbDataSource1" class="MongodbDataSource" destroy-method="destory">
<property name="isSave" value="true" />
<property name="test" value="false" />
<property name="connectionPerHost" value="1" />
<property name="connectionTimeout" value="12000" />
<property name="userName" value="123"/>
<property name="password" value="123"/>
<property name="dbName" value="sku_colocation"/>
<property name="addressList">
<list>
<ref bean="serverAddress5"/>
<ref bean="serverAddress6"/>
</list>
</property>
</bean>
======================================================================================
There is no problem with the network or authenticate , but I got the Exception :
2013-1-14 11:09:22 com.mongodb.ReplicaSetStatus$Node update
警告: Server seen down: 10.12.212.196:20011
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at org.bson.io.Bits.readFully(Bits.java:35)
at org.bson.io.Bits.readFully(Bits.java:28)
at com.mongodb.Response.<init>(Response.java:39)
at com.mongodb.DBPort.go(DBPort.java:128)
at com.mongodb.DBPort.go(DBPort.java:93)
at com.mongodb.DBPort.findOne(DBPort.java:146)
at com.mongodb.DBPort.runCommand(DBPort.java:157)
at com.mongodb.ReplicaSetStatus$Node.update(ReplicaSetStatus.java:255)
at com.mongodb.ReplicaSetStatus$Node.update(ReplicaSetStatus.java:249)
at com.mongodb.ReplicaSetStatus.ensureMaster(ReplicaSetStatus.java:440)
at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:431)
at com.mongodb.ReplicaSetStatus$Updater.run(ReplicaSetStatus.java:421)



 Comments   
Comment by Jeffrey Yemin [ 12/Jul/13 ]

Are you still experiencing this. It's hard to see how you would get a SocketException and not have some connectivity issue. I'm going to close this, but please re-open if you have any more information.

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