Where does that Python DB handle go?

UPDATE: Well, that didn’t take long. My solution works, but there’s a better way. Create the connection in main(), and create separate *cursors* for each Host. Cursors are cheap, and you reuse the connection. Thanks to Brend on #python (irc.freenode.net) for the enlightenment.

Python has been a wonderful language to get to know so far. However, one thing I didn’t really miss about Java and C++ were the decisions that are kind of forced upon you when you have various objects working together in a program, controlled by code in a “main” function. Here’s one decision I was faced with that took more thinking than I’d like to admit to make.

I have a class, we’ll call it “Host”. Of course, there are also methods for that class, like Host.record_name() and Host.update_mac() and the like. These methods are just wrappers around some SQL.

I also have, of course, a “main” function, which is where we create instances of Host and call the methods we need on the objects.

The question now is, where exactly should we create the handle to the database? There would appear to be 4 choices (without getting overly absurd):

  1. In main() and then pass the handle to the class’s ‘init()’
  2. In main(), but pass the handle to each method of any object that needs it.
  3. In Host.init(), so methods can refer to it without creating it themselves.
  4. Inside the individual methods themselves.

I chose option #3. The only part that bothers me about it is that a new “Host” is created, and then destroyed, for each of a couple thousand lines of a file. Hence, there is the overhead of setting up and tearing down a database connection a couple thousand times every time this script is run. I’m not sure (yet) what the cost of this will be.

Certainly, the cost will be less than if I did it for each *method* that was called, so that insures that option 4 is probably not the right way to go.

The semantics involved in creating the handle in main() and passing it to Host’s init seemed less than straightforward to me. It seemed there was the potential for main to pass in a copy of the handle at object instantiation time, have the object be destroyed when it’s done doing its work, taking the connection with it, and then main still thinks it’s a useful handle and tries to pass it to the next object that needs to be created. If that’s not the case, then what is the status of this thing that main created? What will happen if main tries to pass it to another object?

Of course, this problem would also exist if we passed it to each method that needed it. It would also be more overhead, and it would seem needlessly draconian, especially since *every* method of the object in question would need it.

If I’ve misunderstood something, or have made a poor choice, feel free to clarify/flame/enlighten me in the comments :)

Technorati Tags: , , , , , , ,

Social Bookmarks:

  • http://ru-linux-geek.livejournal.com rrenaud

    I think (3) is the wrong decision from a software engineering/test driven design point of view. How can you write a unittest for your Host class? Presumably, inside Host.init() it knows how to connect to the in production database. You don’t want a unittest run to have any real world side effects. If you pass in the handle to your Host class, you could presumably replace the database connection with a mock object (or maybe an SQL-lite handle for a database created on the fly for the unittest) so that you can test your class side effect free.

    Google for test driven design and mock objects if you want the gory details.

  • http://m0j0.wordpress.com/ m0j0

    rrenaud – you’re absolutely right. This is another aspect of the application that I had not considered (in addition to Brend’s input, which is in the UPDATE above). Thanks a lot for pointing this out.