Using the debugger to know the status of program execution

We have seen how to use the debugger to verify the execution flow of a program (using the step operations) and to inspect variables. You can also use the debugger to know what the status of the running program is. For example, a web request is taking too long and you want to know where exactly the execution is stuck. You can use the debugger to find this. It is similar to taking the thread dump of a running program, but is much easier than the methods used to get the thread dump. Let's assume that our CourseDAO.getCourses method is taking a long time to execute. Let's simulate this by using a couple of Thread.sleep calls, as shown in the following code snippet:

public List<Course> getCourses () throws SQLException { 
  //get connection from connection pool 
  Connection con = 
DatabaseConnectionFactory.getConnectionFactory().getConnection(); try { Thread.sleep(5000); } catch (InterruptedException e) {} List<Course> courses = new ArrayList<Course>(); Statement stmt = null; ResultSet rs = null; try { stmt = con.createStatement(); StringBuilder sb = new StringBuilder("select course.id as
courseId, course.name as courseName,") .append("course.credits as credits, Teacher.id as teacherId,
Teacher.first_name as firstName, ") .append("Teacher.last_name as lastName, Teacher.designation
designation ") .append("from Course left outer join Teacher on ") .append("course.Teacher_id = Teacher.id ") .append("order by course.name"); rs = stmt.executeQuery(sb.toString()); while (rs.next()) { Course course = new Course(); course.setId(rs.getInt("courseId")); course.setName(rs.getString("courseName")); course.setCredits(rs.getInt("credits")); courses.add(course); int teacherId = rs.getInt("teacherId"); if (rs.wasNull()) //no teacher set for this course. continue; Teacher teacher = new Teacher(); teacher.setId(teacherId); teacher.setFirstName(rs.getString("firstName")); teacher.setLastName(rs.getString("lastName")); teacher.setDesignation(rs.getString("designation")); course.setTeacher(teacher); } try { Thread.sleep(5000); } catch (InterruptedException e) {} return courses; } finally { try {if (rs != null) rs.close();} catch (SQLException e) {} try {if (stmt != null) stmt.close();} catch (SQLException e)
{} try {con.close();} catch (SQLException e) {} } }

Start Tomcat in Debug mode, and run listCourses.jsp in Debug mode. Because we have inserted Thread.sleep statements, the request will take time. Go to the Debug view, which is where threads and stack frames are displayed. Click on the first node under the Tomcat debug configuration node and select the Suspend option, as shown in the following screenshot: 

Figure 6.15: Suspending program execution

The debugger pauses execution of all threads in the program. You can then see the status of each thread by expanding the thread nodes. You will find one of the threads executing the CourseDAO.getCourse method and the statement that it was executing before being suspended:

Figure 6.16: The status of suspended threads

From the preceding screenshot, you can see that the execution of the thread is suspended in the CourseDAO.getCourses method of the Thread.sleep statement. You can even inspect variables at each stack frame when the program is suspended. By suspending the program and inspecting the state of threads and stack frames, you may be able to find bottlenecks in your application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.133.180