Posts

Singleton - Double locking mechanism

Let us see the Singleton code which we use generally to create a singleton instance: public static synchronized Singleton getInstance() { if (instance == null) //1 instance = new Singleton(); //2 return instance; //3 } The code in Listing 2 works fine for multithreaded access to the getInstance() method. However, when you analyze it you realize that synchronization is required only for the first invocation of the method. Subsequent invocations do not require synchronization because the first invocation is the only invocation that executes the code at //2, which is the only line that requires synchronization. All other invocations determine that instance is non-null and return it. Multiple threads can safely execute concurrently on all invocations except the first. However, because the method is synchronized, you pay the cost of synchronization for every invocation of the method, even though it is only required on the first invocation. In an effort to mak...

How to use Derby database

Well, I directly jump into the subject matter instead of taking you through its history and detail; because you may be already aware or if not you can google it. Please follow these following steps:- 1. Download the Apache Derby binary distribution (e.g., v10.6) from the Apache Derby web site ( http://db.apache.org/derby/ ) and extract it to a directory of your choice to complete the installation. Say for instance you have extracted it into 'c:\derby' folder. 2. Set ‘DERBY_INSTALL’ as user environment variable in you computer first. 3. Go to the physical location of ‘DERBY_INSTALL’ .and start the bat file ‘C:\derby\bin\startNetworkServer.bat’ by double clicking it . This will start the database server. Minimize this started command prompt. 4. Now, open a new command prompt 5. set CLASSPATH=%DERBY_INSTALL%\lib\derby.jar;%DERBY_INSTALL%\lib\derbytools.jar;%CLASSPATH% 6. To run the sysinfo utility, type the following in a command window or shell: java org.apache.derby.tools.sysin...

How to see what ports are open in my computer

Image
To find out what ports are open or exposed, follow these steps: Start >Run >type "cmd" At the command line type " netstat -a > c:\portsStatus.txt " {enter) This will create a file called portsStatus.txt in c: drive. Open this file, you will get the list of each ports in your computer. Interesting, eh ? The list displayed shows "Listening ports" and established "Who is on the other end" connections to your computer. Also, Who is listening: netstat -an |find /i "listening" Who is established: netstat -an |find /i "established" Also, if yourcomputer's operating system is XP and want to see what are the open ports at this mements, then type:-> netstat -ao |find /i "listening" This will give the detail against some Process Ids (PID)s. Till now you have identified the list of ports that are in use at this moment. And somehow you smelled that one of the port is in use which really should not be (this may be ...

Spring

   Today we all know that Spring is being widely used in enterprise applications because of its easy of development and layered architecture.  Which enable us to integrate its various layers with other technologies and API(s), like - JMS, MVC, Junit, EJB, Hibernate and so on.   This framework is very popular because of  its great feature of Dependency Injection (DI). Let us look a simple example to understand DI:- I have a class A (singleton class) that has some parameters that sets at start up time. Also. I have a class B (prototype) that needs the parameters in class A. Now, I am trying to figure out how to get the instance of A which is to be use in B later sometime. class A { //singleton class       String a; } class B {      //prototype class      A a;     String b;     public B createB() {  //for method lookup          return null; ...

XML Bean

XMLBeans is an XML parser developed by BEA for WebLogic 8.1 and recently released as an open source initiative. With XMLBeans, you can access and manipulate the data contained in an XML document using Java classes. XMLBeans is different from the typical XML parser because it is designed to build applications based on known, specific schemas. At its core is a schema compiler that parses an XSD file and creates a JAR. The JAR contains a bunch of Java classes that developers can use to manipulate instance documents as if they were normal Java types. XMLBeans gives you an object-based view of the underlying XML data, without losing access to the original XML structure. In addition, it gives you an XQuery parser and XMLCursor objects that allow you to query your documents with the flexibility of a SQL database. EXAMPLE : CASE Study STEP 1: - -Open a DOS command prompt and run the 'scomp' command. If it runs without error, then congratulations, you have successfully set up XMLBea...

Hibernate

Interview Questions: http://www.developersbook.com/hibernate/interview-questions/hibernate-interview-questions-faqs-2.php

Java

Image
Some Methods syntax needs to be remembered: protected Object readResolve() { return getInstance(); } protected void finalize() throws Throwable { try { close(); // close open files } finally { super.finalize (); // This is mandatory to write } } //normally it should be overridden to clean-up non-Java resources ie closing a file //in singleton classs public Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException(); } } –pleae note that this method is present in Object class not in Cloneable Interface as it is marker interface MultiThreading means: thread-safe = true. Overloading is providing multiple methods with the same name but different arguments (and potentially return type). Overriding is changing the behavior of a method in a subclass. Only objects are instantiated in the Java Runtime Environment (JRE) and held in a division of the program's memory allocation called the heap. S...