Tuesday, 12 November 2013

NOLOCK hint in SQL Server

I have been reading some articles and get more understanding on NOLOCK hint in SQL server. A good article I found to understand NOLOCK hint is here. However, I will now briefly summarise what I understood here for quick references.

1. NOLOCK reads uncommitted data i.e., dirty data. This is the key point that needs to be considered before using this keyword. This will determine the accuracy of the final result. Learn more about this issue, please visit  http://msdn.microsoft.com/en-us/library/ms190805.aspx

2. NOLOCK will be ignored in the DELETE, UPDATE or INSERT statement but on the table to be updated/inserted. The NOLOCK syntax on UPDATE/DELETE /INSERT will be removed in the future release of SQL Server. See examples (from the MSDN forum) below:

Example 1:

UPDATE orders
SET no_of_orderlines = (
 SELECT COUNT(*)
 FROM orderlines WITH (NOLOCK)
 WHERE orderlines.order_id = orders.order_id
)

The NOLOCK hint in the above example is okay and will be honored.

Example 2:

UPDATE orders
SET no_of_orderlines = COUNT(*)
FROM orders   O WITH (NOLOCK)
JOIN orderlines OL ON OL.order_id = O.order_id

However, the NOLOCK in the above example will be ignored as it is on the table being updated.

3. NOLOCK equals to READUNCOMMITED and they are exchangeable. Have seen people suggest that the NOLOCK keyword should be removed in the future release but keep READUNCOMMITED keyword in use only.

4. Use isolation level as shown below to apply NOLOCK on a group of statements

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; -- turn it on 
SELECT * FROM Person.Contact WHERE ContactID < 20
UPDATE Person.Contact SET Suffix = 'B' WHERE ContactID = 1
SELECT * FROM Person.Contact WHERE ContactID < 20  
SET TRANSACTION ISOLATION LEVEL READ COMMITTED; -- turn it off

5. Use READPAST hint instead of NOLOCK hint will eliminate the dirty data. However, everything else will be the same as NOLOCK. See this article for more details.


Thursday, 17 October 2013

"Joy" of using LoadableComponent in Selenium

When using LoadableComponent in my JUnit tests, found the biggest issue is the usage of load() and isLoaded() functions.

First of all, the first trip-over is the isLoaded() HAS TO be implemented. If you leave it blank, your LoadableComponent will not be loaded. There is no error or any warning. You JUnit test will just keep hanging there and waiting until timeout.

Second thing, which is still a myth to me, is if the isLoaded() method is called to access some elements are not ready on the page yet (which is logical and common and even from the official example provided on Selenium Wiki), it does not ALWAYS successful. It seems we have to force the load() method to be called (to load the element) before any isLoaded() validation kicks in.

With debugging, I found the isLoaded() method is actually called before load() method and then is called again. It sort of makes me feeling confused as with example on Selenium Wiki, it looks logical that load() method is the first method will be called upon calling the get() method of the LoadableComponent but it is not (the load() and isLoaded() methods are not exposed to use publicly anyway and we do not have any other choices).

However, my expectation on how to use these two methods are completely flipped after I found this post on the Internet, then it explained why I have been experiencing the problems above as the load() method will not be called at first and only isLoaded() method is called and then if it is not successful, it will then call load() method. However, in my case, some of my tests on isLoaded() calls are still not successful for some (unknown) reason(s) and that is still a myth.

So the official guide of using LoadableComponent is not quite correct and it sort of leads us to think the load() method is the place we put all the logic to load the page whereas isLoaded() method is the place we put the validation on checking whether the page is loaded or not. So to my impression, load() method should be the one we always implement as it is the access point of the page and will be called first. However, from the source code in that post, it seems load() method is not necessarily to be implemented whereas the isLoaded() method is one has to be implemented.

Moreover, my pages are loadable components and in dependency on other pages. So the load() method cannot be placed in the Contructor. At the end, my solution is to call my load() method as the first line in my isLoaded() method. This then solved my problems. However, I still do not think it is nice.

Tuesday, 15 October 2013

"Wait" for the element to be ready/visible in Web page when automating functional tests

When coding to automate functional tests for web applications, I find that it is always an issue when waiting for the Web element to be ready and be accessed. I think the challenge here is the waiting time is really various as it depends on the response of the browser in different environment. No one knows how long exactly an element will be available on the page for accessing.

In Selenium, we use WebDriverWait and there are many ExpectedConditions we can choose from in Java. However, this approach does not work in many occasions based on the context. It is not complete guarantee that the element would be waited till the expected the condition meets and becomes accessible. Moreover, the wait will immediately throw error if the locator of the element is evaluated as read-only at the time. So the entire wait process will be terminated.

In Coded UI, we can use UITestControl and its related wait functions to wait until the element is ready to be accessed. However, it does not work every time, either. Moreover, if using IWebElement, there are no such waiting methods available.

With Thread.Sleep() method, we could force the wait in fixed timeframe. However, it is not efficient enough and it is not 100% guaranteed that the fixed wait is long enough.

So at the end, I go for the customized way of waiting and it is not perfect, either. But it should solve most of the issues.

public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds) {
            IWebElement element = null;
            bool found = false;                      

            do {
                try {
                    element = driver.FindElement(by);                    
                    found = true;
                } catch (Exception e) {
                    throw e;
                }

                if (!found) {
                    Thread.Sleep(1000);
                }

                timeoutInSeconds--;
            } while (timeoutInSeconds > 0 && !found);
                     
            return element;
        }

Note that the parameter of timeoutInSeconds is the time of trying to wait for the element to be available but not the time has to be waited in this call. So if the element is available before the timeout, it will be returned.

Based on this method, we can then write more customized methods (if necessary) that to add conditions of waiting. I guess this approach would sort of guarantee that an element will be accessed in the most efficient way.

Monday, 16 September 2013

What is TempDB?

Working with SQL Server for many years but in recent years, performance and tuning becomes a more and more popular topic at my work. At recent TechEd Australia, I have heard over so many sessions about these topics. Within the most mentioned, it is the TempDB. The TempDB is the critical part for SQL performance as it is the most read workplace in SQL Server and therefore, the configuration and disk IO would impact how SQL Server interact TempDB thus influence the performance. Thank you for these sessions and the presenters and I have gained a new level of knowledge.

I have collected some of the links here and hoping this can be help myself and others who are in the same boat keep learning.

The Basics 

What is TempDB?
http://technet.microsoft.com/en-us/library/ms178028.aspx

Compilation of SQL Server TempDB IO Best Practices
http://blogs.msdn.com/b/cindygross/archive/2009/11/20/compilation-of-sql-server-tempdb-io-best-practices.aspx

Analyzing I/O Characteristics and Sizing Storage Systems for SQL Server Database Applications
http://msdn.microsoft.com/en-us/library/ee410782.aspx

SQL Server Performance in the Cloud, Windows Azure

Windows Azure SQL Database and SQL Server -- Performance and Scalability Compared and Contrasted
http://msdn.microsoft.com/en-us/library/windowsazure/jj879332.aspx

Other Reading

SQL IaaS Basics

SQL Server in Windows Azure Virtual Machines
http://msdn.microsoft.com/en-us/library/windowsazure/jj823132.aspx

High Availability and Disaster Recovery for SQL Server in Windows Azure Virtual Machines
http://msdn.microsoft.com/en-us/library/jj870962.aspx

Window Azure Storage

SOSP Paper - Windows Azure Storage: A Highly Available Cloud Storage Service with Strong Consistency
http://blogs.msdn.com/b/windowsazurestorage/archive/2011/11/20/windows-azure-storage-a-highly-available-cloud-storage-service-with-strong-consistency.aspx

SQL Server Backup and Restore with Windows Azure Blob Storage Service
http://msdn.microsoft.com/en-us/library/jj919148.aspx

USENIX Best Paper Award: Erasure Coding in Windows Azure Storage
http://blogs.msdn.com/b/windowsazurestorage/archive/2012/06/13/usenix-best-paper-award-erasure-coding-in-windows-azure-storage.aspx

Monday, 12 August 2013

How to delete the Windows IconCache.db file

Recently, I have experienced a problem that my application shortcut icon is not displayed anymore after an upgrade. Later, we fixed the issue by deleting the Windows IconCache.db file and it involves in stoping and starting of the Windows Explorer. The steps to delete the IconCache.db file is listed below and all operations are done via Windows Command Line:

Step 1: open command line

type cmd in search programs and files at Start menu

Step 2: Command to kill the current Windows Explorer (i.e., the desktop will become empty and without task bar afterward)

taskkill /IM explorer.exe /F

Step 3: Go to location of IconCache.db file

cd C:\Users\Admin\AppData\Local

Step 4: Command to view the IconCache.db file (it is hidden)

dir /A:H

Step 5: Delete the IconCache.db file

del IconCache.db

Step 6: Restart Windows Explorer

explorer 

Tuesday, 28 May 2013

COALESCE() vs. ISNULL()

Having been reading the differences between COALESCE() and ISNULL() functions in T-SQL. I have found many other posts talking about this in details and thought put a summary table here for quick reference check.


COALESCE()
ISNULL()
Parameters (Syntax)
Accept multiple parameters
COALESCE ( expression [ ,...n ] )

Example:
SELECT COALESCE(NULL, NULL, 'foo')
Accept only two parameters
ISNULL( check_expression , replacement_value )

Example:
SELECT ISNULL(NULL, ISNULL(NULL, 'foo'))
Data Type Returned
Returns the data type of expression with the highest data type precedence. If all expressions are nonnullable, the result is typed as nonnullable.
Returns the same type as check_expression. If a literal NULL is provided as check_expression, returns the datatype of the replacement_value. If a literal NULL is provided as check_expression and no replacement_value is provided, returns an int.
Column Selected Into
Nullable column returned
Non-nullable column returned
SQL Function Type
Standard function (recommended to use)
Non-standard function
Performance Impact
Not recommended to use in the conditions of joins
In some cases where subquery is used in the expression can generate less efficient plan. E.g.,

SELECT COALESCE
(
    (SELECT a2.au_id
        FROM pubs..authors a2
        WHERE a2.au_id = a1.au_id),
    ''
)
FROM authors a1

Not recommended to use in the conditions of joins

References

COALESCE()
http://msdn.microsoft.com/en-us/library/ms190349.aspx

ISNULL()
http://msdn.microsoft.com/en-us/library/ms184325.aspx

Should I use COALESCE() or ISNULL()?
http://databases.aspfaq.com/database/coalesce-vs-isnull-sql.html

Can I have optional parameters to my stored procedures?
http://sqlserver2000.databases.aspfaq.com/can-i-have-optional-parameters-to-my-stored-procedures.html

Wednesday, 15 May 2013

Clearing IE9 cache

Have come across an issue lately with Internet Explorer 9. There is one temporary file seems not deletable regardless of how we tried with all available functions in IE.

1. Via F12 through development tool menu option
2. Via Internet Options menu option to delete all temporary files

At the end, we had to use PowerShell command to help clear IE temporary files, which finally deleted the file for us.

get-childitem "c:\Users\USERNAME\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\*" -recurse -force | remove-item -force -recurse

However, all applications that use these temporary Internet files need to be closed at mean time. Otherwise, the command will fail.

Thursday, 9 May 2013

The myth to dynamically display page header in Crystal Report

This is one of the Crystal myths I have come across.

We have two different page headers to display based on a boolean parameter. It works perfectly until we tried to export the report to Excel (Data Only) format. One of the parameter choice will cause the corresponding header not to be exported to that format. However, it works with the opposite parameter selection.

After many different tests, I found it is not to do with the parameter choice but just the position of the header section. As we have to create two page header sections to achieve this, it will always be the one at bottom section gets exported.

With some Internet search, I have eliminate the following common causes of issues to export to Excel (Data Only) format:

1. different fields width and height
2. different alignment with the Detail fields
3. uncheck Simplify page header option in the Excel(Data Only) format export options

At the end, I tried from a mew blank report just had single same field in each header. Use the same boolean parameter to show/hide one and another. It still did not work.

Concluded it is a bug and now, I have to think of a work around.

So I have tried to place these two headers together in one page header and use Supress formula on fields to do show/hide. Found if placing them in parallel, it still will not work until to place them at one line i.e., overlap each other. However, the fields exported to Excel (Data Only) are having spaces in between as the difference between these two headers.

So we had to just use one header and show and hide the extra coulmn(s) which will leave space if it is not show and hopefully, someone else can find the better solution on this.

Tuesday, 7 May 2013

Be Cloud-ready

It has been several years that since Cloud Computing entered into ICT world. It is an exciting change that would affect the way we work. Personally, as a technical professional, it is an exciting experience to look forward to.

However, there are many to learn and to be Cloud-ready. Lately, I have come across CloudU and a very good site to start my Cloud Computing learning journey...

Six Steps That Simplify Moving to the Cloud

http://www.itbusinessedge.com/slideshows/six-steps-that-simplify-moving-to-the-cloud.html

CloudU to learn more

http://www.rackspace.com/knowledge_center/cloudu/curriculum