Class Loading – JDBC Vs JNDI

source : Interesting comment in Mutant World

Did you ever wonder why you need to call Class.forName() to load JDBC driver but don’t have to for JNDI, which appeared in Java 2 at the same time when Context Classloader was introduced?

Here is the answer. When a classloader loads classes, it asks the parent classloader to load, only if the class is not available, it tries to load it.
In the case of API, such as JNDI, the parent classloader usualy can load JNDI classes, but only child classloader can load JNDI implementation, such as J2EE server client library.
So, standard classloading mechanism doesn’t work. That is why we need context classloader. JNDI API is using it to load JNDI implementation classes from child classloader!

Advertisements

Leave a comment

Filed under Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s