Di Zou
Di Zou

Reputation: 4609

How do I resolve a MongoDB timeout error when connecting via the Scala Play! framework?

I am connecting to MongoDB while using the Scala Play! framework. I end up getting this timeout error:

! @6j672dke5 - Internal server error, for (GET) [/accounts] ->

play.api.Application$$anon$1: Execution exception[[MongoTimeoutException: Timed out while waiting to connect after 10000 ms]]
at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10-2.2.1.jar:2.2.1]
at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10-2.2.1.jar:2.2.1]
at     play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$12$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:165) [play_2.10-2.2.1.jar:2.2.1]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$12$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:162) [play_2.10-2.2.1.jar:2.2.1]
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:33) [scala-library-2.10.4.jar:na]
at scala.util.Failure$$anonfun$recover$1.apply(Try.scala:185) [scala-library-2.10.4.jar:na]
Caused by: com.mongodb.MongoTimeoutException: Timed out while waiting to connect after 10000 ms
at com.mongodb.BaseCluster.getDescription(BaseCluster.java:131) ~[mongo-java-driver-2.12.3.jar:na]
at com.mongodb.DBTCPConnector.getClusterDescription(DBTCPConnector.java:396) ~[mongo-java-driver-2.12.3.jar:na]
at com.mongodb.DBTCPConnector.getType(DBTCPConnector.java:569) ~[mongo-java-driver-2.12.3.jar:na]
at com.mongodb.DBTCPConnector.isMongosConnection(DBTCPConnector.java:370) ~[mongo-java-driver-2.12.3.jar:na]
at com.mongodb.Mongo.isMongosConnection(Mongo.java:645) ~[mongo-java-driver-2.12.3.jar:na]
at com.mongodb.DBCursor._check(DBCursor.java:454) ~[mongo-java-driver-2.12.3.jar:na]

Here is my Scala code for connecting to the database:

//models.scala
package models.mongodb

//imports

package object mongoContext {
  //context stuff

  val client = MongoClient(current.configuration.getString("mongo.host").toString())
  val database = client(current.configuration.getString("mongo.database").toString())
}

Here is the actual model that is making the connection:

//google.scala
package models.mongodb

//imports

case class Account(
  id: ObjectId = new ObjectId,
  name: String
)

object AccountDAO extends SalatDAO[Account, ObjectId](
  collection = mongoContext.database("accounts")
)

object Account {

  def all(): List[Account] = AccountDAO.find(MongoDBObject.empty).toList
}

Here's the Play! framework MongoDB conf information:

# application.conf
# mongodb connection details
mongo.host="localhost"
mongo.port=27017
mongo.database="advanced"

Mongodb is running on my local machine. I can connect to it by typing mongo at the terminal window. Here's the relevant part of the conf file:

# mongod.conf

# Where to store the data.

# Note: if you run mongodb as a non-root user (recommended) you may
# need to create and set permissions for this directory manually,
# e.g., if the parent directory isn't mutable by the mongodb user.
dbpath=/var/lib/mongodb

#where to log
logpath=/var/log/mongodb/mongod.log

logappend=true

#port = 27017

# Listen to local interface only. Comment out to listen on all interfaces. 
#bind_ip = 127.0.0.1

So what's causing this timeout error and how do I fix it? Thanks!

Upvotes: 2

Views: 5692

Answers (1)

Di Zou
Di Zou

Reputation: 4609

I figured out that I needed to change:

val client = MongoClient(current.configuration.getString("mongo.host").toString())
val database = client(current.configuration.getString("mongo.database").toString())

to:

val client = MongoClient(conf.getString("mongo.host"))
val database = client(conf.getString("mongo.database"))

Upvotes: 1

Related Questions