0

I am super new to Scala development with Play Framework. I am building a project using Scala and Play Framework 2.8x where I am trying to connect to MYSQL database.

I have now spent days trying to find out the problem and online answers could not help me.

However, I tried to follow this thread: Steps needed to use MySQL database with Play framework 2.0

This is my application.conf file database set up

play.db {
  config = "db"
  default = "MyDB" 
} 

#Database config
db {
  MyDB.driver="com.mysql.jdbc.Driver"
  MyDB.url="jdbc:mysql://127.0.0.1:3306/MyDB?characterEncoding=UTF-8&autoReconnect=true&useSSL=false"
  MyDB.username="root"
  MyDB.password="password"

  MyDB.hikaricp {
    connectionTimeout = 60 seconds
    validationTimeout = 5 seconds
    maxLifetime = 60 minutes
  }
}

# db connections = ((physical_core_count * 2) + effective_spindle_count)
fixedConnectionPool = 9
database.dispatcher {
  executor = "thread-pool-executor"
  throughput = 1
  thread-pool-executor {
    fixed-pool-size = ${fixedConnectionPool}
  }
}

This is my model code:

class MyDatabase @Inject() (@NamedDatabase("MyDB") db: Database, databaseExecutionContext: DatabaseExecutionContext) {

  def read(query: String) = {
    Future {
      // get jdbc connection
      try {
        val connection = db.getConnection()
        val statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
        val data = statement.executeQuery("SELECT * FROM User")

        while (data.next()) {
          System.out.print("Data: " + data.getString("username"))
          // System.out.print("")
        }

        // remember to close the connection
        connection.close()

        data
      }
      catch {
        case exception: Exception => System.out.print("Error: " + exception.getMessage())
      }
    }(databaseExecutionContext)
  }

The issue is that I am getting an exception from the catch method with Hikaripool connection time out request

--- (RELOAD) ---

2021-03-24 18:45:07 INFO  play.api.db.HikariCPConnectionPool  Shutting down connection pool.
2021-03-24 18:45:09 INFO  play.api.db.DefaultDBApi  Database [MyDB] initialized
2021-03-24 18:45:09 INFO  play.api.db.HikariCPConnectionPool  Creating Pool for datasource 'MyDB'
2021-03-24 18:45:09 INFO  play.api.http.EnabledFilters  Enabled Filters (see <https://www.playframework.com/documentation/latest/Filters>):

    play.filters.csrf.CSRFFilter
    play.filters.headers.SecurityHeadersFilter
    play.filters.hosts.AllowedHostsFilter

2021-03-24 18:45:09 INFO  play.api.Play  Application started (Dev) (no global state)
Error: HikariPool-15 - Connection is not available, request timed out after 60001ms.

Your help will be much appreciated!

Jjk
  • 1
  • 1

0 Answers0