12 March 2012

Difference between CHAR and NCHAR in Oracle

I'd like to share you with some piece of info I found while searching about the difference between CHAR and NCHAR in Oracle and why Oracle DB has two Charsets

This info I got from SO here. and Here's it:

The NVARCHAR2 datatype was introduced by Oracle for databases that want to use Unicode for some columns while keeping another character set for the rest of the database (which uses VARCHAR2). The NVARCHAR2 is a Unicode-only datatype.
One reason you want to use NVARHCAR2 might be that your DB uses a non-Unicode character and you still want to be able to store Unicode data for some columns. Another reason might be that you want to use two Unicode character set (AL32UTF8 for data that comes mostly from western Europe, AL16UTF16 for data that comes mostly from Asia for example) because different character sets won't store the same data equally efficiently.
Both columns in your example (Unicode VARCHAR2(10 CHAR) and NVARCHAR2(10)) would be able to store the same data, however the byte storage will be different. Some strings may be stored more efficiently in one or the other.
Note also that some features won't work with NVARCHAR2, see this SO question:
Oracle Text will not work with NVARCHAR2. What else might be unavailable?



19 February 2012

13 February 2012

Java normalize spaces

How work with XSLT should know about "normalize spaces" see : XPath normalize-space() doesn't just trim!

To accomplish this in Java, use:
"My      non-normalized spaces   string   ".replaceAll("\\s+", " ")

Source: http://stackoverflow.com/questions/5472450/how-to-normalize-polish-a-text-in-java#5472516

09 February 2012

Strange Java syntax (for me at least)

I've more over 4 years working with Java and today I've seen some piece of code that I thought at first glance it is invalid Java code. The code is:
List<String> interests = new ArrayList<String>() {{
            add("Java");
            add("C#");
        }};
Although, the following code is familiar to me:
List<String> interests = new ArrayList<String>() {
            @Override
            public void add(int index, String element) {
                // TODO Auto-generated method stub
                super.add(index, element);
            }
        };
Here I've defined a reference named "interests" of type "subclass to ArrayList" and I am overriding the "add" method in this subclass. So, I've re-looked at the first snippet again and could realize it to be:
List<String> interests = new ArrayList<String>() {
        
            // Anonymous initialization block (vs static init block)
            {
                add("Java");
                add("C#");
            }
        };
As if I defined a class the following way:
public class MyClass {

    {
        doSomeThing();
        doSomeThing();
    }

    void doSomeThing() {
        System.out.println("doing");
    }

    public static void main(String[] args) {
        MyClass c = new MyClass();
    }
}

Which Prints:
doing
doing

31 January 2012

Servlets and thread safty

From Servlet 2.4 specs:

SRV.2.3.3.3
Thread Safety
Implementations of the request and response objects are not guaranteed to be thread
safe. This means that they should only be used within the scope of the request han-
dling thread.
References to the request and response objects should not be given to objects
executing in other threads as the resulting behavior may be nondeterministic. If
the thread created by the application uses the container-managed objects, such as
the request or response object, those objects must be accessed only within the
servlet’s service life cycle and such thread itself should have a life cycle within
the life cycle of the servlet’s service method because accessing those objects
after the service method ends may cause undeterministic problems. Be aware
that the request and response objects are not thread safe. If those objects were
accessed in the multiple threads, the access should be synchronized or be done
through the wrapper to add the thread safety, for instance, synchronizing the call
of the methods to access the request attribute, or using a local output stream for
the response object within a thread.

When the destroy method gets called in the Servlet?

It is an implementation-depended, nobody knows!
SRV.2.3.4
End of Service
The servlet container is not required to keep a servlet loaded for any particular
period of time. A servlet instance may be kept active in a servlet container for a
period of milliseconds, for the lifetime of the servlet container (which could be a
number of days, months, or years), or any amount of time in between.
When the servlet container determines that a servlet should be removed from
service, it calls the destroy method of the Servlet interface to allow the servlet to
release any resources it is using and save any persistent state. For example, the
container may do this when it wants to conserve memory resources, or when it is
being shut down.
Before the servlet container calls the destroy method, it must allow any
threads that are currently running in the service method of the servlet to complete
execution, or exceed a server-defined time limit.
Once the destroy method is called on a servlet instance, the container may
not route other requests to that instance of the servlet. If the container needs to
enable the servlet again, it must do so with a new instance of the servlet’s class.
After the destroy method completes, the servlet container must release the
servlet instance so that it is eligible for garbage collection.


Source Servelt-2.4 specs document

How easy to parse JSON in JS

It is that easy!


var myObj = eval("obj={'name':'ali', 'age':'20'}");

document.write (myObj.name + ' ' + myObj.age);