java如何使用mongodb,如何使用Java建立MongoDB连接?

In my app, MongoDB 3.2.4 runs on a custom port, I want to implement logic where my app will try to reach MongoDB on a custom port and if it fails it will use the default 27018 port.

In order to do that I use the following code:

String mongoClientURI = "mongodb://" + DB_SRV_USR + ":" + DB_SRV_PWD + "@" + DB_URL + ":" + DB_PORT_CUS + "/" + dbName;

MongoClientURI connectionString = new MongoClientURI(mongoClientURI);

// enable SSL connection

MongoClientOptions.builder().sslEnabled(true).build();

if (this.mongoClient == null) {

this.mongoClient = new MongoClient(connectionString);

}

// create database if doesn't exist

MongoDatabase mdb = this.mongoClient.getDatabase(dbName);

try {

this.mongoClient.getAddress();

} catch (com.mongodb.MongoSocketOpenException e) {

System.out.println("Switch to default port");

/*…use default port logic…*/

}

The problem is that this exception is not caught.

Although MongoDB throws the following exception:

com.mongodb.MongoSocketOpenException: Exception opening socket at

com.mongodb.connection.SocketStream.open(SocketStream.java:63) at

com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114)

at

com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128)

at java.lang.Thread.run(Thread.java:745) Caused by:

java.net.ConnectException: Connection refused: connect at

java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) at

java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)

at

java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)

at

java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)

at

java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)

at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at

java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at

java.net.Socket.connect(Socket.java:589) at

com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)

at com.mongodb.connection.SocketStream.open(SocketStream.java:58)

... 3 more

my try-catch expression can't catch this exception.

I tried multiple approaches, such as to catch:

Exception

RuntimeException

MongoSocketOpenException

MongoException

MongoCommandException

none of them doesn't work.

My questions:

How can I check if MongoDB connection is established?

How can catch the exception MongoSocketOpenException?

解决方案

I use this code to check connection:

try {

mongo.getAddress();

} catch (Exception e) {

System.out.println("Database unavailable!");

mongo.close();

return;

}

Not sure here my guess would be that this.mongoClient.getAddress(); does not throw that exception, but I don't really know

EDIT: I initialized it via:

Builder builder = MongoClientOptions.builder().connectTimeout(3000);

MongoClient mongo = new MongoClient(new ServerAddress("192.168.0.1", 3000), builder.build());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值