13
13
import com .mongodb .MongoClient ;
14
14
import com .mongodb .MongoClientOptions ;
15
15
import com .mongodb .MongoException ;
16
+ import com .mongodb .MongoTimeoutException ;
16
17
17
18
import org .slf4j .Logger ;
18
19
import org .slf4j .LoggerFactory ;
35
36
*/
36
37
public class GetMongoDB {
37
38
39
+ //TODO needs rewrite. 1) Add tests, 2) deprecated methods, 2) replace NFE, 3) auth in constructor, 4) multiple hosts option, 5) logger on/off option 6) retry rewrite for mongo timeouts.
40
+
38
41
private static Logger getLogger () {
39
42
return LoggerFactory .getLogger (GetMongoDB .class );
40
43
}
@@ -96,7 +99,7 @@ public static DB getDB(final String host, final String database)
96
99
* @param host the MongoDB host address.
97
100
* @param database the database to get.
98
101
* @param retryCount - the number of times to retry the MongoDB
99
- * connection, 1 retry / sec.
102
+ * connection, 1 retry / sec, not including the time retrying takes .
100
103
* @param logIntervalCount - how often to log the retries. Logs occur when
101
104
* retries % logIntervalCount = 0.
102
105
* @return the MongoDB database instance.
@@ -120,9 +123,12 @@ public static DB getDB(final String host, final String database,
120
123
try {
121
124
db .getCollectionNames ();
122
125
break ;
123
- } catch (MongoException .Network men ) {
126
+ } catch (MongoException .Network | MongoTimeoutException e ) {
124
127
if (retries >= retryCount ) {
125
- throw (IOException ) men .getCause ();
128
+ if (e instanceof MongoException .Network ) {
129
+ throw (IOException ) e .getCause ();
130
+ }
131
+ throw e ;
126
132
}
127
133
if (retries % logIntervalCount == 0 ) {
128
134
getLogger ().info (
@@ -165,7 +171,7 @@ public static DB getDB(final String host, final String database,
165
171
* @param user the MongoDB user with access to the database.
166
172
* @param pwd the MongoDB user's password.
167
173
* @param retryCount - the number of times to retry the MongoDB
168
- * connection, 1 retry / sec.
174
+ * connection, 1 retry / sec, not including the time retrying takes .
169
175
* @param logIntervalCount - how often to log the retries. Logs occur when
170
176
* retries % logIntervalCount = 0.
171
177
* @return the MongoDB database instance.
@@ -191,9 +197,12 @@ public static DB getDB(final String host, final String database,
191
197
try {
192
198
db .authenticate (user , pwd .toCharArray ());
193
199
break ;
194
- } catch (MongoException .Network men ) {
200
+ } catch (MongoException .Network | MongoTimeoutException e ) {
195
201
if (retries >= retryCount ) {
196
- throw (IOException ) men .getCause ();
202
+ if (e instanceof MongoException .Network ) {
203
+ throw (IOException ) e .getCause ();
204
+ }
205
+ throw e ;
197
206
}
198
207
if (retries % logIntervalCount == 0 ) {
199
208
getLogger ().info (
0 commit comments