java.net.SocketException

tip

It's possible you're trying to write to a connection that's already closed. Another cause for this is that you closed the socket with unread data in the socket receive buffer.


rafaelrafael
tip

This might be caused by unmatching versions of SSL. Java starts normally with SSLv2 and your server might not be able to negotiate. You have to force Java to use SSLv3. See this comment: https://goo.gl/hx0YB3


rafaelrafael

You have a different solution? A short tip here would help you and many other users who saw this issue last week.

  • 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) { 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()){ boolean auth = db.authenticate(userName, password.toCharArray()); if(!auth){ log.error("数据库用户名和密码验证失败!"); return null; } } return db; } /** * 关闭所有活动的数据库链接,在容器关闭的时候需要调用这个方法 */ public synchronized void destory(){ 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)
    via by huangzili,
  • mongodb 2.4.8 initial import
    via GitHub by dblado
    ,
  • hive | Apache Help Blog | Page 5
    via by Unknown author,
  • GitHub comment 70#2419677
    via GitHub by hzlinux
    ,
  • rhive.connect() thread error
    via Stack Overflow by defyax
    ,
    • 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)

    Users with the same issue

    Nikolay RybakNikolay Rybak
    1 times, last one,
    mauritiusmauritius
    2 times, last one,
    Unknown visitor
    Unknown visitor1 times, last one,
    Unknown visitor
    Unknown visitor1 times, last one,
    Unknown visitor
    Unknown visitor1 times, last one,
    195 more bugmates