Monday, 20 October 2008

Software caused connection abort: recv failed with MySQL and Hibernate

If you get "Software caused connection abort: recv failed" after attempting to login to your JBOSS server after a long period of idle time then you need to take a cup of coffee and sit down as this is going to take a while to fix.

In a web application that we developed,  we were using a JBOSS server version 4.04, connecting to MYSQL 5.0.24 with Hibernate 3.1 and we started getting these messages after the server had been sitting idle for some time and a login was attempted. The initial diagnosis was that the MySQL /JDBC connection was getting stale and it could be resolved by updating your MySQL driver to the latest version and adding a few properties to your data-source connection file. We updated the driver to mysql-connector-java-5.0.8 and as explained in this excellent post we added the following lines :
       
 (exception-sorter-class-name)
com.mysql.jdbc.integration.jboss.ExtendedMysqlExceptionSorter
(/exception-sorter-class-name)
(valid-connection-checker-class-name)
com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker
(/valid-connection-checker-class-name)        
The basic idea was that the JNDI would ping the DB periodically and keep the data source alive. Well, it didn't work for us but atleast we had the latest driver.

The next approach was to write a custom class that would operate on the database every hour or so and keep the connection fresh. (According to the docs, MySQL marked the connection stale after 8 hours but I was not in a trusting mood). I wrote a Java Timer class that was called by a servlet every 30 minutes. The objective of the class was to check the table which housed our support requests and if found a new request, it would shoot me an email. The Timer class and the Servlet hookup worked fine and I stated getting emails if there was a support request waiting to be serviced but the original problem still remained. The server would still spit out a long stream of exceptions, starting with Software caused connection abort: recv failed if a login was attempted after a few hours of idle time.This was getting annoying!

I then approached the problem in a different way and went over my Hibernate connection pooling setup. We were using C3PO but maybe something was missing? I reset the Hibernate connection pooling parameters to the following :
(property name="connection.provider_class")org.hibernate.connection.C3P0ConnectionProvider(/property)
(property name="c3p0.acquire_increment")1(/property)
(property name="c3p0.idle_test_period")100(/property) (!-- seconds --)
(property name="c3p0.max_size")100(/property)
(property name="c3p0.max_statements")0(/property)
(property name="c3p0.min_size")10(/property)
(property name="c3p0.timeout")100(/property) (!-- seconds --)

and Voila, the exceptions disappeared. We finally had a clean console when attempting login ever after several hours. So while, the Hibernate connection pooling params appear to be the main culprit, I feel that its equally important to update your MySQL driver and make sure that there are no loose ends in the JNDI data-source params.

Wednesday, 15 October 2008

www.samaaj.com.au - a platform for students !!


One night while half asleep, I had a dream and in my dreams I saw a website. WoW!! People have such cool dreams and I just saw a website. But in my dream, the website was not just a website, it was a cool thing, a platform for students to get together and help each other, a helpline for students. It was something that I had to act upon.

Having been an International student myself, I understood the problems that some students faced and this was the main motivation behind www.samaaj.com.au. I got up from my sleep and shot of an email to a few friends who I hoped would share my enthusiasm of giving up their free time and instead spend time building a web-site. Well, my faith wasn't misplaced. My pals responded and we slogged through the weeks and months. Yes, there were periods of inactivity and procrastination but finally on the 13th of October, 2008 we made a BETA version of our website LIVE and available to the general public. 

While the initial version was BETA and I expected several refinements and fixes over the coming months but overall, it was a fine effort and when I look back at the year that we spent in refining and developing the application, I am happy that we acted on our impulse. Even though at times it appeared as if we were hardly moving, we persisted and today that dream is a reality. The web-app has been built in what I know best - Java. Over the next few years, we refined our user interface, added several bug fixes and changed our hosting from a home run server to Amazon Web Services's cloud infrastructure and released a brand new look of the Student Helpline version 2.1.1 in September 2012.


Wednesday, 10 September 2008

Preparing for Java Certification 7?

Changes to the exam process for Java certification 7, now require two exams to be cleared if not holding an older certification. Those who hold a prior certification, can take the Java SE 7 Upgrade Exam  and upgrade their quals.
The mantra to succeed in these certification exams appears to be 
(a) practice, practice, practice writing a lot of code without an IDE 
(b) Sit for mock exams. Enthuware appears to be a popular choice for Java 7 aspirants. 
(c) Follow a good book. Kathy and Sierra are to come out with their book for version 7, sometime late 2013, but if using their SCJP 6.0 guide, follow Bert Bates advice here and here.


 ** Links to Sample Tests **
  • JavaRanch's collection of sample tests and sites that give a very good overview of the questions. This is the most comprehensive set of links and tests. A MUST!!
  • Good set of questions based on SCJP 1.4 but well worth a study.
  • JavaProgramming's  set of tests.

    ** Links to Study Notes and Materials **
  • Study Notes  on AK Gupta's site. Also visit his compilation of links for MORE tests and resources.
  • Another set of study notes on the exam. This one is based on SCJP 2.0 but it is still worth a look.
  • Read a guy's experience and thoughts on how to prepare.
       ** Links to some good Java resources **
  • Start with the JavaRanch and its forums.
  • JavaWorld has a set of Java blogs and Q&A forums.
  • JavaMagazine is Oracle's latest offering starting August 2011. 

    Saturday, 6 September 2008

    Test Driven Development and a Code Kata

    (Please click on the images to get a more readable version).


    This week I thought of trying out a CodeKata problem and choose Kata 4 . The kata requires you to read a data file, parse it , extract some values and print the difference between them. A simple exercise one would say.

    To explain the problem in brief. A data file containing the results of the football league is provided. The requirement is to extract the name of the team with the lowest goal difference in For and Against goal values. I decided to make this problem a bit more exciting by not just coding along a 'happy path' but also looking out for scenarios where there might be no valid data rows or there might be two teams matching the lowest goal difference criteria.

    Using a test driven approach, I hacked out a set of basic JUnit tests to parse a valid data row and extract the For and Against goal values. My data row looked somewhat like this.



    and the tests to extract the values from this data row looked like this:

    Now that I was getting the For and Against Goal values, all I needed was to calculate their difference and store the team name (& goal difference) in a 'results list'. The tests for doing this were accomplished as below:

    As evident from the test, my 'controller' class has ONE main function which is updateLowestGoalDifferenceTeam(). This function performs the core business processing in terms of updating the list holding the lowest goal difference team names. This function is given as below.




    The function gets the list holding the team name, extracts the goal difference values and compares the same against the goal difference value passed to it as a parameter in a HashMap. If the passed in value is lower, the existing team becomes a part of the list.



    In all the Kata was a simple excercise but it was instructive in analysing the data structures that could be used and in studying the different approaches that could be taken to solve this problem. If you would like to compare your efforts with mine or wanted to know more about the solution, ping me for the complete code as an Eclipse project.