Tuesday, June 30, 2009

How to stop a run-away SQL transaction

Recently was testing my web-app against a SQL-Server 2008 database and got a time-out expired message from the SQL database. No matter what I did after that it just did not want to drop the transaction and I continually got the time-out. I needed to find and stop the SQL transaction.

The solution: Sign onto the SQL-Server with the Enterprise Manager.

Execute sp_who2
and look for rows that are blocked on another connection (the BlkBy column will have the spid of the blocking connection in it).

Now kill the culprit:
kill xx
(xx = spid).

Monday, June 29, 2009

jqGrid Dynamically loading select tag

Scenario: You are using the jqGrid to edit rows that contain fields that are of HTML tag type "SELECT".

Problem: You do not want to hard code the values of the select tag like in the jqGrid samples. For example:

editoption: { value: "FE:FedEx; IN:InTime; TN:TNT" }
Solution: For example to load a list of countries dynamically, define the variable before the definition of the jqGrid:

//get all countries
var countries = $.ajax({url: $('#ajaxAllCountriesUrl').val(), async: false, success: function(data, result) {if (!result) alert('Failure to retrieve the Countries.');}}).responseText;


Now define your jqGrid:

$(yourgrid).jqGrid({
...
colModel: [
{ name: 'Id', index: 'Id', width: 20, sortable: true, align: "center", editable: false, editoptions: { readonly: true, size: 0 }, search: true },

{ name: 'Country', index: 'Country', width: 80, sortable: true, editable: true, edittype: "select", editrules: { required: true }, editoptions: { size: 71} }
],
...
loadComplete: function() {
$(item).setColProp('Country', { editoptions: { value: countries} });
},
...
Important: The ajax call to retrieve the countries must be set to "async: false" otherwise you have a very good chance that the grid will be defined before the data is actually returned. Using the same logic you can reload the list based on some other event and trigger the reload of the grid if need be to reload the new list.

Wednesday, June 24, 2009

Firebug Network Monitoring Quick Reference Guide


Network Monitoring

Your web app on the server has plenty of tools and ways to measure performance, as well as tools like SQL-Server Profiler for SQL databases. But how do you trace, track and debug client-side code interactively? The use of alerts has been useful but does not support an interactive way for you to interact with the web-page on the fly. To make matters worse, even if you place some traces into your js code to help find the slow functions, etc, network latency may be another cause of slow response. Or maybe the order your files are loaded is an issue? Or maybe their are caching issues. Well, Firebug to the rescue. It has an interactive way to interact with your code, css and other files. It has great support for debugging code and injecting css on the fly. It also has a informative network activity tracking tab, which is what this article is all about, that will help troubleshoot network latency, caching and other network related issues.

Watch the timeline unfold
Every file that is included and loaded into your web-page is represented by one row in the table. Notice how the files are loaded not just one after the other, but also when they start loading and end loading by looking at the file's bar. The bar shows you when the file started and stopped loading relative to all the other files. In this example the notice how the second file (after Get 1) is only starts loading after its predecessor (GET 1) has ended, but how the four files loaded after the first one's bars overlap. The first file is a JavaScript file, which are loaded one at time, never in parallel. The second to fifth files are also JavaScript files, but they are loaded from the cache and can be loaded in parallel. The last five files in this example are images.

A description of the information in the panel:
  1. Name of files : The first column shows the names of all the files that were loaded as part of this web-request. Note it is not a running history and only applies to this page for this single web-request. Also note the GET prefix to the name which depicts the method of the request (POST vs. GET).
  2. HTTP Status : The second column shows the status of the HTTP request and the code. Some interesting codes you may see in the example are: Code 200 being successful HTTP request, code 304 stating that the file was not modified since the last request which is based on some caching time-limit.
  3. Base URL : The third column shows the base URL of each file. If you are loading files from other sites (e.g: linking image from other site, putting ads in your page) then a different URL will be shown for that particular file.
  4. Size : The fourth column shows the size of each file.
  5. Load Time : The last column shows the time it took to load that particular file. It also shows whether or not those files are loaded from the cache. The different colors used for the bars have significance:
    1. Green : DNS Lookup
    2. Light-Green : The time to connect to the server
    3. Light-Brown : The time it waited in the queue. You'll notice this when loading js files as they can only be loaded in sequence, not in parallel
    4. Purple : The time waiting for a response from the server
    5. Dark Grey : Request sent to server, file loaded from server and not the cache
    6. Light Grey: Request sent to server, “304 Not Modified” received, file loaded from the cache.

Note that the order of the files in the list denotes the order in which the files where loaded from the server. Also take note that js files are loaded in sequence, one-after-the-other.

Break it down by type

Sometimes you are particularly concerned about a certain type of file, like HTML (GET 1 in this example) files or images. Click the buttons in the Net toolbar to filter the list by type. This is also a great way to find out the total size and download time for a particular type of file.

Cached or not cached

A picture is worth a thousand words. In the example to the left a lot can be taken from the color of the bars. Each file's network request can be different, some are loaded from the cache and others have to be loaded from the web-server over the network. Firebug color codes each request that is served from the cache in a lighter gray and darker gray for when it is loaded from the server (see the fifth file, 907ms).



Files loaded in sequence (not parallel) follow each other, you can see this by the bars not overlapping. This is not true for cached js files however, as can be seen for the js files loaded with a "304 Not Modified" HTTP status code. See how the images are loaded in parallel?! These are all indicators of bottlenecks and design issues that can be a great tool in performance tuning your app.



Lets see what the effect is of setting for caching a file. Have a look at the GetjQueryScripts file that is loaded (by the way this is a call to an MVC action method called GetJqueryScripts that loads one js file for all jQuery scripts by combining them on the server). Note in the example to the left that "GET GetjQueryScript" has a status of "200 OK". This means it has been loaded from the server (not a cached version). I also took 2.36s (seconds) to load from the server. This is the first time this JavaScript file is loaded from the server although caching has been set for this file.

Now lets see what happens on the next page request. Notice how the HTTP status has changed from "200 OK" to "304 Not Modified". Since the file has caching enabled, it told the client (your browser) to cache the file for a predetermined amount of time for subsequent requests. There are three tabs now, as apposed to the previous example where there was only a Headers and Response tab, now there is also a Cache tab, denoting that this file was cached.

Some of the information in the cache shows the time it was last modified and how long this file will be cached for, which is the Expires property that tells you that it expires in about 8 hours. It also shows how often this file was requested, and in this case 106 times from the disk (cache location for the browser on your machine).


Examine HTTP Headers

HTTP headers contain a lot of information like the mime type of the file, the type of web server, caching directives, the cookie, and lots more.


XMLHttpRequest monitoring

There is also an Ajax tab for requests performed via the XMLHttpRequest module. Since a browser has no visual way op representing what is happening during this AJAX request to the user, unlike when we submit a page and you see the progress indicators on the browser showing you the progress, FireBug's XHR tab exposes these events and you can see the start and completion of these AJAX requests and if they were successful and what was returned from the server in the Conole tab's Response tab . If the template gets too busy simply click the Clear button.

Firebug Console Quick Reference Guide

Firebug adds a global variable named "console" to all web pages loaded in Firefox. This object contains many methods that allow you to write to the Firebug console to expose information that is flowing through your scripts.

console.log(object[, object, ...])

Writes a message to the console. You may pass as many arguments as you'd like, and they will be joined together in a space-delimited line.

The first argument to log may be a string containing printf-like string substitution patterns. For example:

console.log("The %s jumped over %d tall buildings", animal, count);

The example above can be re-written without string substitution to achieve the same result:

console.log("The", animal, "jumped over", count, "tall buildings");

These two techniques can be combined. If you use string substitution but provide more arguments than there are substitution patterns, the remaining arguments will be appended in a space-delimited line, like so:

console.log("I am %s and I have:", myName, thing1, thing2, thing3);

If objects are logged, they will be written not as static text, but as interactive hyperlinks that can be clicked to inspect the object in Firebug's HTML, CSS, Script, or DOM tabs. You may also use the %o pattern to substitute a hyperlink in a string.

Here is the complete set of patterns that you may use for string substitution:

String Substitution Patterns
%sString
%d, %iInteger (numeric formatting is not yet supported)
%fFloating point number (numeric formatting is not yet supported)
%oObject hyperlink

console.debug(object[, object, ...])

Writes a message to the console, including a hyperlink to the line where it was called.

console.info(object[, object, ...])

Writes a message to the console with the visual "info" icon and color coding and a hyperlink to the line where it was called.

console.warn(object[, object, ...])

Writes a message to the console with the visual "warning" icon and color coding and a hyperlink to the line where it was called.

console.error(object[, object, ...])

Writes a message to the console with the visual "error" icon and color coding and a hyperlink to the line where it was called.

console.assert(expression[, object, ...])

Tests that an expression is true. If not, it will write a message to the console and throw an exception.

console.dir(object)

Prints an interactive listing of all properties of the object. This looks identical to the view that you would see in the DOM tab.

console.dirxml(node)

Prints the XML source tree of an HTML or XML element. This looks identical to the view that you would see in the HTML tab. You can click on any node to inspect it in the HTML tab.

console.trace()

Prints an interactive stack trace of JavaScript execution at the point where it is called.

The stack trace details the functions on the stack, as well as the values that were passed as arguments to each function. You can click each function to take you to its source in the Script tab, and click each argument value to inspect it in the DOM or HTML tabs.

console.group(object[, object, ...])

Writes a message to the console and opens a nested block to indent all future messages sent to the console. Call console.groupEnd() to close the block.

console.groupEnd()

Closes the most recently opened block created by a call to console.group.

console.time(name)

Creates a new timer under the given name. Call console.timeEnd(name) with the same name to stop the timer and print the time elapsed..

console.timeEnd(name)

Stops a timer created by a call to console.time(name) and writes the time elapsed.

console.profile([title])

Turns on the JavaScript profiler. The optional argument title would contain the text to be printed in the header of the profile report.

console.profileEnd()

Turns off the JavaScript profiler and prints its report.

console.count([title])

Writes the number of times that the line of code where count was called was executed. The optional argument title will print a message in addition to the number of the count.

Firebug is a free Firefox extension. Ready to install?

OutputCacheAttribute Override/Customization

The scenario: A web app is using the OutputCacheAttribute to set a cache duration for data that has different life spans.

The IDE: ASP.NET MVC with Visual Studio 2008
The environment: Web-App on IIS

The data that is being retrieved varies greatly in terms of their lifespan:
  • Some data is very static like States, Countries
  • Other data is more fluid, especially during end-user testing, like the load of scripts that have to be fixed occasionally and pushed to production during the day. Especially early on in the test these pushes can happen hourly in some cases and as progress is made during the tests becomes less frequent
  • A list of FAQ and Bug fixes that the users can access from the web page has to be refreshed a few times a day

No matter the reason or validity thereof, it is simple enough to specify the OutputCache for each. For example:
Caches the load of script files for 2 hours:

[OutputCache(Duration = 7200, VaryByParam = "none")]
public FileResult GetjQueryScripts()
{
string combinedResults = combineLikeDirectoryFiles("~/Scripts/jquery/Base/", FileType.js);
combinedResults += combineLikeDirectoryFiles("~/Scripts/jquery/Plugins/", FileType.js);
combinedResults += combineLikeDirectoryFiles("~/Scripts/jquery/Plugins/jquery.grid/", FileType.js);
combinedResults += combineLikeDirectoryFiles("~/Scripts/jquery/Plugins/jquery.datepick/", FileType.js);

return streamText(combinedResults, js);
}

Caches States for 2 days:

[OutputCache(Duration = 57600, VaryByParam = "none")]
public string GetAllStates()
{
return GetAllForGenericData(Repository.FindAll());
}


The problem: This works great, but has some issues:
  • hard coded so needs re-compilation and code-migration (who knows how long that can take)
  • In a development environment, especially if you do not want caching. For example my scripts are cached and when I make a script change do not want the cached version locally during unit testing. Set the duration property of the cache to 0 for this and REMEMBER to change it to (what was it again?!) before you build and deploy, or commit/check-in to version-control.
The solution: Use the CacheProfile property from ASP.NET's MVC or create a custom output cache attribute that in this example takes two parameters, a file type (enum) and the VaryByParam. Unlike the CacheProfile option, this one can target any source, from databases to flat files, xml files, JSON files, etc. Here is what the method/action looks like when decorated with the custom attribute. It loads script files based on a file type (js, css, etc) and retrieves the duration from the web.config file (of course this can be anything, DB, xml file, etc):

[CustomOutputCacheAttribute(TypeOfFileToCache = FileType.js, VaryByParam = "none")]

public FileResult GetjQueryScripts()
{
string combinedResults = combineLikeDirectoryFiles("~/Scripts/jquery/Base/", FileType.js);
combinedResults += combineLikeDirectoryFiles("~/Scripts/jquery/Plugins/", FileType.js);
combinedResults += combineLikeDirectoryFiles("~/Scripts/jquery/Plugins/jquery.grid/", FileType.js);
combinedResults += combineLikeDirectoryFiles("~/Scripts/jquery/Plugins/jquery.datepick/", FileType.js);

return streamText(combinedResults, js);
}


Here is the declaration of the custom attribute:

public class CustomOutputCacheAttribute : OutputCacheAttribute
{
private FileType _typeOfFileToCache;

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
SetupDuration();
base.OnActionExecuting(filterContext);
}

public FileType TypeOfFileToCache
{
get { return _typeOfFileToCache; }
set { _typeOfFileToCache = value; }
}

public string VaryByParam
{
get { return base.VaryByParam; }
set { base.VaryByParam = value; }
}

public void SetupDuration()
{
int durateMe;
switch (TypeOfFileToCache)
{
case FileType.js:
{
base.Duration = int.TryParse(ConfigurationSettings.AppSettings[ "outputCacheDurationScriptLoad" ],
out durateMe)
? durateMe
: 0;
}
break;
case FileType.css:
{
base.Duration = int.TryParse(ConfigurationSettings.AppSettings[ "outputCacheDurationCssLoad" ],
out durateMe)
? durateMe
: 0;
}
break;
default:
base.Duration = 0;
break;
}
}
}

public enum FileType
{
js = 1,
css = 2
}


Note that it inherits from System.MVC library's OutputCacheAttribute class. The OnActionExecuting method is called by the framework, which is overridden. The OnActionExecuting method essentially only has one addition, the SetupDuration() method that gets its duration from the web.config file based on the file type. In this example the app has a different duration for css and js files. The next important step in the OnActionExecuting() method is to invoke the base's OnActionExecuting() method, and you're done.

Tuesday, June 23, 2009

jQuery Grid Plugin

Building web-apps using the new MVC-framework for ASP.NET can be challenging if you need to build the boiler-plate code for the SMART controls that come standard with ASP.NET, like the GridView, ListView, etc.

However, jQuery's plug-in, jqGrid, makes this easy. Although it can be time-consuming to setup the template that specifies the requirements for the grid, it is easy to duplicate once you have it set up.

So basically you specify the columns you want to see in the grid, specify the format for each column, and it will perform an Ajax call for built-in sorting, search, and CRUD functions.

Some of the supported features:
  • paging functions
  • add, edit, delete & search records
  • accepts XML, JSON, array or user data as input
  • multiple selection of rows
  • sub grid & grid details (great feature!)
  • UI datepicker integration
  • & more…

Of course you need to specify the server-side-code to handle these events. Pass a DTO back, JSON serialized (some other formats are supported), and it will bind the DTO's properties to the grid.

Minimum Requirements : jQuery 1.1.4+
Browser Compatibility: All Major Browsers
Website : http://www.trirand.com/blog/
Demo : http://trirand.com/jqgrid/jqgrid.html
Downloads : http://www.trirand.com/blog/?page_id=6

The number of lines of HTML you need to write yourself for this to work is just two lines:
  1. One <TABLE> tag
  2. One <DIV> tag for paging if desired
That's it, 2 lines, one for the body and one for the paging controls that also contains the controls for the CRUD functions.

Have been using it in a in-house web-development project for a UI/Process intensive CRM app with users who's machines are 10 years old, about 785MB RAM machines with an x86 Family 6 Model CPU, and it performs great. We are running it mainly on FireFox (the gold standard browser engine :-), and have tested it on IE8, Chrome and Safari 3, on Windows 2000 to XP and Vista platforms with no issues.