Friday, December 3, 2010

Few Basic Things about String intern( ) in Java

Consider a scenario when we read a csv file with very large number of records, we may end up a a lot of duplicate String objects. To avoid duplicate String objects getting created in these kind of scenarios we can use String.intern( ) method.

How it works ? Internally there will be Map/Table of String literals. First time when intern( ) is called on a String, it is added to this table. Subsequent calls String.intern ( ) will return reference to the String in the previous mentioned Table/Map.

one benefit of string interning is that == comparison is much faster.Say you interned few Strings which got added in the previous mentioned table. How do you remove any of those from the table ? Oops ! Are we Struck here until the program ends ?In the most recent JVMs, interned Strings are implemented as Soft references, so that they can be garbage collected soon.

String literals at compile time will be automatically interned, But literals created on run time(like command line arguments) will not be interned.

Monday, November 22, 2010

Diff b/w ClassNotFoundException and NoClassDefFoundError

When i was googling about this I found that information in some of blogs were misleading. Hence an attempt to clear this confusion.

ClassNotFoundException

Java Specification for ClassNotFoundException says below:
Thrown when an application tries to load in a class through its string name using:
  • The forName() method in class Class.
  • The findSystemClass method() in class ClassLoader.
  • The loadClass() method in class ClassLoader.
but no definition for the class with the specified name could be found.
So a ClassNotFoundException is thrown if an explicit attempt to load a class fails. ClassNotFoundException is thrown because the test attempts the load using an explicit call to loadClass().


NoClassDefFoundError

Java Specification for NoClassDefFoundError says:
Thrown if the Java virtual machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.
Essentially, this means that a NoClassDefFoundError is thrown as a result of a unsuccessful implicit class load.
public class MyNoClassDefFoundTest {
public static void main(String[] args) {
X x = new X();
}
}
public class X extends Y {
public void myShow(){
System.out.println("In X");
}
}

public class Y {
public void myShow(){
System.out.println("In Y");
}
Once you have compiled the code, remove the classfile of Y and execute the code. We got to see that it throws NoClassDefFound Error.
Please note that the same error would still occur if X referenced Y in any other way -- as a method parameter, for example, or as an instance field

Wednesday, June 2, 2010

Cannot open connection Error on large hibernate updates/inserts

Problem:Recently I was getting "Cannot open connection/Transaction Inactive" Error when doing large number (100000) updates with hibernate
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<;100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); } tx.commit(); session.close();
Solution:When making new objects persistent flush() and then clear() the session regularly in order to control the size of the first-level cache.
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<;100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); if ( i % 20 == 0 ) { //similar to JDBC batch //flush a batch of inserts and release memory: session.flush(); session.clear(); } } tx.commit(); session.close();