Thursday, June 19, 2014

tomcat dbcp abandoned connections

We recently switched from commons dbcp to tomcat dbcp and after 2-3 weeks another architect ran into an issue where his backend job would run into a resultset close issue.  He was saying it started right about the time we made the switch and it happens only for long running jobs.  That reminds me of something I had read about abandoned connections so I asked him to check after how many minutes it does this and it was 20min 1 sec. It seems I had added a property in dbcp


so first thing was to confirm if this was really the case because our transaction timeout for long running queries is also 20 min but that has been for ages so this abandoned was the only recent change. Finally I found logAbandoned would log into tomcat catalina.out and I indeed saw this

Jun 18, 2014 3:54:07 PM org.apache.tomcat.jdbc.pool.ConnectionPool abandon
WARNING: Connection has been abandoned PooledConnection[com.mysql.jdbc.JDBC4Connection@2ba0c302]:java.lang.Exception
    at org.apache.tomcat.jdbc.pool.ConnectionPool.getThreadDump(
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(
    at org.apache.tomcat.jdbc.pool.ConnectionPool.getConnection(
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(

So now only thing left is to fix it.

There are two approaches:
  1. While iterarting resultset reset the abandoned timer for long running job using ResetAbandonedTimer
  2. Or  for now just use dataSource.setAbandonWhenPercentageFull(50);
I am leaning more towards second one because most long running jobs run on slave and I am sure we don't exceed more than 50% capacity for now. If this creates an issue I would do #1.  This was interesting issue and the only reason I was able to debug in 30min to 1 hour because I had read DBCP documentation before for atleast 2 hours so sometimes passive reading stuff helps when you indirectly :).

Wednesday, June 18, 2014

Band-Aid programming

I saw a code sample last Saturday for someone where there was a method with 9 TODOs and I saw some code that was doing

                xxxDao.setLastSectionNum(custId, lastSectionNum);
                xxxDao.setLastChangeNum(custId, lastChangeNum);
                xxxDao.setLastModified(custId, now, xxxContext);
                if (subject == null) {
                        xxxDao.updateSubject(custId, subject);

My blood was boiling after seeing this. This is what is called as band aid programming. I found out that programmer has added first line 2 years ago and then one more requirement came in so he added one more dao method and in 2 years we get 4 methods. WTH couldnt he refactor the code to just do 1 update query or use some ORM with POJO. 

But to me he is typical band-aid programmer because I saw 9 TODOs in one 100 lines method. What that tells me is that he is just fixing bare minimum things and is having a tunnel vision.

Monday, June 9, 2014

Copy as cURL -- a great idea

Sometimes small actions are very powerful. I was recently using firebug and ran into a menu item called as copy as cURL. for e.g. when invoked on a  DuckDuckGo search on "Fox4 weather" gives

curl '' -H 'Host:' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' -H 'Accept-Encoding: gzip, deflate' -H 'DNT: 1' -H 'Referer:' -H 'Connection: keep-alive'

Great idea Firebug team and I am hoping this small action will save me a lot of time in future.

Wednesday, June 4, 2014

iphone5 charger cable worse quality I have seen

The lightning cable broke within a month. I was on trip to Houston and in the car it will start charging and then die. Then I went to hotel and put it on wall plug and it wont charge. I had a suspicion that putting it in car confused the phone but restart wont help. I remember 2-3 days back it would charge and 90% and then say accessory not supported. I thought its some bug in new update.

So I went to walmart in Houston at 9:00 in night on a trip to NASA and then got a cable, while I was checking out I found one more customer came asking for iphone lightning charger cable, I was now sure its a cable issue.  So I bought the cable went to hotel and this also wont charge, I went again and got another cable and that one worked.  What a bummer I thought the phone went bad.

Three days after I came back home I called AT&T to send me a replacement cable and they sent me their own proprietary cable which means even they know that the OEM cable by Apple sucks and they charge $30-$40 for it. No wonder walmart guy was telling me not to waste money on it and buy the $15 off the shelf non OEM cable.

Anyways the AT&T cable looks sturdy so far.