如何配置Mongo max thread?

时间:2014-11-03 15:46:26

标签: spring-data-mongodb

我把Mongo注入了春天。 一切都很好。但是当我尝试使用太多线程来保持数据时,我有这样的错误:

nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: Too many threads are already waiting for a connection. Max number of threads (maxWaitQueueSize) of 50 has been exceeded.; nested exception is com.mongodb.MongoWaitQueueFullException: Too many threads are already waiting for a connection. Max number of threads (maxWaitQueueSize) of 50 has been exceeded.] with root cause
com.mongodb.MongoWaitQueueFullException: Too many threads are already waiting for a connection. Max number of threads (maxWaitQueueSize) of 50 has been exceeded.
    at com.mongodb.PooledConnectionProvider.get(PooledConnectionProvider.java:70)

我可以在哪里增加此设置?

我只有这个配置:

<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
    <property name="host" value="X.X.X.X" />
    <property name="port" value="27017"></property>
</bean>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongo" ref="mongo" />
    <constructor-arg name="databaseName" value="test" />
</bean>

我在春天的日志服务:

@Service
@Transactional
public class LogService {

    @Autowired  
    MongoOperations mongoOperations;  

    public void saveTransactionLog(Log l) {
        mongoOperations.save(l);
    }
}

3 个答案:

答案 0 :(得分:1)

 <bean id="mongo" class="com.mongodb.Mongo">
      <constructor-arg name="addr" ref="address" />
      <constructor-arg name="options" ref="options" />
   </bean>

   <bean id="options" class="com.mongodb.MongoOptions">
      <property name="connectionsPerHost" value="${mongo.db.pool.size}"/>
      <property name="maxWaitTime" value="${mongo.db.pool.maxwait}"/>
   </bean>

   <bean id="address" class="com.mongodb.ServerAddress">
      <constructor-arg name="host" value="${mongo.db.host}" />
      <constructor-arg name="port" value="${mongo.db.port}" />
   </bean>

   <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
      <constructor-arg ref="mongo" />
      <constructor-arg name="databaseName" value="${mongo.db.name}" />
   </bean>

答案 1 :(得分:0)

查看threads-allowed-to-block-for-connection-multiplier<property name="threads-allowed-to-block-for-connection-multiplier" value="xx" /> 属性下的<{p}>

  

可以阻止的线程数的connectionsPerHost的乘数。默认值为5.如果connectionsPerHost为10,且threadsAllowedToBlockForConnectionMultiplier为5,那么50个线程可以阻止更多,并且将抛出异常。

所以我觉得你可以做点什么

{{1}}

答案 2 :(得分:0)

添加方法如何设置属性名称=&#34; connectionsPerHost&#34;在java配置

public Mongo mongo() throws Exception {
    return new MongoClient("127.0.0.1",MongoClientOptions.builder()
                                                         .connectionsPerHost(250)
                                                         .build());