Hi,
I'd like to propose an API for pulling standalone connections out of a
*sql.DB. There are many uses cases for this, such as:
1) Holding session-level locks while executing certain queries
without being in an explicit transaction.
2) Setting parameters before running a query, and then resetting them
before the connection is put back into the pool.
3) Though not part of this proposal, ultimately I'd like a way for
drivers to be able to export functionality not supported by *sql.DB,
such as bulk loading, large objects or notifications.
The suggested API looks like the following:
- *DB.AcquireConn returns a *Conn from the pool. The connection is
taken out of the pool to ensure that nobody else can use it while the
*Conn object is alive, similarly to how transactions currently work.
- The *Conn object has Exec, Query and QueryRow methods for executing
queries.
- The *Conn object has a Begin method for starting a transaction.
- The *Conn object has an Stmt method similar to the *Tx.Stmt method
for making sure a prepared statement has been prepared on the connection.
- The *Conn object has a Prepare method for preparing a statement on
the connection. Such statements are automatically destroyed once the
*Conn is released.
- Concurrent access is not supported and may lead to undefined
behaviour (similarly to how *Tx works). Additionally, if a transaction
is in progress, most *Conn methods return an error until the transaction
has finished (since operating on the connection would not be safe).
- Once the user is done with the connection, *Conn.Release puts it
back into the pool it came from.
I've implemented this API here in case someone wants to have a look:
https://go-review.googlesource.com/#/q/Ic38bc421428ebc95f2034da4d4e8858182fc746b
Any thoughts?
.marko