mauricio / postgresql-async

Async, Netty based, database drivers for PostgreSQL and MySQL written in Scala
Apache License 2.0
1.43k stars 222 forks source link

Await.result in README example? #239

Open fancywriter opened 6 years ago

fancywriter commented 6 years ago

Hi!

I have a few questions about example described in README.md

def main(args: Array[String]) {
    val configuration = URLParser.parse("jdbc:postgresql://localhost:5233/my_database?user=postgres&password=somepassword")
    val connection: Connection = new PostgreSQLConnection(configuration)

    Await.result(connection.connect, 5 seconds)
    val future: Future[QueryResult] = connection.sendQuery("SELECT 0")

    val mapResult: Future[Any] = future.map(queryResult => queryResult.rows match {
      case Some(resultSet) => {
        val row : RowData = resultSet.head
        row(0)
      }
      case None => -1
    }
    )

    val result = Await.result( mapResult, 5 seconds )
    println(result)
    connection.disconnect
  }
  1. Why does the last statement return Future and just ignored? connection.disconnect? Looks like the result is just ignored, and disconnect even not finished before application shutdown. I would add Await.result here, not anywhere else.
  2. Can't we use flatMap instead of two Await.result ?

Something like

val future = for {
  _ <- connection.connect
 queryResult <- connection.sendQuery("SELECT 0")
 answer = extractResultSet(queryResult)
 _ <- connection.disconnect
} yield answer

Await.ready(future,5.seconds)

?

decapo01 commented 6 years ago

I have been very curious about this also.