Wednesday, October 19, 2011

Recently, I heard a guy cried that the DB2 for iSeriese does not use index he created. Then, I asked him if he has check the size of table. The answer is NO!

So, it seems that some developers do not aware that a RDBMS' query optimizer may not use index though indexes are created to optimize SQL query speed. In fact, DBMS normally have its own algorithm to decide when to use index scan or table scan in a SQL query. In general, DBMS should use index scan instead of full table scan only when size of index is smaller than size of table. Otherwise, there is no point for DBMS to use index. This is true for DB2, MySQL, Oracle, MS SQL etc.

How to create index or combined index and how many index should be created for a table is big topic. But, we shall know that index may not be always used when SQL query optimizer thinks it is not worthy. This is most likely true for a new launched application, which has not received/created many data in tables. So, do not cry that DBMS is suck that it does not use indexes. Check your table size first. Then, check your DBMS configuration. For example, MySQL has configurable threshold value, which determine when query optimizer can use index instead of full table scan. But, you can not force SQL query optimizer to use index. The most we can do is configuring table to prefer index scan than table scan.

Tuesday, October 18, 2011

We always say that there is only one copy of static fields of a class in JVM memory. This is incorrect. At least, it is not precise. The precise statement should be "there is only one copy of static fields of class for each class loader in one JVM". In other words, we will have multiple copies of static fields in one JVM memory if we use different ClassLoaders to load same class into one JVM.

Different JVM vendor may have different ways to do JVM memory management. But, I think it will be common for JVM to have class initializer and instance initializer. static fields in class is initialized in class initializer. Using hotspot as example, "instanceKlass" in JVM contains static fields. To understand more about java memory management, these articles may be helpful. Presenting the Permanent Generation , OpenJDK Storage management, and JVM spec.

But, we are not guys who are able to create JVM. So, let's use our ways to determine this. What we will do here are

Create our own class loader.

Create a simple Java class which has static field for testing.

Create two instances of our own class loader and let them to load class.

Changing static field value in one loaded class instance and see if the rest one has changed field or not.

We will say that static fields defined in same class have different copies in memory if their containing class is loaded into memory by different classloader.

getting a class loader from JavaBlogging and slightly modified it as below.

package jia.blog;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* Our custom implementation of the ClassLoader.
* For any of classes from "javablogging" package
* it will use its {@link CustomClassLoader#getClass()}
* method to load it from the specific .class file. For any
* other class it will use the super.loadClass() method
* from ClassLoader, which will eventually pass the
* request to the parent.
*
*/
public class JiaClassLoader extends ClassLoader {
/**
* Parent ClassLoader passed to this constructor
* will be used if this ClassLoader can not resolve a
* particular class.
*
* @param parent Parent ClassLoader
* (may be from getClass().getClassLoader())
*/
public JiaClassLoader() {
super(JiaClassLoader.class.getClassLoader());
}
/**
* Loads a given class from .class file just like
* the default ClassLoader. This method could be
* changed to load the class over network from some
* other server or from the database.
*
* @param name Full class name
*/
private synchronized Class getClass(String name)
throws ClassNotFoundException {
// is this class already loaded?
Class cls = findLoadedClass(name);
if (cls != null) {
System.out.println("class " + name + "has been loaded.");
return cls;
} else {
System.out.println("class " + name + " has not been loaded. Loading now.");
}
// We are getting a name that looks like
// javablogging.package.ClassToLoad
// and we have to convert it into the .class file name
// like javablogging/package/ClassToLoad.class
String file = name.replace('.', File.separatorChar)
+ ".class";
byte[] b = null;
try {
// This loads the byte code data from the file
b = loadClassData(file);
// defineClass is inherited from the ClassLoader class
// and converts the byte array into a Class
cls = defineClass(name, b, 0, b.length);
resolveClass(cls);
return cls;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* Every request for a class passes through this method.
* If the requested class is in "javablogging" package,
* it will load it using the
* {@link CustomClassLoader#getClass()} method.
* If not, it will use the super.loadClass() method
* which in turn will pass the request to the parent.
*
* @param name
* Full class name
*/
@Override
public Class loadClass(String name)
throws ClassNotFoundException {
System.out.println("loading class '" + name + "'");
if (name.startsWith("jia.")) {
return getClass(name);
}
return super.loadClass(name);
}
/**
* Loads a given file (presumably .class) into a byte array.
* The file should be accessible as a resource, for example
* it could be located on the classpath.
*
* @param name File name to load
* @return Byte array read from the file
* @throws IOException Is thrown when there
* was some problem reading the file
*/
private byte[] loadClassData(String name) throws IOException {
// Opening the file
InputStream stream = getClass().getClassLoader()
.getResourceAsStream(name);
int size = stream.available();
byte buff[] = new byte[size];
DataInputStream in = new DataInputStream(stream);
// Reading the binary data
in.readFully(buff);
in.close();
return buff;
}
}

Having this knowledge in mind, we can explore more about how to use static and thread safe issue. In real world, we can see customized class loader in java application server, loading class from network, Web application is a born multi thread environment. It will be necessary to have a clear view about class loader architecture, thread safe, and static members.

Thursday, October 6, 2011

After password strength meter widget, I am asked to give a Captcha verify widget too. Well, it is not so difficult, below is a ReCaptcha widget for Ext JS 4.x. On ExtJS 2.x forum, there are detailed description about how to apply for ReCaptcha account.

I lived in China before 1999. Since 1996, I had been working as a software R&D engineer. I worked on JavaOS, a lighweight network operating system for Network Computer. Meanwhile, I used CGI, applet, swing, activeX control, and Webshpere 1.0 to develop banking Web applications.

1999~2010. I immigrated to Singapore and worked on core Java applications, Web applications, mobile applications, and interactive TV middleware and applications. Singapore feeds me more than 10 years. I appreciate her!

2010 ~ present. I moved to USA to reunion with my family. I am currently living in great Boston area. I'd like to make more friends here. Nice to meet you :)