Tuesday, December 27, 2011

Java pass by reference or pass by value

Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value.

Take the badSwap() method for example:

public void badSwap(int var1, int var2) {   int temp = var1;   var1 = var2;   var2 = temp; } 

When badSwap() returns, the variables passed as arguments will still hold their original values. The method will also fail if we change the arguments type from int to Object, since Java passes object references by value as well. Now, here is where it gets tricky:

public void tricky(Point arg1, Point arg2) {   arg1.x = 100;   arg1.y = 100;   Point temp = arg1;   arg1 = arg2;   arg2 = temp; } public static void main(String [] args) {   Point pnt1 = new Point(0,0);   Point pnt2 = new Point(0,0);   System.out.println("X: " + pnt1.x + " Y: " +pnt1.y);    System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);   System.out.println(" ");   tricky(pnt1,pnt2);   System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);    System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);   } 

If we execute this main() method, we see the following output:

X: 0 Y: 0 X: 0 Y: 0 X: 100 Y: 100 X: 0 Y: 0 

The method successfully alters the value of pnt1, even though it is passed by value; however, a swap of pnt1 and pnt2 fails! This is the major source of confusion. In the main() method, pnt1 and pnt2 are nothing more than object references. When you pass pnt1 and pnt2 to the tricky() method, Java passes the references by value just like any other parameter. This means the references passed to the method are actually copies of the original references. Figure 1 below shows two references pointing to the same object after Java passes an object to a method.

Figure 1. After being passed to a method, an object will have at least two references

Java copies and passes the reference by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects. But since the references are copies, swaps will fail. As Figure 2 illustrates, the method references swap, but not the original references. Unfortunately, after a method call, you are left with only the unswapped original references. For a swap to succeed outside of the method call, we need to swap the original references, not the copies.

Figure 2. Only the method references are swapped, not the original ones

Monday, December 19, 2011

SQL Server 2000 - Replication Step by Step Procedure


Replication is the process of sharing data between databases in different locations. Using replication, we can create copies of the database and share the copy with different users so that they can make changes to their local copy ofdatabase and later synchronize the changes to the source database.

Microsoft SQL Server replication uses publisherdistributor and subscriber entities.

Publisher is a server that makes the data available for subscription to other servers. In addition to that, publisher also identifies what data has changed at the subscriber during the synchronizing process. Publisher containspublication(s).

Subscriber is a server that receives and maintains the published data. Modifications to the data at subscriber can be propagated back to the publisher.

Distributor is the server that manages the flow of data through the replication system. Two types of distributors are present, one is remote distributor and the other one local distributor. Remote distributor is separate from publisher and is configured as distributor for replication. Local distributor is a server that is configured as publisher and distributor.

Agents are the processes that are responsible for copying and distributing data between publisher and subscriber. There are different types of agents supporting different types of replication.

An article can be any database object, like Tables (Column filtered or Row filtered), Views, Indexed views, Stored Procedures, and User defined functions.

Publication is a collection of articles.

Subscription is a request for copy of data or database objects to be replicated.

Types of Subscription:

Changes to the subscriptions at the publisher can be replicated to subscribers via PUSH subscription or PULL subscription. With Push subscription, the publisher is responsible for synchronizing all the changes to the subscriber without subscriber asking for those changes. With Pull subscription, the subscriber initiates the replication instead of the publisher.

Replication Types

Microsoft SQL Server 2000 supports the following types of replication:

  • Snapshot Replication
  • Transactional Replication
  • Merge Replication

Snapshot Replication

  • Snapshot replication is also known as static replication. Snapshot replication copies and distributes data anddatabase objects exactly as they appear at the current moment in time.
  • Subscribers are updated with complete modified data and not by individual transactions, and are not continuous in nature.
  • This type is mostly used when the amount of data to be replicated is small and data/DB objects are static or does not change frequently.

Transactional Replication

  • Transactional replication is also known as dynamic replication. In transactional replication, modifications to the publication at the publisher are propagated to the subscriber incrementally.
  • Publisher and the subscriber are always in synchronization and should always be connected.
  • This type is mostly used when subscribers always need the latest data for processing.

Merge replication

It allows making autonomous changes to replicated data on the Publisher and on the Subscriber. With merge replication, SQL Server captures all incremental data changes in the source and in the target databases, and reconciles conflicts according to rules you configure or using a custom resolver you create. Merge replication is best used when you want to support autonomous changes on the replicated data on the Publisher and on the Subscriber.

Replication agents involved in merge replication are snapshot agent and merge agent.

Implement merge replication if, changes are made constantly at the publisher and subscribing servers, and must be merged in the end.

By default, the publisher wins all conflicts that it has with subscribers because it has the highest priority. Conflict resolver can be customized.

Necessary steps to be taken before doing replication process:

  1. Before starting the replication process, change the log on account for the MSSQLSERVER service as �This account�. Use any SQL login account which is a member of sysadmin server role. Please see the screenshot for more information. Don�t forget to restart the MSSQLSERVER service.

  2. Adequate disk space should be allocated for publisher, distribution and subscriber�s databases.
  3. Use NOT FOR REPLICATION option when defining Identity columns.

Step by Step Procedure for Merge Replication setup

  1. Open SQL Server Enterprise Manager and select Tools menu -> Replication -> Configure Publishing, Subscribers, and Distribution�
    1. Configure the appropriate server as publisher or distributor.

    2. Enable the appropriate database for merge replication.

    3. Enable the appropriate server as subscriber.

  2. Open SQL Server Enterprise Manager and select the appropriate SQL Server Group for which replication needs to be done, then select Tools menu -> Replication -> Create and Manage Publications.

  3. This will open a dialog box for �Create and Manage Publications on respective server�. Select the appropriatedatabase and then click �Create Publication�. This will open �Create Publication Wizard�. Just click Next.

  4. It will ask to choose a Distributor for the selected server. Select �Make Server its own Distributor; SQLServer will create a distribution database and a log�. Then click Next.

  5. It will ask for the Snapshot folder path. Browse and select the appropriate path for Snapshot folder and then click Next.

    Note: Create one folder in the Publisher machine and share the folder, then give full permissions for the user through which you logged in. Make sure that you are able to access this folder from the Subscriber machine also. If you are not able to access, give full permissions to that shared folder for the appropriate user in the Publisher machine. The Snapshot folder should be in the Publisher machine.

  6. Choose the database which you want to publish and Click Next.

  7. Select the Publication Type as �Merge Publication�.

  8. Specify the Subscriber Types. Select �Servers running SQL Server 2000�. Then click Next.

  9. Select the Object Types (like Tables, Stored Procedures and Views) which you want to publish, and click Next.

  10. It will show some issues which may require some changes at later stages in order to work as expected. Just click Next.

  11. Give Publication Name and click Next.

  12. It will ask to customize the properties of the Publication. Select �Yes, I will define data filters, enable anonymous subscriptions, or customize other properties�. Then click Next.

  13. Then, it will ask �How do you want to filter this publication?� Don�t select any thing here. Just click Next.

  14. Then, it will ask �Whether you want to allow anonymous subscription to this publication?�. Select �No, allow only named subscriptions�, and click Next.

  15. It will show �Set Snapshot Agent Schedule� dialog box. Change the Snapshot Agent Schedule as per your requirement, then select �Create the first snapshot immediately�. And click Next.

  16. Click Finish to create a Publication.

  17. Finally, it will show �SQL Server Enterprise Manager successfully created publication �pub1� from database�db1�. Just click Close.

  18. It will show the dialog box �Create and Manage Publications on respective Server�. Now go to the respective created Publication and click �Push New Subscription�.

  19. Before doing �Push New Subscription�, create new SQL Server Registration for Subscriber machine in the Publisher machine�s SQL Server Enterprise manager with SQL Authentication mode. For this, there should be one common SQL login name in both Publisher and Subscriber machines. Set server roles for this user as System Administrator, Process Administrator and Bulk Insert Administrators, and give database access to the respective database for which you want to perform replication.
  20. Go to �Push New Subscription� wizard. This will open �Push Subscription Wizard�. Just Click Next.

  21. Choose one or more subscribers from Enabled Subscribers and click Next. (Note: It will show the Subscriber�sSQL Server name under Enable Subscribers only if you do step 19.)

  22. Choose Subscription (destination) database name by browsing and clicking Next. (Note: You can create newdatabase if you want by clicking Create New).

  23. �Set Merge Agent Schedule�. Change the Schedule as per your requirement and click Next.

  24. Specify whether the Subscription(s) needs to be initialized or not. Select �Yes, initialize the schema and data� as well as select �Start the Snapshot Agent to begin the initialization process immediately�, and click Next.

  25. �Set Subscription Priority� as �Use the Publisher as a proxy for the Subscriber when resolving conflicts�, and click Next.

  26. It will show the status of the SQLSERVERAGENT service as running. Just click Next.

  27. Click Finish to complete the Push Subscription.

  28. Finally, it will show �Subscriptions were created successfully at the following Subscribers:�. Just click Close.

  29. Now, in the SQL Server Enterprise Manager, go to the appropriate SQL Server Group and go to �Replication Monitor -> Publishers -> Respective Server -> Publication Name�. In the right pane, you will see the snapshot agent. Just right click and select �Start Agent�. Refresh it once. Then right click on the respective publication name and select �Start Synchronizing�. It will merge the necessary data. Refresh it once.

Important Note:

SQL Server 2000 replication will not support full-text indexing. But, enable full-text indexing at the subscriber machine manually. This can be done by Full-text indexing wizard. Select the appropriate table and enable the required fields in that table as full-text indexed. Then, create a new catalog or else use the existing catalog and schedule it, if needed. Once this is done, go to that particular catalog and right click and select �Start full population�. The status will be displayed as �population in progress�.

Advantages in Replication:

Users can avail the following advantages by using replication process:

  • Users working in different geographic locations can work with their local copy of data thus allowing greater autonomy.
  • Database replication can also supplement your disaster-recovery plans by duplicating the data from a localdatabase server to a remote database server. If the primary server fails, your applications can switch to the replicated copy of the data and continue operations.
  • You can automatically back up a database by keeping a replica on a different computer. Unlike traditional backup methods that prevent users from getting access to a database during backup, replication allows you to continue making changes online.
  • You can replicate a database on additional network servers and reassign users to balance the loads across those servers. You can also give users who need constant access to a database their own replica, thereby reducing the total network traffic.
  • Database-replication logs the selected database transactions to a set of internal replication-management tables, which can then be synchronized to the source databaseDatabase replication is different from file replication, which essentially copies files.

Replication Performance Tuning Tips:

  • By distributing partitions of data to different Subscribers.
  • When running SQL Server replication on a dedicated server, consider setting the minimum memory amount forSQL Server to use from the default value of 0 to a value closer to what SQL Server normally uses.
  • Don�t publish more data than you need. Try to use Row filter and Column filter options wherever possible as explained above.
  • Avoid creating triggers on tables that contain subscribed data.
  • Applications that are updated frequently are not good candidates for database replication.
  • For best performance, avoid replicating columns in your publications that include TEXTNTEXT or IMAGE data types.


In a nutshell, replication is the capability to reliably duplicate data from a source database to one or more destinationdatabases. SQL Server 2000 gives you the power for replication design, implementation, monitoring, and administration. This gives you the functionality and flexibility needed for distributing copy of data and maintaining data consistency among the distributed. You can automatically distribute data from one SQL Server to many different SQLServers through ODBC (Open Database Connectivity) or OLE DB. SQL Server replication provides update replication capabilities such as Immediate Updating Subscribers and merges replication. With all the new enhancements to SQLServer replication, the number of possible applications and business scenarios is mind-boggling.

Wednesday, December 14, 2011

How to Change the Default Firefox Search Engine

Step 1

Type about:config into the Firefox address bar.  This will take you to the config warning page that says "This might void your warranty."  Take this warning label with a grain of salt, since Firefox doesn't have a warranty.  However, this is the advanced system settings area of Firefox, so don't go messing around with stuff unless you're a groovy expert.

With that said, Click the "I'll be careful, I promise!" button.

about:config firefox 4

Step 2

In the Filter box, Type in keyword.URL
Under Preference Name, keyworld.URL should pop up.  Right-Click it and Select Modify.

modify firefox 4 keyword.url to default provider

Step 3

The default URL is likely Bing, just erase it.  Now all you need to do is Paste in the following URL that matches the search engine you would like the bar to use.

You can also use a custom search engine of your choice, you'll just need to get the correct URL code for it.

Once entered, Click the OK button.

insert firefox 4 keywrod.url


Changes should take effect immediately, to test it out open a new window (Ctrl+N) and type a search query into the address bar.  If it performs a search on the search engine that you specified then you can close your about:config window and call it groovy.  Now you're using a search engine that you prefer, and you won't be bothered by the default one.
In my example, I changed my Firefox 4 default search provider to Google.  Bing has it's uses, but I find that it just doesn't compare to Google in terms of speedy, relevant, search results.

Wednesday, November 9, 2011

Switch different JDK versions in Windows

java.pngBeing a Java developer, I always compile and test my code on different Java versions. But switching between them is a huge problem. So finally I found an easy method to do this. You have to create following batch files and place them in directory you open your command line in or in SYSTEM PATH. You can use you favorite text editor to create these files.


@echo off echo Setting JAVA_HOME set JAVA_HOME=C:\j2sdk1.4.2_12 echo setting PATH set PATH=C:\j2sdk1.4.2_12\bin;%PATH% echo Display java version java -version 


@echo off echo Setting JAVA_HOME set JAVA_HOME=C:\Program Files\Java\jdk1.5.0_12 echo setting PATH set PATH=C:\Program Files\Java\jdk1.5.0_12\bin;%PATH% echo Display java version java -version 


@echo off echo Setting JAVA_HOME set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_11 echo setting PATH set PATH=C:\Program Files\Java\jdk1.6.0_11\bin;%PATH% echo Display java version java -version 

Make sure you assign the appropriate JAVA_HOME value in batch files, according to your Java installation. Whenever you want to switch between Java versions, just run the respective batch file and you are done.

Note- JAVA_HOME and the path to java must always refer to the exact same version of the JDK. If you mix them up, unpredictable things will happen!

Tuesday, October 4, 2011

Scheduling automated backup using SQL server

It is very important to take backups for the database files on regular basis. Microsoft SQL server 2008 made this task very easy. In this blog, I am going through step by step that will allow

  • Users to schedule the backup to be taken on a particular interval
  • Delete the backup copies after a certain period of time

Schedule the database backup

First I am going to tell you the steps required to schedule the backup. Login to Sql Management studio and connect to the required database. Now from the object explorer, make sure SQL server agent is running, if not start SQL server agent(Right click and press start).



Expand the Management Node from the object explorer, and then select the maintenance plan node. To schedule maintenance plan, you need to have "SYSADMIN" database role. If you dont see the maintenance node, make sure you have the necessary permission. 



Right click the maintenance plan and then select "new maintenance plan".


Enter the maintenance plan name in the popup box (This can be any name that identifies your task for ). This will identify your backup plan and you should choose a relevant name that suits your plan.




Now you will be in the configuration page for the maintenance plan. . Note the marked area, these are the two areas you need to use for setting up the maintenance plan. The marked area in the right top will be used to configure the time that the plan executes. Choose a time so that the database is least used. The bottom left pane shows the tasks that can be utilized to create an sql maintenance plan. since explaining all of them is not in the scope of this document, I am going to explore only two of them.



Click on the calendar item shown in the right side top. This will bring the job schedule properties popup window that configure the execution time/frequency of the tasks. Configure the data carefully so that it suits your requirement. Usually database backups are taken daily basis. Make sure you are selecting proper time so that your database is least used. Click ok once you finish.



From the maintenance plan tasks pane in the left side, select the backup database plan, this will be used to take backups for the databases. Drag and drop backup database task to the right side(as shown in the diagram).




Double click on the backup database task, it will open up a new window that allows you to configure the database configuration for the backup. Here you configure the databases that you need to backup, then specify a location for the backup, specify the extension for the backup files etc.

From the pop up modal window, by clicking on "Databases" dropdown, you will be able to select the required databases. Also configure the file location, extension for the backup file etc.




Click ok once finished. Now backup plan configuration is over. The backup files will be created on the scheduled time to the mentioned folder. The name of the file will be created by appending the date so that you can identify the back up for a particular date.

Since the backup files are created frequently,… it is a good practice that you delete backup files after a certain period of time. For this you need to execute clean up task  along with the maintenance plan. You can configure the clean up task as follows.

From the left side pane, drag and drop maintenance cleanup task.



Double click on the dropped item inorder to edit the clean up properties. Here you need to specify the backup location, and file extension for the back up files and specify the age of the file. It is a good practice that you keep one month old data, and delete anything prior to one month. 



Once you click ok, then save the maintenance plan. You can either wait till the next execution time or execute it manually inorder to check whether everything is working fine.

Wednesday, September 28, 2011

SQL SERVER – Find Last Day of Any Month

Following script demonstrates the script to find last day of previous, current and next month.

----Last Day of Previous Month
----Last Day of Current Month
----Last Day of Next Month

2007-07-31 23:59:59.000

2007-08-31 23:59:59.000

2007-09-30 23:59:59.000

If you want to find last day of month of any day specified use following script.
--Last Day of Any Month and Year
SET @dtDate '8/18/2007'

2007-08-31 23:59:59.000

Monday, September 12, 2011

How to use a driver ODBC 32 BITS under the Window 2003(64 bits)?

  • You have a Windows 2003 server (64-bits) and you want to use a 32 bits ODBC driver: one that is usually found in the administrative panel for ODBC on Windows or an external ODBC driver
  • Well if you open the board of directors of HBO Windows 2003 server 64, you may not find most of your usual drivers.
  • Similarly if you install an ODBC driver external 32-bit installation will go well but you probably do not see in the panel.
  • However if you open the administrative panel ODBC under Windows 2003 (64 bits), you may not find all your usual drivers.
  • Note that you may not find external drivers (32 bits) in the panel even it gets installed on your computer.
  • In fact, the boards of directors of the pilots 64-bit and 32 bit are separated on this version of Windows. One you find through administrative tools, the panel for 64-bit
  • The administrative panels for 64 bits and 32 bits drivers are separate on the windows. In the windows 2003 (64 bits), you will find the panel for ODBC 64 bits.
  • To access the panel 32 bits drivers, your should run the following command :
    •   c:\windows\syswow64\odbcad32.exe

32-Bit ODBC Drivers in Windows Server 2008 R2

Being a database guy, one of the issues I noticed right away was that the ODBC Data Source Administrator accessible via Control Panel / Administrative Tools is the 64-bit version and can only be used to setup connections for 64-bit ODBC drivers. Not only was the 64-bit version missing the Postgres driver I had just installed, there were no drivers at all other than SQL Server…

It turns out that there are are two, entirely identical ODBC tools and the one that most of us will end up using initially (unless we're lucky enough to have an all 64-bit architecture) is kept in the basement that is the c:\Windows folder. The 32-bit ODBC Data Source Administrator can be found by going to the Start Menu, selecting Run and executing c:\Windows\SysWOW64\odbcad32.exe as follows…

Once launched, you'll see a tool that appears to be identical in every way, except that the 'missing' ODBC drivers are now available…

Please note that you can't run both 32-bit and 64-bit tools at the same time so please make sure you close the 64-bit one first, it's also worth bearing in mind that if you're running 32-bit applications they will probably be looking for 32-bit DSNs so even if you can get a 64-bit driver for your data source it doesn't mean that it's the right thing to do.