Wednesday, December 25, 2013

How to redirect a page or URL using JavaScript and JQuery

Redirecting a web page means, taking user to new location. Many website use redirect for many different reasons, e.g. Some website redirect user from old domain to new domain, some redirect from one page to another e.g. a more relevant page. If you are Java programmer, and worked previously with Servlet and JSP, then you might be familiar with SendRedirect and Forward methods, which are used to redirect user in web applications. Actually there are two kinds of redirect, Server side redirect and client side redirect. In Server side redirect, server initiate redirection, while in client side redirect, client code does the redirection. Redirecting a web page using JavaScript and JQuery is a client side redirection. Well, HTTP redirection is a big topic and different people do it differently. e.g. bloggers mostly used platform like WordPress, Blogger feature to redirect a page, though this might be restricted to same domain. In this JavaScript and JQuery tutorial, we will learn a new JQuery tip to redirect a page.
Read more �

Monday, December 23, 2013

Restore master database with Dell NetVault Litespeed for SQL Server




The DBA teams asked if I could create a job in our maintenance routines to do a native backup of the master database instead of a Litespeed backup. Their reason was they weren't able to restore master using Litespeed because the database server had to be in single-user mode to do such a restore and Litespeed required two connections to the server to do the task.





I mentioned that there are instructions in the Litespeed help files for a master database restore.





The team told me that it didn't work for them. So, I took the task to validate the instructions found here https://support.quest.com/search/SolutionDetail.aspx?id=SOL13484




I took a backup of the master database at 9:44 AM.






























Next, I created a login on the server after the backup called ThisLoginWillNotBeHereAfterMasterDatabaseRestore to prove the master database restore did in fact happen.


This login will no longer exist after the upcoming master database restore because it was not captured by the backup taken at 9:44 AM.













































































I stopped the SQL Server services for the instance.  Either SSMS or SQL Configuration Manager can be used to stop the services.  When you stop the database instance, the corresponding SQL Agent will also be stopped.







I started the database instance in single user mode in one command window.


























































The instance has started in single user mode.




















































I open a second command window and ran the database restore command.


























































The restore runs successfully in seconds.














































Additional messages are written to the first command window and the command prompt returns.

I restart the database instance using the SQL Configuration Manager.  


I start SQL Server Management Studio to find the login created after the backup no longer exists proving the master database has been restored.


































































The database restore was completed in a few seconds. The entire process probably takes less than 5 minutes.  

I took a bit longer because I was taking screenshots. ;-)





Thursday, December 19, 2013

JUnit Testing Tips - Constructor is Called Before Executing Test Methods

Most of Java programmers either use JUnit or TestNG for there unit testing need, along with some mock object generation libraries e.g. Mockito, but not everyone spend time and effort to learn subtle details of these testing libraries, at least not in proportion of any popular framework e.g. Spring or Hibernate. In this blog post, I am sharing one of such details, which has puzzled me couple of years ago. At that time, though I had been using JUnit for significant time, I wasn't aware that code written inside constructor of Test class is executed before each test method. This behaviour of JUnit has caused, some of my test to failed and putting hours of investigation in my code, without realizing that this is happening because of JUnit is initializing object by calling constructor before executing test method annotated with @Test annotation. I had following code to test my vending machine implementation as coding exercise. If you look at closely, I have initialized vending machine in class body, which is executed as part of constructor. I was assuming one instance of vending machine is shared between all test methods, as I was not using @Before and @After, JUnit 4 annotation for setup() and tearDown(). In first test, one item from Inventory is consumed and in second test another item, but when you assert count of items based upon previous test, it will fail, because you are testing a different vending machine instance, which has different inventory. So always, remember that JUnit calls constructor of test class before executing test method. You can verify it by putting a System.out.println message in constructor itself.
Read more �

Sunday, December 15, 2013

Typing master


Use TypingMaster Touch Typing Tutor to Double Your Typing Speed. Fast, Personalized Training. Download Free Trial!



http://www.typingmaster.com/fr/pro/demo.asp

Thursday, December 12, 2013

How to configure Log4j in Java program without XML or Properties File

Sometime configuring Log4j using XML or properties file looks annoying, especially if your program not able to find them because of some classpath issues, wouldn't it be nice if you can configure and use Log4j without using any configuration file e.g. XML or properties. Well, Log4j people has thought about it and they provide a BasicConfigurator class to configure log4j programmatically, thought this is not as rich as there XML and properties file version is, but it's really handy for quickly incorporating Log4j in your Java program. One of the reason programmer prefer to use System.out.println() over Log4j, of-course for testing purpose, because it doesn't require any configuration, you can just use it, without bothering about XML or properties file configuration, but most programmer will agree that, they would prefer to use Log4j over println statements, even for test programs if it's easy to set them up. By the way, for production use prefer SLF4J over Log4J. In this Java tutorial, we will see a nice little tip to use Log4j right away by configuring in couple of lines. In fact you can configure Log4J in just one line, if you intend to use there basic configuration which set's the log level as DEBUG.
Read more �

Tuesday, December 10, 2013

There is more than one way to find database backup files

I was told during the initial implementation of Ola Hallengren's Maintenance Solution at my workplace that it was too hard to find database backup files in the default folder structure his solution creates.  The concern was that it took too long to find the oldest file or the biggest file when trying to resolve a space issue on a database server.



Ola's solution creates a folder tree with these elements. Drive:\root\ServerName\DatabaseName\TypeOfBackup



You specify the backup drive and root folder using the Directory parameter of the DatabaseBackup stored procedure.


When I originally solved this problem, Windows XP was in use.  I wish I had spent more time figuring this out because I had to modify Ola's database backup procedure to dump all the backups into one folder in order for the DBA teams to sign off on the initial deployment.


It only takes a couple of minutes to search using Windows Explorer in Windows XP but finding files in a folder tree has gotten much easier on Windows 7 and higher.  



Given Windows XP's imminent retirement, I will only cover Windows 7 or higher.










GUI

Start Windows Explorer and navigate to the backup root folder on the server.


Type *.bak in the Search field in the upper right hand corner of Windows Explorer on Windows 7 or 8 and press Enter.


By default, Windows Explorer in Windows 7 and higher searches sub-folders.




The Search result returns all the database backups
in the entire folder tree.


The two requirements given were finding files by size or date.



Both are easily solved by clicking on the appropriate header. 

In the search result, headers are clickable and allow sorting by any column. 

So, click on Date Modified to find files by date or click on Size to find files by size.

Finding files to delete by size or date is done in less than a minute.

You can even save searches now.

http://www.howtogeek.com/howto/5316/how-to-save-searches-in-windows-7/








Microsoft's GUI interfaces are sufficient if you only have a few servers to check. But GUIs don't scale when you need to do the same task on a few hundred servers. So, you'll need to do a bit of scripting.  


PowerShell

Google answered this question pretty quickly.


Find the ten largest files in a directory.

http://stackoverflow.com/questions/798040/find-the-10-largest-files-within-a-directory-structure



get-childitem -path  C:\Backup -recurse | ?{ -not $_.PSIsContainer } | sort-object Length -desc | select-object fullname -f 10

gci C:\Backup -r |  ?{ -not $_.PSIsContainer } | sort Length -desc | select fullname -f 10



Finding the oldest ten files in a folder tree by date

get-childitem C:\Backup  -recurse | ?{ -not $_.PSIsContainer } | sort-object LastWriteTime | select-object fullname -f 10

gci C:\Backup  -r | ?{ -not $_.PSIsContainer } | sort LastWriteTime | select fullname -f 10

gci \\ServerName\FolderName\dump_data  -r |  ?{ -not $_.PSIsContainer } | sort Length -desc | select fullname -f 10



Including ?{ -not $_.PSIsContainer } in the pipeline ensures only files not folders are included in the results. 



If you want to run these commands on multiple servers, wrap them in a foreach loop that reads a list of text files from a server. Check out my PowerShell posts for examples. 



Old School

If you are not comfortable with PowerShell, the venerable dir command from the Windows command line is still available.



Oldest files first in a folder tree

dir /S /OD | more


Files ordered by size largest first in folder tree

dir /S /O-S | more



So, it doesn't take very long to find the largest or oldest database backup using any of these methods.  Which means I didn't need to modify Ola's scripts for the next release. ;-)




Wednesday, December 4, 2013

When to make a method final in Java

Final keyword in Java is not as mysterious as volatile or transient, but still it creates lot of doubts on programmers mind. I often receives questions like, When to make a method final in Java or When to make a method static in Java, later I answered in my earlier post. Questions like this is not trivial, knowing what a keyword does is just small part of mastering that functionality. Similar to real world, where knowing that a sword can cut lot of things is not enough for a warrior to survive, you need to know how to use that and more importantly, use if effectively. Final keyword can be applied to class, methods and variable and has different meaning for each of them, but the motive remains same, it state completeness, it opposes change. For example, final class can not be extended, value of final variable can not be change and a final method can not be overridden in Java. This gives you first hint, when to make a method final in Java, obviously to prevent subclass for changing it's definition, technical preventing it from overridden. There are lot of clues on How to use final keyword on methods in Java API itself, one of the prime example of this java.lang.Object class, which declares a lot of method final including wait method with timeout. Another example of making a method final is template method, which outlines algorithm in Template method design pattern. Since, you don't want a sub class to change the outline of algorithm, making it final will prevent any accidental or malicious overriding. In this tutorial, we will learn few things, which will help you to effectively use final keywords with Java methods.
Read more �