The code which made me sleepless last night.

I wrote a small piece of code which basically listens to JMS queue and receives messages. The following 3 code snips shows what I wanted to do.
The snip on left is the improved and refactored code and the one on the right is the bad code.

In this following snip I tried to create a queue and receive messages in it.

[ Click to enlarge the image ]

The mistake I did here, as can be seen in the right hand side is, I create the queue and also receive messages in it, both in the same method.

In the following snip, I am calling the method to receive messags in the queue but what I overlooked is that I am also creating the queue in it. This is the where the disaster is lurking.

[ Click to enlarge the image ]

So even if I close session and connection in the following snip, it just closes the last connection.

[ Click to enlarge the image ]

Whereas I have created hell lot of connections in snip-2, ( Notice the while loop, which was churning umpteen number of queue connections )
I am only closing the last connection in snip-3 which leaves thousands of connections opened in memory. This triggered garbage collection which took about 12-13% of CPU consistently even if nothing was processing in the system.

This demonstrates the side effects of trying to do too many things in a single method. Also it is very important to handle connections, whether databse / JMS / sockets very very carefully. This made me sleepless for a night.

Published by Chaitanya

I am a family man, perpetual learner, coffee drinker, dessert eater and Happiness Engineer at Automattic. I also read, write, move, and meditate.

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: