MongoDB Connection issue

Hi and thanks in advance.

When I’m trying the same code in eclipse using groovy it’s working but it’s not working in katalon.

I’m trying to connect katalon with my mongoDB by using this code:

class MongoDB_functions {

public getConnection(){

	MongoClientURI connectionString = new MongoClientURI("mongodb+srv://****:****@********.mongodb.net");
	MongoClient mongoClient = new MongoClient(connectionString);
	MongoDatabase database = mongoClient.getDatabase("database");
	MongoCollection<Document> collection = database.getCollection("case");
	Document myDoc = collection.find().first();
	println(myDoc.toJson());
}

and I’m calling to the connection by:
@Keyword
GetPhoneVerificationCode(String phone_number) {
println('Getting phone verification code for ’ + phone_number);
getConnection();
}

The response that I’m getting is:

2022-04-05 17:34:03.631 ERROR k.k.c.m.CustomKeywordDelegatingMetaClass - :x: Unable to look up SRV record for host ******.mongodb.net
2022-04-05 17:34:03.635 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/test case No.1 FAILED.
Reason:
com.kms.katalon.core.exception.StepErrorException: com.mongodb.MongoConfigurationException: Unable to look up SRV record for host *****.mongodb.net
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.throwError(CustomKeywordDelegatingMetaClass.java:96)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:68)
at test case No.1.run(test case No.1:20)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:442)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:433)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:412)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:404)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:281)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1649169179628.run(TempTestCase1649169179628.groovy:25)
Caused by: com.mongodb.MongoConfigurationException: Unable to look up SRV record for host **.mongodb.net
at com.mongodb.internal.dns.DnsResolver.resolveHostFromSrvRecords(DnsResolver.java:79)
at com.mongodb.ConnectionString.(ConnectionString.java:321)
at com.mongodb.MongoClientURI.(MongoClientURI.java:234)
at com.mongodb.MongoClientURI.(MongoClientURI.java:216)
at common.MongoDB_functionsa.getConnection(MongoDB_functions.groovy:41)
at common.MongoDB_functionsa.GetPhoneVerificationCode(MongoDB_functions.groovy:66)
at common.MongoDB_functionsa.invokeMethod(MongoDB_functions.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:50)
at Script1648040310758.run(Script1648040310758.groovy:20)
… 11 more
Caused by: javax.naming.CommunicationException: DNS error [Root exception is java.net.SocketTimeoutException: Receive timed out]; remaining name '_mongodb._tcp.
.mongodb.net’
at com.sun.jndi.dns.DnsClient.query(DnsClient.java:312)
at com.sun.jndi.dns.Resolver.query(Resolver.java:81)
at com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:434)
at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:235)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:141)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:129)
at com.mongodb.internal.dns.DnsResolver.resolveHostFromSrvRecords(DnsResolver.java:60)
… 19 more
Caused by: java.net.SocketTimeoutException: Receive timed out
at com.sun.jndi.dns.DnsClient.doUdpQuery(DnsClient.java:422)
at com.sun.jndi.dns.DnsClient.query(DnsClient.java:211)
at com.sun.jndi.dns.Resolver.query(Resolver.java:81)
at com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:434)
at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:235)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:141)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:129)
at com.mongodb.internal.dns.DnsResolver.resolveHostFromSrvRecords(DnsResolver.java:60)
at com.mongodb.ConnectionString.(ConnectionString.java:321)
at com.mongodb.MongoClientURI.(MongoClientURI.java:234)
at com.mongodb.MongoClientURI.(MongoClientURI.java:216)
at common.MongoDB_functionsa.getConnection(MongoDB_functions.groovy:41)
at common.MongoDB_functionsa.GetPhoneVerificationCode(MongoDB_functions.groovy:66)
at common.MongoDB_functionsa.invokeMethod(MongoDB_functions.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:50)
at test case No.1.run(test case No.1:20)
… 11 more

2022-04-05 17:34:03.643 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/test case No.1 FAILED.
Reason:
com.mongodb.MongoConfigurationException: Unable to look up SRV record for host express-qa.wpkpu.mongodb.net
at com.mongodb.internal.dns.DnsResolver.resolveHostFromSrvRecords(DnsResolver.java:79)
at com.mongodb.ConnectionString.(ConnectionString.java:321)
at com.mongodb.MongoClientURI.(MongoClientURI.java:234)
at com.mongodb.MongoClientURI.(MongoClientURI.java:216)
at common.MongoDB_functionsa.getConnection(MongoDB_functions.groovy:41)
at common.MongoDB_functionsa.GetPhoneVerificationCode(MongoDB_functions.groovy:66)
at common.MongoDB_functionsa.invokeMethod(MongoDB_functions.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:50)
at test case No.1.run(test case No.1:20)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:442)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:433)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:412)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:404)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:281)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1649169179628.run(TempTestCase1649169179628.groovy:25)
Caused by: javax.naming.CommunicationException: DNS error [Root exception is java.net.SocketTimeoutException: Receive timed out]; remaining name ‘_mongodb._tcp.*******.mongodb.net’
at com.sun.jndi.dns.DnsClient.query(DnsClient.java:312)
at com.sun.jndi.dns.Resolver.query(Resolver.java:81)
at com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:434)
at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:235)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:141)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:129)
at com.mongodb.internal.dns.DnsResolver.resolveHostFromSrvRecords(DnsResolver.java:60)
at com.mongodb.ConnectionString.(ConnectionString.java:321)
at com.mongodb.MongoClientURI.(MongoClientURI.java:234)
at com.mongodb.MongoClientURI.(MongoClientURI.java:216)
at common.MongoDB_functionsa.getConnection(MongoDB_functions.groovy:41)
at common.MongoDB_functionsa.GetPhoneVerificationCode(MongoDB_functions.groovy:66)
at common.MongoDB_functionsa.invokeMethod(MongoDB_functions.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:50)
at Script1648040310758.run(Script1648040310758.groovy:20)
… 11 more
Caused by: java.net.SocketTimeoutException: Receive timed out
at com.sun.jndi.dns.DnsClient.doUdpQuery(DnsClient.java:422)
at com.sun.jndi.dns.DnsClient.query(DnsClient.java:211)
at com.sun.jndi.dns.Resolver.query(Resolver.java:81)
at com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:434)
at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:235)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:141)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:129)
at com.mongodb.internal.dns.DnsResolver.resolveHostFromSrvRecords(DnsResolver.java:60)
at com.mongodb.ConnectionString.(ConnectionString.java:321)
at com.mongodb.MongoClientURI.(MongoClientURI.java:234)
at com.mongodb.MongoClientURI.(MongoClientURI.java:216)
at common.MongoDB_functionsa.getConnection(MongoDB_functions.groovy:41)
at common.MongoDB_functionsa.GetPhoneVerificationCode(MongoDB_functions.groovy:66)
at common.MongoDB_functionsa.invokeMethod(MongoDB_functions.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:50)
at test case No.1.run(test case No.1:20)
… 11 more

2022-04-05 17:34:03.653 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/test case No.1

It’s not working and I have tried all mantioned methods - please help.

Your Java socket is timing out (throws java.net.SocketTimeoutException: Connection timed out) means that it takes too long to get respond from other device and your request expires before getting response.

You can effectively handle it from client side by define a connection timeout and later handle it by using a try/catch/finally block. You can use the connect(SocketAddress endpoint, int timeout) method and set the timeout parameter:

Socket socket = new Socket();
SocketAddress socketAddress = new InetSocketAddress(host, port);
socket.connect(socketAddress, 12000); //12000 are milli seconds

From server side you can use the setSoTimeout(int timeout) method to set a timeout value. The timeout value defines how long the ServerSocket.accept() method will block:

ServerSocket serverSocket = new new ServerSocket(port);
serverSocket.setSoTimeout(12000);

So to avoid this exception in another way, you should keep the connection be alive using the method Socket.setKeepAlive() method although possibly you have not used the method setTimeout() , meaning asking the socket to unlimited block to receive.