-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: database/sql: allow access to raw driver.Conn #29835
Comments
That's an interesting idea. Ill need to think about it. |
ping @kardianos |
Thanks for the ping.
But there are some drivers where this could be a useful escape hatch: The various features that may be aided in this are:
One reason to having this escape hatch is it allows a unified connection pool. However, it also allows users to shoot themselves in the foot easier and may be a source of reported "bugs" in the future if accepted. I think one option SQLite has is to put these management options on the a database/sql#Connector type. I'm declining this proposal for now. If other driver authors come forward with needs that must be put on connection as driver specific, we can re-open this proposal for consideration. |
@kardianos I don't entirely understand your counter-proposal to use "database/sql#Connector". Are you referring to the existing driver.Connector type, or a hypothetical new sql.Connector type? Assuming the former, I don't see how that could actually work, since my client code cannot take the connection lock that the standard library uses. Consequently, such code cannot be used in a thread-safe manner without me needing to add even more locks to the picture. "One reason to having this escape hatch is it allows a unified connection pool." Keep in mind that the database/sql package is useful beyond just connection pooling. It also makes the various driver.Conn methods much easier to use - think of iterating through a result set for example. "However, it also allows users to shoot themselves in the foot easier and may be a source of reported "bugs" in the future if accepted." That's fair, but I'd argue the same could be said of |
Hi @rittneje Are you using this driver for sqlite? https://godoc.org/github.com/mattn/go-sqlite3 Let's look at the methods on the Conn:
I admit that SQLite has a weak concept of "database" vs "connection". Many users of SQLite limit the connection pool to a single connection. If we took your above approach, then an online backup command would lockout all other queries until it completed. Other functions, such as the "RegisterX" functions should probably be called prior to any query is called.
driver.Connector is an interface that is provided to sql.OpenDB. Here is an example of a Connector that currently works that provides a richer interface to the database: https://godoc.org/github.com/denisenkom/go-mssqldb#Connector . Most configuration in this instance are just properties. But in SQLite's case the connector could also contain various methods that are currently located on the Conn. I don't think the mutex issue you raised would actually be an issue. |
@kardianos I believe you are mistaken about how SQLite works. There actually is a strong distinction between a connection (or context) and the actual database (file). Like any other DBMS, the connection allows you to interact with the database in various ways. There's the typical select/insert/update/delete operations, as well as some more "meta" operations, like adding/removing users, etc. Going through a few of your examples:
Indeed, in the specific case of backup, holding the lock for the whole thing is unnecessary (but there are ways of dealing with that). However, for most of the other "special" SQLite methods, this locking is necessary, especially because the contract on driver.Conn is that it won't be used by multiple goroutines at once.
I agree that in the common case, the intent is to register such things at the pool level, so having this be the job of the |
I'm re-opening this issue. Upon further inquery, an Oracle SQL driver maintainer would also be interested in such an interface as well. I think I would call the function |
Upon further reflection, I would want to avoid locking in a specific interface such as driver.Conn to this method, as that would be difficult to satisfy if we were to release a
Also, I don't expect or want driver.Conn methods to be called, we would expect someone to type assert this to a specific driver instance anyway for specific functionality. The other way I could see spelling this type of function would be as follows:
|
sounds good. Let's do that unless anyone objects. Will leave open for another week. |
Looks like no one objects. Accepted. |
I'll implement within a few days. |
Change https://golang.org/cl/174182 mentions this issue: |
It would be really nice if the new
sql.Conn
type had a method to access the underlyingdriver.Conn
, similar to hownet.TCPConn
has theSyscallConn
method. This would allow access to custom driver methods that don't map to the standarddriver.Conn
interface. For example, the mattn driver for SQLite exposes aBackup
method on its*SQLiteConn
object, but you cannot really call it once you are in the database/sql world.Specifically, I propose adding a new method to
sql.Conn
as follows:The
driver.Conn
provided to the callback must only be used within the callback. (Just like the fd cannot be used outside the callback forsyscall.RawConn
.) Until the callback function returns, that particulardriver.Conn
is "in use" and cannot be used concurrently by any other goroutine.An equivalent method could also be added to
sql.DB
itself, but that doesn't seem necessary.Sample use case:
The text was updated successfully, but these errors were encountered: