Sweat the Small Stuff



  • In 2009, a peer wrote a section of code that used db connections from db connection pools managed separately by Spring and Hibernate. He wrapped it in transactions using both of the respective transaction managers. Then, just to make it interesting, he built up the prepared statements in different modules, across different threads, by passing around handles to connections, statements, etc.

    He prefaced this one particularly funky section of code with a comment:

         Do not change or alter this section of code in any way if you value your sanity.

    The block was about 800 lines of single character variable names referencing both streams of connections and statements. The file was unmodified for the three years since.

    About two weeks ago, this same person made a change to that very section of code and appeared to have broken something. Since he couldn't figure out what he did wrong, he rolled back what he changed. Sadly, the awakened demon persisted.

    The interleaved connection pools, prepared statements, transactions, different transaction managers and threading all served as potential places to possibly have something go wrong. Is it a poorly placed/missing synchronized block? A race condition? Are we passing the wrong connection? The wrong prepared statement? Is a counter off? Are Spring and Hibernate playing Hatfields and McCoys?

    Basically, in pseudo code, spread across threads, functions and classes (unrelated junk omitted):

       Connection              h = ...;      // hibernate
       Connection              s = ...;      // spring
       PreparedStatement       ph;           // hibernate
       PreparedStatement       ps;           // spring
       boolean                 b = false;    // complex condition based upon content of data
       while (more elements in some list) {
         Connection             c;
         PreparedStatement p;
         if (b) {
            c = h;
            p = ph;
         } else {
            c = s;
            p = ps;
         }
         // look up some oracle-type on connection: c
         // use it to construct a type-arg from the next element of the list
         // set the argument on p
         b |= !(some condition based upon the value of the current item in the list);
      }
    

    Do you see it? On the last line: it's ORing in addition to assigning.

    It turns out that all of the data, until now, has ALWAYS worked out such that the condition was always true, so it always did:

      b |= false;
    

    As such, the parameter always got set on the correct prepared statement.

    Now, with the business gaining new customers, we have new data, and the condition is sometimes false. Doing:

      b |= true;
    

    just once leaves the value true from that point forward. Depending upon the order of the items in the list, this puts some of the parameters on the wrong prepared statement. Depending upon the order in which the (unsorted) data is returned from the DB, even multiple runs of the same data give random results. It wreaks of a threading issue. Sadly, everything is unformatted strings, so it didn't crash. The DB accepted the data because a string (no format) is a string, as long as it fits.

    I wasted four days proving that the different transaction managers, transactions, connection pools, prepared statements being populated across threads and modules all had nothing to do with it. As I kept backing further up the tree from where this guy claimed the problem was located, I began to discover the above logical loop spread out all over the place.

    It's object ENcapsulation people; not DEcapsulation!

    Why can't folks ever just go with the flow? Why does everyone need to use the one tool THEY think is better than all the others? If a system is using Spring, stick with it. If a system is using Hibernate, stick with it. OR get permission and then rewrite everything that uses the old tool to use the new tool. Do NOT intermix them! It just makes things more difficult to figure out when something goes wrong.

    When in Rome...

    I discussed the situation with the code author and our boss. My boss asked me to rewrite the mess. I asked what it does. The author hemmed, hawed and generally acted sheepish, as he wasn't really sure. Apparently, three years ago he was trying to do several things at once, and so he just put code here, there, wherever until it worked. He never went back to clean it up. So now I'm supposed to re-implement something that does who-knows-what and verify it how? Maybe I'll just code a Paula Bean with a sleep...

    Since my boss doesn't trust the other guy with this, it's now on my schedule for a complete reverse engineering/rewrite.

    If it wasn't for idiots, I'd be a poor man.



  • Huh, I thought Spring and Hibernate could play perfectly together and use the same connection pool. I often hear the words "Spring" and "Hibernate" used in the same sentence as if they were married.


    From what I can tell, bitwise operations are the main tool for the young "genius" developers who try to prove their hotness with unnecessarily complex logic.


    Snoofle, do you have a blog of any kind where you detail your adventures of undevelopment? I'd love to read that.



  • @OhNoDevelopment said:

    I thought Spring and Hibernate could play perfectly together and use the same connection pool
    I'm not really proficient in either tool. For all I know, it's entirely possible that they do play well together. I doubt they would share the same connection pool, though (dunno, it just seems they'd each have their own tools).

    What I know for certain is that this guy had no clue about either tool.

    @OhNoDevelopment said:

    do you have a blog of any kind where you detail your adventures of undevelopment?
    Yes, it's located here and it's open for anyone to contribute; feel free.



  • @snoofle said:

    What I know for certain is that this guy had no clue about either tool.
    At least you got tools. We've got a NIH syndrome. I long for the days of using a framework.
    A quick google for "Spring Hibernate integration" seems to confirm my suspicions about the tools.

    @snoofle said:

    Yes, it's located here and it's open for anyone to contribute; feel free.
    Oh yes I do intend to contribute, but I was hoping for "Snoofle: The Book" or something like that.



  • @snoofle said:

    @OhNoDevelopment said:

    do you have a blog of any kind where you detail your adventures of undevelopment?
    Yes, it's located here and it's open for anyone to contribute; feel free.

    Watch out, if you are not nicer to your fanbase you will lose your Star status and become a regular nobody.



  • @Speakerphone Dude said:

    @snoofle said:

    @OhNoDevelopment said:

    do you have a blog of any kind where you detail your adventures of undevelopment?
    Yes, it's located here and it's open for anyone to contribute; feel free.

    Watch out, if you are not nicer to your fanbase you will lose your Star status and become a regular nobody.

    I joined TDWTF for this?! Forget that, I'm out!



  • @OhNoDevelopment said:

    @Speakerphone Dude said:
    @snoofle said:

    @OhNoDevelopment said:

    do you have a blog of any kind where you detail your adventures of undevelopment?
    Yes, it's located here and it's open for anyone to contribute; feel free.

    Watch out, if you are not nicer to your fanbase you will lose your Star status and become a regular nobody.

    I joined TDWTF for this?! Forget that, I'm out!

    snoofle has provided tons of entertainment to many WTFers for a long time, however you can see that his writing is becoming less focused, more tired, and sometimes it feels like he is submitting too many stories in order to rekindle the flame of his past glory. I would not be surprised to see him having his own reality tv show someday, like Ozzy Osborne, Gene Simmons or Donald Trump. Can't wait to see if he has a hot daughter that post racy pictures of herself on Twitter (like Wayne Gretky's) or if he looks like a zombie with the shakes and the jimmy legs (like Ozzy).



  • @OhNoDevelopment said:

    Huh, I thought Spring and Hibernate could play perfectly together and use the same connection pool.
    Hmm, we use our in-house (database connection) pool, which works well enough, even though the code isn't exactly cleanly written. It actually implements javax.sql.DataSource (that was my idea), but it took a small library to hook it up to Hibernate.



  • @OhNoDevelopment said:

    Snoofle, do you have a blog of any kind where you detail your adventures of undevelopment?
    "Undevelopment"? Ist that even a word? Shouldn't the opposite of "development" simply be "velopment"? Or maybe"envelopment"?

    So the idiots that snoofle works for "(en)velop" the solutions for their problems in layers of confused, misguided requirements and/or broken, inefficient or just simply bad code, and snoofle's job is to "de-velop" it...

    @Speakerphone Dude said:

    @snoofle said:
    Yes, it's located here and it's open for anyone to contribute; feel free.

    Watch out, if you are not nicer to your fanbase you will lose your Star status and become a regular nobody.
    What do you mean? He's nice enough to even let us contribute to his own blog/forum website!


  • @Speakerphone Dude said:

    his writing is becoming less focused...rekindle the flame of his past glory
     

    Sorry to disappoint... When I first joined the forum, I was essentially posting best-of-the-last-25-years, so the stories tended to be more epic. Now, it's more along the lines of what-did-those-idiots-do-today? The stories aren't quite so epic, but one wouldn't (I hope) expect them to be.

    And of course, you always have the option of not reading my posts...

     



  • What if Snoofle is the Zombie with the jimmy legs with a hot exhibitionist daughter?

    That reality show would rock!

    Code Zombie Dilbert with Interluides!

     



  • @snoofle said:

    @Speakerphone Dude said:

    his writing is becoming less focused...rekindle the flame of his past glory
     

    Sorry to disappoint... When I first joined the forum, I was essentially posting best-of-the-last-25-years, so the stories tended to be more epic. Now, it's more along the lines of what-did-those-idiots-do-today? The stories aren't quite so epic, but one wouldn't (I hope) expect them to be.

    And of course, you always have the option of not reading my posts...

     

    I think/hope that Speakerphone Dude's comments were meant to be taken as tongue in cheek.

    Your posts are brilliant Snoofle, you pretty much run this forum! I love logging in to find you've posted yet another amusing anecdoe. This latest one sounds like it's been agonising to debug though. :-(

    Good luck with the rewrite, it makes the re-write that I've got coming up seem a bit less epic now (in a good way).



  • @OhNoDevelopment said:

    I was hoping for "Snoofle: The Book" or something like that.
     

    Personally, I'm waiting for "Snoofle : The Movie".  I heard that Michael Bay will be directing.

     



  • @DCRoss said:

    @OhNoDevelopment said:

    I was hoping for "Snoofle: The Book" or something like that.
     

    Personally, I'm waiting for "Snoofle : The Movie".  I heard that Michael Bay will be directing.

     

    To be followed almost immediately by "Snoofle: The Video Game".

     


  • Trolleybus Mechanic

    @da Doctah said:

    @DCRoss said:

    @OhNoDevelopment said:

    I was hoping for "Snoofle: The Book" or something like that.
     

    Personally, I'm waiting for "Snoofle : The Movie".  I heard that Michael Bay will be directing.

     

    To be followed almost immediately by "Snoofle: The Video Game".

     

     

    I can't wait to read the developer blog about that one.

     



  • @DoctaJonez said:

    @snoofle said:

    @Speakerphone Dude said:

    his writing is becoming less focused...rekindle the flame of his past glory
     

    Sorry to disappoint... When I first joined the forum, I was essentially posting best-of-the-last-25-years, so the stories tended to be more epic. Now, it's more along the lines of what-did-those-idiots-do-today? The stories aren't quite so epic, but one wouldn't (I hope) expect them to be.

    And of course, you always have the option of not reading my posts...

     

    I think/hope that Speakerphone Dude's comments were meant to be taken as tongue in cheek.

    I have no problem with this interpretation as long as it's not some guy's tongue that is in my cheek!



  • @Lorne Kates said:

    I can't wait to read the developer blog about that one.
     

    It would be so meta.


Log in to reply