A common trap

Maybe I’ll make this into an interview question one day. Yesterday I was trolling through some code for a little script that ETLs from one database to another. It looked a bit like this:

void quit() {
db1.rollback(); db2.rollback();
db1.disconnect(); db2.disconnect();
void main() {
if (!db1.connect()) exit;
if (!db2.connect()) exit;
if !(db1.commit()) quit();
if !(db2.commit()) quit();
db1.disconnect(); db2.disconnect();

Now, I’ve simplified tremendously here, and there was actually some error handling in getStuff and writeStuff, which made the code look somewhat robust, but what about those two commits? Now, the interesting thing about this was not so much the code, which is wrong, but but rather that I was talking to a colleague who I regard as a highly talented and extremely productive developer, and he didn’t see anything wrong with it. It all makes me wonder how much of the world’s code is rife with similar errors.
So, if you’re ever in an interview with me, get ready for this question.


3 Responses to “A common trap”

  1. Nally Says:

    Are we allowed to hint at the problem in these comments if you want it to be a valid question for future interviews?

  2. renegourley Says:

    Of course you may hint at the problems! The candidate’s ready answer would illustrate that they do their homework. These days, most candidates will claim that they have looked at your website, but couldn’t tell you the first thing about your company. Now back in my day, you had to do serious library work to answer the question, “what do you know about my company?”

  3. ChadL Says:

    Well, I have been in an interview with you, and I never got this question. You did ask some good ones though. 🙂

    On first pass, I see is that if db2.commit fails, then we’ll have a couple problems:

    1) the rollback for db1 will fail

    2) depending on what is going on in ‘writestuff’, we likely end up with a data consistency problem. (state of db1 changes, but db2 does not. Uh ohhh!)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: