java.net.SocketException: Connection reset

JIRA | huangzili | 4 years ago
  1. 0

    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)

    JIRA | 4 years ago | huangzili
    java.net.SocketException: Connection reset
  2. 0

    mongodb 2.4.8 initial import

    GitHub | 3 years ago | dblado
    java.net.SocketException: Invalid argument
  3. 0

    I assume that astyanax when calling to context just shutdown not closing the connection

    GitHub | 4 years ago | PavelTCo
    org.apache.thrift.transport.TTransportException: java.net.SocketException: Connection reset
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    hive | Apache Help Blog | Page 5

    filegala.com | 1 year ago
    org.apache.thrift.transport.TTransportException: java.net.SocketException: Connection reset

  1. Andreas Häber 4 times, last 2 months ago
  2. sriharshakiran 1 times, last 7 months ago
14 unregistered visitors
Not finding the right solution?
Take a tour to get the most out of Samebug.

Tired of useless tips?

Automated exception search integrated into your IDE

Root Cause Analysis

  1. java.net.SocketException

    Connection reset

    at java.net.SocketInputStream.read()
  2. Java RT
    BufferedInputStream.read
    1. java.net.SocketInputStream.read(SocketInputStream.java:168)
    2. java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
    3. java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
    4. java.io.BufferedInputStream.read(BufferedInputStream.java:317)
    4 frames
  3. MongoDB Java Driver
    ReplicaSetStatus$Updater.run
    1. org.bson.io.Bits.readFully(Bits.java:35)
    2. org.bson.io.Bits.readFully(Bits.java:28)
    3. com.mongodb.Response.<init>(Response.java:39)
    4. com.mongodb.DBPort.go(DBPort.java:128)
    5. com.mongodb.DBPort.go(DBPort.java:93)
    6. com.mongodb.DBPort.findOne(DBPort.java:146)
    7. com.mongodb.DBPort.runCommand(DBPort.java:157)
    8. com.mongodb.ReplicaSetStatus$Node.update(ReplicaSetStatus.java:255)
    9. com.mongodb.ReplicaSetStatus$Node.update(ReplicaSetStatus.java:249)
    10. com.mongodb.ReplicaSetStatus.ensureMaster(ReplicaSetStatus.java:440)
    11. com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:431)
    12. com.mongodb.ReplicaSetStatus$Updater.run(ReplicaSetStatus.java:421)
    12 frames