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

PreparedStatements seems never get closed #195

Open jilen opened 8 years ago

jilen commented 8 years ago

Which will leads to #194 . As a normal jdbc driver, we must call statment.close after using an prepared statement

mauricio commented 8 years ago

Yeah, I started work for this at https://github.com/mauricio/postgresql-async/tree/closing-prepared-statements but the MySQL piece is giving me trouble, any help is appreciated.

jilen commented 8 years ago

@mauricio I am trying to close it like this. But actually with no confidence whether it is correctly

Narigo commented 7 years ago

@mauricio I've looked into the code a bit, what do you think about a different API for releasing prepared statements? If I understand the code in Connection correctly you want to do this:

val sql = "SELECT something FROM t WHERE id=?"
for {
  res <- connection.sendPreparedStatement(sql, List(1))
  x <- doSomething(res)
  _ <- connection.releasePreparedStatement(sql)
} yield x

I'd propose to let Connection.sendPreparedStatement return a Future[PreparedStatementQueryResult], where PreparedStatementQueryResult is something like this:

class PreparedStatementQueryResult(val rowsAffected: Long, val statusMessage: String, val rows: Option[ResultSet] = None)
  extends QueryResult(rowsAffected, statusMessage, rows) {

  def release(): Future[Boolean] = ???

}

What do you think about that?

User code might look like this:

for {
  res <- connection.sendPreparedStatement("SELECT something FROM t WHERE id=?", List(1))
  x <- doSomething(res)
  _ <- res.release()
} yield x
Narigo commented 7 years ago

I just saw that this issue is more or less a duplicate of #111 @mauricio :)

mauricio commented 7 years ago

Yup, MySQL is still a pain in this :(

oshai commented 5 years ago

I merged the fix in jasync fork: https://github.com/jasync-sql/jasync-sql/issues/82