Stop accidental git commits of local dev changes to config files

During development, I often make changes to a few configuration files for local testing. Most of the time, I add each file individually into the staging area so these local config changes aren’t committed. Yesterday, I made a mistake and committed the local config. I wasn’t sure how it happened but I must have clicked the commit all tracked files accidentally. The test server was then built with my local config. Oops.

To stop this from happening again, I did some googling and found this handy git command:

git update-index --assume-unchanged <file>

This will temporarily ignore changes in the specified file. All without changing .gitignore which is a tracked file in the project.

SQL Server stored procedures continue execution on error

This week I came across a bug in a stored procedure caused by this baffling default behaviour in SQL Server. By default, the execution of a stored procedure continues onto the next statement after an error is raised. The stored procedure in question calls another stored procedure, which has been modified in the previous release to take an extra parameter.

DECLARE @tmp TABLE (value VARCHAR(100))
	EXEC sp_anotherStoredProcedure
		@orderId = @orderId,
		@petType = 'cat'

		FROM @tmp l
		INNER JOIN Awesome_Event_Buffer r ON l.value = r.Description
		WHERE r.customer = @customer
		print 'do some important things'

Because of the addition of the new parameter, the nested store procedure call now always fail with an error. However, the calling stored procedure is not halted, but continues with the next statement. The if not exists check now always return true, and the important things are done regardless.

To abort a stored procedure whenever a runtime error occurs, set When SET XACT_ABORT to ON, like

ALTER PROCEDURE [dbo].[sp_storedProcedure]
	@parameter1 INT,
        @parameter2 INT
	set xact_abort on

Alternatively, you can check for errors explicitly using the error number @@error

if (@@error <> 0)
    return (@@error)

SQL union does not guarantee the order of the result set

I introduced a bug in a SQL script I wrote recently because I was unaware that SQL union did not guarantee the order of the returned result set.

The SQL stored procedure finds the primary and secondary network nodes that match a set of criteria. It looks something like:

select, n.nodeId, r.realmName
from BuildDefaults bd
join NetworkNodes n on bd.nodeId = n.nodeId
join NetworkRealms r on cn.realmId = r.realmId
where bd.typeId = @type and bd.locationId = @location
select, n.nodeId, r.realmName
from BuildDefaults bd
join NetworkNodes n on bd.nodeId = n.nodeId
join NetworkRealms r on n.realmId = r.realmId
where bd.typeId = @type and bd.locationId = @secLocation

I assumed the result returned by the first select would always be the first row of the final result set. (And the result from the second select would be the second row). In retrospect, it should have been obvious that this assumption was wrong. SQL never guarantees order unless the keyword order by is used!

To guarantee the order in a SQL union, an extra order column has to be used:

select, n.nodeId, r.realmName, 1 as nodeOrder
from BuildDefaults bd
join NetworkNodes n on bd.nodeId = n.nodeId
join NetworkRealms r on cn.realmId = r.realmId
where bd.typeId = @type and bd.locationId = @location
select, n.nodeId, r.realmName, 2 as nodeOrder
from BuildDefaults bd
join NetworkNodes n on bd.nodeId = n.nodeId
join NetworkRealms r on n.realmId = r.realmId
where bd.typeId = @type and bd.locationId = @secLocation
order by nodeOrder

Running SQLCMD in SQL Server Management Studio 2012

I normally run sqlcmd commands on the command line. However, there are others in my team who run them inside SQL Server Management Studio (SSMS). Therefore I have to make sure my scripts run within SSMS as well.

By default, SSMS doesn’t understand sqlcmd commands.

Not in SQLCMD mode

To use the Database Engine Query Editor to write or edit SQLCMD scripts, you must enable the SQLCMD scripting mode. By default, SQLCMD mode is not enabled in the Query Editor. You can enable scripting mode by clicking the SQLCMD Mode icon in the toolbar or by selecting SQLCMD Mode from the Query menu.

To use SSMS to write or edit sqlcmd scripts, sqlcmd scripting mode must be enabled before script execution.

SQLCMD mode button

However, the sqlcmd mode icon is not on the toolbar in SSMS 2012 by default. To drop the icon onto the toolbar,

  1. Open the menu Tools -> Customize -> Command, choose the SQL editor toolbar.
  2. Then under ‘Query Options’, click the ‘Add Command…’ button.
  3. Select ‘Query’ in Categories, and choose ‘SQLCMD Mode’ in the commands.

Now when you have the query window highlighted, you should be able to toggle SQLCMD mode, before running execute.

Hot deploy JSPs in Wildfly 8.2.0

Wildfly has a development mode for JSP. In development mode, the wildfly server will check for changes in JSP files in deployed applications. JSPs can therefore be edited and tested without recompiling and redeploying the entire war file. Very handy.

This is configured in ${WILDFLY_ROOT}/standalone/configuration/standalone.xml. To enable it, set the development attribute to true in the element <jsp-config>.

<subsystem xmlns="urn:jboss:domain:undertow:1.2">
      <servlet-container name="default">
                <jsp-config development="true" tag-pooling="false"/>

If the original war file is deployed with maven, the exploded application can be find in ${WILDFLY_ROOT}/standalone/tmp/vfs/temp/tempxxxxxxx/content-xxxxxxx, where xxxxxxx is a series of random numbers. The JSPs should be in the WEB-INF directory under the application root. Replacing a JSP with a newer version and changes are immediately reflected when the webpage is reloaded.

Debug remote Mule server applications in Eclipse

Normally, I debug mule applications using the Debug as Mule Application option in Anypoint Studio. However, I recently started on an existing mule project that does not run within Anypoint Studio.

I have set up my development environment using eclipse as my IDE, deploying into a mule standalone server, and debugging via Java Debug Wire Protocol (JDWP). JDWP is the protocol used for communication between a debugger and the Java VM which it debugs. In other words, it allows you to set breakpoints, step, evaluate expressions in Java applications running within the target server container, outside eclipse.

First, the mule server must be set up to run in debug mode. Open wrapper.conf and uncomment (or add) the following entries,server=y,suspend=y,address=5005

When the mule server is next started, there should be an entry in wrapper.log showing the server is running in debug mode:

Listening for transport dt_socket at address: 5005

To debug in eclipse, go to Run -> Debug Configurations. Create a new debug configuration for Remote Java Application. Set the port as 5005. Hit the Debug button and eclipse will attempt to connect to the mule server. Sometimes, eclipse does not switch to the Debug perspective automatically. To do so manually, go to Window -> Perspective -> Open Perspective -> Debug.

Initially, I made the mistake of deploying my mule applications in zipped files. When I tried to connect to the mule server within eclipse, I get the following errors

  • Hot code replace failed – Scheme change not implemented
  • Hot code replace failed – delete method not implemented

To say they are cryptic is an understatement! This error basically says the IDE cannot hot deploy code in the target VM, which is needed when you debug a remote Java application. You must use the same compiler for both the IDE and the application files running on the remote Java server. And this includes not letting the remote server to ‘explode’ an application during deployment. The simplest way to set this up is to use the maven install step to copy the application files to the mule server, and then run maven within eclipse.

To undeploy the previous installation, delete the anchor file in the mule server apps directory. Never delete a deployed application directory manually. Always let mule do the deletion. This intervenes with mule’s hot deployment layer and can lead to jar locking problems.

PS. I’m running mule standalone server 3.4 and eclipse mars 4.5.

Installing Anypoint Studio Plug-in 5.3.0 in Eclipse Mars

Installing anypoint studio should be a straight forward process, according to the official documentation on the mulesoft website. You add mulesoft to the list of available software site, select anypoint studio, click finish. Eclipse install software wizard should, theoretically, pull in required dependencies.

However, it didn’t work. I got the following exception

No repository found containing: osgi.bundle,org.eclipse.m2e.archetype.common,

I had a Java EE installation of Eclipse Mars (4.5). It already included maven support via m2e-wtp. Anypoint studio wasn’t happy with this version of m2e, and was also unable to pull in the required version of m2e itself. I had to manually add m2e’s update site and install m2e. After that, anypoint studio was installed without a problem.

Solutions to Exercises: Learning JQuery – Chapter 3 Handling Events

My solutions to exercises found in Chapter 3 Handling Events of Learning jQuery 4th Edition by Chaffer and Swedberg.

  1. When Charles Dickens is clicked, apply the selected style to it.
    $('.author').click(function() { $(this).toggleClass('selected') })
  2. When a chapter title (<h3 class=”chapter-title”>) is double-clicked, toggle the visibility of the chapter text.
    $('.chapter-title').click(function() {
  3. When the user presses the right arrow key, cycle to the next body class. The key code for the right arrow key is 39.
    var setBodyClass = function() {
    	var body = $('body');
    	if (body.hasClass('narrow')) {
    	else if (body.hasClass('large')) {
    	else {
    $(document).keyup(function(event) {
    	if (event.which == 39) {
  4. Challenge: Use the console.log() function to log the coordinates of the mouse as it moves across any paragraph. (Note: console.log() displays its results via the Firebug extension for Firefox, Safari’s Web Inspector, or the Developer Tools in Chrome or Internet Explorer).
    Not sure why this is a challenge question. The answer is in the jquery documentation for mouseover

    $('p').mouseover(function(event) {
    	console.log(event.pageX + " " + event.pageY);
  5. Challenge: Use .mousedown() and .mouseup() to track mouse events anywhere on the page. If the mouse button is released above where it was pressed, add the hidden class to all paragraphs. If it is released below where it was pressed, remove the hidden class from all paragraphs.
    var down = -1;
    $(document).mousedown(function(event) {
    	down = event.pageY;
    $(document).mouseup(function(event) {
    	if (down != -1) {
    		var up = event.pageY;
    		if (up < down) {
    		else if (up > down) {

Testing CSS selectors in Firefox

CSS selectors are used frequently when writing jquery code. Recently I discovered Firefox provides a handy syntax for testing CSS selectors:

$$('CSS selector')

In the screenshot below, I’m looking at an <h3> element with the class ‘chapter-title’ using Firefox’s web developer tools.

firefox inspector

To test if the selector ‘h3.chapter-title’ returns the expected elements, enter $$('h3.chapter-title') in the console. The command returns a nodelist with two elements, one for each match. Hovering over the individual element in the console will highlight the corresponding element in the page.

testing css selector with $$

Solutions to Exercises: Learning JQuery – Chapter 2 Selecting Elements

After finishing the excellent Object Oriented Javascript, I’m now working my way through Learning jQuery 4th Edition by Chaffer and Swedberg. Here’s my solution to exercises found in the end of Chapter 2 Selecting Elements.

  1. Add a class of special to all of the <li> elements at the second level of the nested list.
    $('#selected-plays ul li').addClass('special')
  2. Add a class of year to all the table cells in the third column of a table.
  3. Add the class special to the first table row that has the word Tragedy
    in it.

  4. Challenge: Select all the list items (<li>s) containing
    a link (<a>). Add the class afterlink to the sibling list items that follow
    the ones selected.

    $('li a').not('[href^="mailto:"]').parent().nextAll().addClass('afterlink')
  5. Challenge: Add the class tragedy to the closest ancestor <ul> of
    any .pdf link.