我SpringConfig.xml說:MongoDB的連接不是在春季啓動封閉得到
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.10.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<mongo:mongo-client id="mongo" host="127.0.0.1" port="27017" >
<mongo:client-options
connections-per-host="8"
threads-allowed-to-block-for-connection-multiplier="4"
connect-timeout="1000"
max-wait-time="1500"
socket-keep-alive="true"
socket-timeout="1500"
/>
</mongo:mongo-client>
<mongo:db-factory dbname="test" mongo-ref="mongo" />
<bean id="mappingContext"
class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
<bean id="defaultMongoTypeMapper"
class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
<constructor-arg name="typeKey"><null/></constructor-arg>
</bean>
<bean id="mappingMongoConverter"
class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mappingContext" ref="mappingContext" />
<property name="typeMapper" ref="defaultMongoTypeMapper" />
</bean>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
</bean>
</beans>
我在我的服務的構造函數只有一次調用此:
ApplicationContext ctx;
public DoctorService() {
ctx = new GenericXmlApplicationContext("SpringConfig.xml");
}
,然後使用ctx like:
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
Query searchUserQuery = new Query(Criteria.where("_id").is(new ObjectId(userId)));
Keys k = new Keys();
Doctor doctor = mongoOperation.findOne(searchUserQuery, Doctor.class);
k.setSessionId(doctor.getSessionid());
k.setToken(doctor.getToken());
List<Keys> keys = new ArrayList<Keys>();
keys.add(k);
return keys;
但是,即使在超時期限之後,連接仍未關閉。
有什麼想法?
感謝
你的期望是什麼?你想爲每個查詢建立一個新的連接嗎? – Veeram
@Veeram,當我運行db.serverStatus()。連接時,我獲得當前鍵下增加的連接,比發送請求之前 – Satya