Xaprb

Stay curious!

Interesting findings about one of the Go database drivers for MySQL

with 3 comments

Moral of the upcoming story: when your systems rely on someone else’s software, make sure you test it thoroughly to understand how it works.

I found a couple of interesting things about the go-mysql-driver driver for Go. One is that it prepares every query before executing it, and then closes it afterwards, if you’re just using the db.Query() or db.QueryRow() functionality. There is zero benefit to this; Bill Karwin has probably listed it as an antipattern somewhere. I asked for one query, but I got three.

The other is that it doesn’t open the database connection when you call db.Open(). You can call that function and get no error. The first query on the resulting “db” object will actually connect to MySQL. Thus it’s actually kind of like the other major opensource Go/MySQL driver (mymysql), which has an “autorc” interface that will automatically reconnect to the server. In normal usage, go-mysql-driver will reconnect more or less silently if you kill its connection to the server.

How did I discover these things? By tailing the general log, of course — and by running KILL on some connections. Good old-fashioned stuff. My manual version of Chaos Monkey :-)

I try to follow the principle of least surprise with my own software, but it’s good never to assume anything about anyone else’s.

Written by Xaprb

December 28th, 2012 at 6:20 pm

Posted in SQL

3 Responses to 'Interesting findings about one of the Go database drivers for MySQL'

Subscribe to comments with RSS

  1. It turns out that it’s not the driver that’s surprising me, but the Go database/sql package. As this explains,

    http://golang.org/pkg/database/sql/driver/#Execer

    If the driver implements the optional interface, queries can be executed directly; otherwise they are prepared, executed, and then deallocated. I thought about this for a while and I think it’s a really good design. In my case it doesn’t do what I want, but it’s the best all-around design. Of course, I have the option to change the code to implement this interface, which should be quite simple, and then I should see the behavior I assumed would happen.

    Xaprb

    7 Jan 13 at 11:18 am

  2. Hi Xaprb,

    I’m the author of Go-MySQL-Driver.
    What a nice surprise to read about it on my favorite MySQL-Blog! (Though the context might be nicer) :P

    The overhead of queries without parameters is a known issue of the database/sql package. I wrote a while ago about it on golang-nuts.
    I’ll propose a patch with another optional interface for Queries (Just like Execer) soon. Of course it will be implemented in Go-MySQL-Driver ASAP ;)

    It’s always good to hear the thoughts of an expert. So if you notice another possible issue please feel free to share them with me via mail or at GitHub (Go-MySQL-Driver is currently moving to https://github.com/Go-SQL-Driver/MySQL), or at http://groups.google.com/group/golang-nuts if they are related to the database/sql package.

    Otherwise it might take some time until I stumble upon it here on your blog :P

    Julien Schmidt

    7 Jan 13 at 1:35 pm

  3. Thanks Julien! I noticed that you were moving. I’m following the github repo too. Thanks for the driver!

    Xaprb

    7 Jan 13 at 9:29 pm

Leave a Reply