Issure url:https://jira.mongodb.org/browse/JAVA-519
We just want to check master in starting our web application.
A full client code example:
public class MongoDataSource {
private static Logger log = LoggerFactory.getLogger(MongoDataSource.class);
private DB db;
private String dbName;
private Mongo mongo;
private String writeConcernName;
private WriteConcern writeConcern;
private boolean slaveOk;
private Set<String> hostNames = new HashSet<String>();
private List<ServerAddress> hosts = new ArrayList<ServerAddress>();
private boolean checkMaster;
public MongoDataSource(String replicaHosts, String dbName, String writeConcernName, boolean slaveOk,boolean checkMaster){
String names[] = replicaHosts.split(",");
for (String name : names)
this.dbName = dbName;
this.writeConcernName = writeConcernName;
this.slaveOk = slaveOk;
this.checkMaster=checkMaster;
}
public void init() throws UnknownHostException, MongoException {
for (String hostName : hostNames)
mongo = new Mongo(hosts);
db = mongo.getDB(dbName);
WriteConcern wc = WriteConcern.valueOf(writeConcernName);
db.setWriteConcern(wc);
if (this.slaveOk)
if (checkMaster)
{ db.getCollectionNames(); }}
public void destroy()
{ mongo.close(); }}
configuration in spring xml file:
<bean id="mongods" class="com.alibaba.cep.ep.dao.impl.MongoDataSource" init-method="init" destroy-method="destroy">
<constructor-arg value="10.20.30.56:11112"></constructor-arg>
<constructor-arg value="db"></constructor-arg>
<constructor-arg value="SAFE"></constructor-arg>
<constructor-arg value="false"></constructor-arg>
<constructor-arg value="true"></constructor-arg>
</bean>
- duplicates
-
JAVA-519 MongoDB's connection is not a effective connection to a MongDB server where new Mongo(serverAddressList)
- Closed