Friday, April 30, 2010

java.lang.OutOfMemoryError: PermGen space

First, http://blogs.sun.com/fkieviet/entry/classloader_leaks_the_dreaded_java
Then, http://blogs.sun.com/fkieviet/entry/how_to_fix_the_dreaded

http://community.eapps.com/showthread.php?153-Eliminate-PermGen-out-of-memory-errors-for-good! : It says use -XX:+CMSPermGenSweepingEnabled, -XX:+CMSClassUnloadingEnabled. But what if the classes are still being referred as discussed in the first two SUN blog posts.


http://java.dzone.com/articles/java-performance-tuning?page=0,1 : A generic article


http://stackoverflow.com/questions/88235/how-to-deal-with-java-lang-outofmemoryerror-permgen-space-error

http://forum.springsource.org/showthread.php?t=21383&highlight=cglib+cache : heated up arguments

http://java.sun.com/javase/6/webnotes/trouble/TSG-VM/html/memleaks.html#gbyuu: Permgen issue with interned Strings

http://rajakannappan.blogspot.com/2010/01/outofmemoryerror-on-heapspace-permgen.html : Suggests using XX:+TraceClassLoading and -XX:+TraceClassUnloading , XX:+PrintGCDetails  , JVisualVM


http://www.samaxes.com/2007/10/classloader-leaks-and-permgen-space/ : Basically analyses the first two SUN blog posts and directs to following  useful posts.
1. http://blogs.sun.com/sundararajan/entry/jhat_s_javascript_interface : contains the oql function for histograms
3. http://mediacast.sun.com/users/Frank.Kieviet/media/JavaOne07-BOF9982-PermGen.pdf : awesome


JACKPOT- what worked for me
http://little418.com/2009/08/grails-configslurper-and-its-permgen-apatite.html : check the email chain too.
Basically I was parsing a config (thus creating new() ConfigSlurper ) inside a method of my service that's called multiple fixed no. of times (say X ) from my cron job. At each cron job run some fixed number of dynamically created classes were loaded X times(each time with some different ID that's used in the class name). When cron job finishes the objects are freed but the classes remian loaded.
Solution: Moved config parsing to the constructor of my service and made ConfigSlurper object as a class parameter(earlier it was a local method variable)

Wednesday, April 28, 2010

Fighting java.lang.NoClassDefFoundError: org/cyberneko/html/parsers/SAXParser on Grails-1.2.1

This was a problem that occured while using HttpBuilder for making HTTP requests.

1. When I say httpBuilder.request(GET, HTML){}, it throws
java.lang.NoClassDefFoundError: org/cyberneko/html/parsers/SAXParser

        at groovyx.net.http.ParserRegistry.parseHTML(ParserRegistry.java:157)

ParserRegistry source:

public GPathResult parseHTML( HttpResponse resp ) throws IOException, SAXException {
return new XmlSlurper( new org.cyberneko.html.parsers.SAXParser() )
.parse( parseText( resp ) );
}

My BuildConfig contained:

build ('org.codehaus.groovy:http-builder:0.5.0-SNAPSHOT'){
excludes "junit", "xml-apis", "xercesImpl"
}



Diagnosis: The class "org/cyberneko/html/parsers/SAXParser" belongs to nekohtml.jar. HttpBuilder has a dependency on nekohtml.jar. This dependency gets resolved in the Grails dependency resolution step. However I still get the above exception. Stupid class loading issue.


2. So added to my BuildConfig:
 runtime ('net.sourceforge.nekohtml:nekohtml:1.9.9'){
}
This lead to:

Error executing script RunApp: loader constraint violation: when resolving overridden method "org.apache.xerces.jaxp.SAXParserImpl.getParser()Lorg/xml/sax/Parser;" the class loader (instance of org/codehaus/groovy/grails/cli/support/GrailsRootLoader) of the current class, org/apache/xerces/jaxp/SAXParserImpl, and its superclass loader (instance of ), have different Class objects for the type org/xml/sax/Parser used in the signature

        at org.apache.xerces.jaxp.SAXParserFactoryImpl.newSAXParser(Unknown Sour
ce)

Diagnosis: It seems that nekohtml has a dependency on xercesImpl that in turn has a dependency on xml-apis.jar that contains the class org/xml/sax/Parser. However this class had already been loaded by some parent class loader from somewhere else.

3. So I modified my BuildConfig:
runtime ('xerces:xercesImpl:2.8.1'){
excludes "xml-apis"
}
 runtime ('net.sourceforge.nekohtml:nekohtml:1.9.9'){
excludes "xercesImpl"
}
SUCCESS :D


Thursday, April 22, 2010

Deploying grails war to tomcat- servlet-api conflict

http://jira.codehaus.org/browse/GRAILS-2053

http://jira.codehaus.org/browse/GRAILS-5174

Wednesday, April 21, 2010

The Grails framework Reference Documentation

http://www.grails.org/doc/latest/guide/single.html#3.1.3 GORM

Grails save() dynamic method requires exception throwing to be enabled explicitly

http://www.grails.org/doc/latest/ref/Domain%20Classes/save.html:
Check out the failOnError option.


if( !b.save() ) {
   b.errors.each {
        println it
   }
}
OR
try{
b.save(failOnError :true)
}catch(ValidationException e){
//handle exception
}
However I could't find any ValidationException class in the package grails.validation of grails-1.2.1.
So went for the b.errors.each approach.

Grails with Spring

Some advanced stuff:
http://www.grails.org/doc/1.0.x/guide/14.%20Grails%20and%20Spring.html

Working with AJAX on GRAILS

The home:
http://www.grails.org/Ajax

GRAILS tag docs:
http://grails.org/doc/latest/

We want to constrain the user to entering only Countries and Cities that our database knows about. Change contents of the list of cities when a particular country is selected
http://www.grails.org/AJAX-Driven+SELECTs+in+GSP

Nice 2006 debate:
http://bytes.com/topic/javascript/answers/480297-can-ajax-request-left-open-multiple-responses
Comet arch: little spices on the above debate
http://alex.dojotoolkit.org/2006/03/comet-low-latency-data-for-the-browser/

AJAX XmlHttpRequest states complete reference:
http://www.ibm.com/developerworks/web/library/wa-ajaxintro3/

Aah! got what i needed:
http://www.coderanch.com/t/121362/HTML-JavaScript/Multiple-responses-one-request- look for post by ben souther that points to http://simple.souther.us/not-so-simple.html by http://faq.javaranch.com/java/BioBenSouther
The example named Long Running Process is the one

Tuesday, April 20, 2010

Using excludes while dependency resolution in grails 1.2

http://www.pither.com/articles/2010/02/01/dependency-excludes-in-grails-1.2:
http://www.grails.org/doc/latest/guide/3.%20Configuration.html#3.7%20Dependency%20Resolution

Trying to upgrade a grails 1.1.1 app to 1.2.1.
Without excluding xml-apis.jar in buildConfig.groovy dependency configs one gets an:

Error executing script Upgrade: loader constraint violation: loader (instance of ) previously initiated loading for a different type with
name "org/xml/sax/SAXParseException"
java.lang.LinkageError: loader constraint violation: loader (instance of ) previously initiated loading for a different type with name "or
g/xml/sax/SAXParseException"
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
        at java.lang.Class.getDeclaredMethods(Class.java:1791)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:33)
        at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:20)
        at _PluginDependencies_groovy.readMetadataFromZip(_PluginDependencies_groovy:922)
        at _PluginDependencies_groovy.this$4$readMetadataFromZip(_PluginDependencies_groovy)
        at _PluginDependencies_groovy$_run_closure24.doCall(_PluginDependencies_groovy:911)
        at _PluginDependencies_groovy$_run_closure31_closure87.doCall(_PluginDependencies_groovy:1208)
        at _PluginDependencies_groovy$_run_closure31_closure87.doCall(_PluginDependencies_groovy)
        at _PluginDependencies_groovy.withPluginInstall(_PluginDependencies_groovy:1253)
        at _PluginDependencies_groovy.this$4$withPluginInstall(_PluginDependencies_groovy)
        at _PluginDependencies_groovy$_run_closure31.doCall(_PluginDependencies_groovy:1207)
        at _PluginDependencies_groovy$_run_closure31.call(_PluginDependencies_groovy)
        at _GrailsPlugins_groovy$_run_closure3.doCall(_GrailsPlugins_groovy:96)
        at Upgrade$_run_closure1.doCall(Upgrade.groovy:215)
        at Upgrade$_run_closure2.doCall(Upgrade.groovy:223)
        at gant.Gant$_dispatch_closure4.doCall(Gant.groovy:324)
        at gant.Gant$_dispatch_closure6.doCall(Gant.groovy:334)
        at gant.Gant$_dispatch_closure6.doCall(Gant.groovy)
        at gant.Gant.withBuildListeners(Gant.groovy:344)
        at gant.Gant.this$2$withBuildListeners(Gant.groovy)
        at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
        at gant.Gant.dispatch(Gant.groovy:334)
        at gant.Gant.this$2$dispatch(Gant.groovy)
        at gant.Gant.invokeMethod(Gant.groovy)
        at gant.Gant.processTargets(Gant.groovy:495)
        at gant.Gant.processTargets(Gant.groovy:480)
Error executing script Upgrade: loader constraint violation: loader (instance of ) previously initiated loading for a different type with
name "org/xml/sax/SAXParseException"

Nice technique for piracy protection

http://www.diskserialnumber.com/discuss/
Using harddriveinfo.dll library get hdd serial no. and bind the s/w copy to it.
I suppose the s/w distribution must be writable for this to work.

Monday, April 12, 2010

Grails goof-ups

Wanted to write simple test cases. Caught up for an hour in the following. Should have shown a compile-time error had it been strongly-typed language
Try and spot the difference between the following two groovy statements
Hint: Having statement 1 throws an "org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack: No such property: changefreq for class: MyServiceTests"
Statement2 is the correct one.

Statement 1:
mockDomain(NewSitemapUrl, [
    new NewSitemapUrl(id: 10, loc:"http://example.com/10",lastmod:today, changefreq:"daily", priority: '1.0'),
new NewSitemapUrl(id: 20, loc:"http://example.com/20", lastmod:today, changefreq="daily", priority:'1.0'),
new NewSitemapUrl(id: 30, loc:"http://example.com/30", lastmod:today, changefreq="daily", priority:'1.0')
])

Statement 2: 
mockDomain(NewSitemapUrl, [ 
    new NewSitemapUrl(id: 10, loc:"http://example.com/10",lastmod:today, changefreq:"daily", priority: '1.0'),
new NewSitemapUrl(id: 20, loc:"http://example.com/20", lastmod:today, changefreq:"daily", priority:'1.0'),
new NewSitemapUrl(id: 30, loc:"http://example.com/30", lastmod:today, changefreq:"daily", priority:'1.0')
])

Thursday, April 8, 2010

Mysql ERROR 1153: Got a packet bigger than 'max_allowed_packet' bytes

Personal opinion: Got this while loading a large dump file. Never got this before even for large files.
It seems it happens when you haven't committed for long(data to be committed grow larger in size than a variable called max_allowed_packet)


http://dev.mysql.com/doc/refman/5.1/en/packet-too-large.html: It says use --max_allowed_packet=50M while starting mysql.
http://forums.mysql.com/read.php?35,75794,261640#msg-261640: says server and client have their own copy of these variables. Hence the above solution doesn't work. (Certainly not for remote clients, don't know otherwise).


What worked for me:
$mysql 
mysql>set global --max_allowed_packet=52428800 //(50MB) 
mysql>exit 
$mysql mysql>show variables //check that the variable is set correctly 
mysql>exit 
$mysql -u root -p [dbname] < [path to dump file]


"$" is the shell prompt
"mysql>" is the mysql client prompt


That's it.


http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html: mysql server sys vars

Tuesday, April 6, 2010

Migrating from Grails 1.1.1 to 1.2.1

1. Case: Using an xml-rpc-client. Dependencies are listed as below.
xmlrpc-client-3.1.jar -> xmlrpc-common-3.1.jar->ws-commons-util-1.0.2.jar->xml-apis-1.3.03.jar.

I had such a Grails 1.1.1 application. Wanted to upgrade to 1.2.1
Executed grails clean ->grails upgrade.
Added BuildConfig.groovy stating xmlrpc jars as dependencies.
Removed ivy.xml/ivyconf.xml/ivysettings.xml. Uninstalled ivy plugin(if exists)
After this, running any of the grails scripts triggered dependency resoltion using grails 1.2.1's internal ivy mechanism. This looked at the buildConfig and started downloading xml-rpc jars and their dependencies.
After resolution completes, (for any grails script) we get
grails java.lang.LinkageError: loader constraints violated when linking org/xml/sax/Parser class

Solution:
Don't let ws-commons-util-1.0.2.jar and xml-apis-1.3.03.jar be together in same environment.
Remove dependency xml-rpc-client/server from buildConfig.groovy. Copy corresponding files to lib folder of app manually(xmlrpc-client-3.1.jar -> xmlrpc-common-3.1.jar->ws-commons-util-1.0.2.jar). DOOOOONOT add xml-apis-1.3.03.jar. The exception goes away. If i try to use inbuilt resolution for the xml -rppc jars, ivy always fetches ws-commons-util-1.0.2.jar followed by its dependency xml-apis-1.3.03.jar

http://n4.nabble.com/org-apache-xerces-jaxp-SAXParserImpl-getParser-Lorg-xml-sax-Parser-Error-trying-to-include-compile-o-td1695193.html : haven't tried this yet

OR

Use "excludes" mechanism while specifying xmlrpc jar dependencies in BuildConfig

2. Getting "java.lang.NoSuchMethodError:
org.codehaus.groovy.grails.commons.GrailsApplication.getClassLoader()Lgroovy/lang/GroovyClassLoader;" when application tries to start.
http://n4.nabble.com/1-2RC2-Error-creating-bean-with-name-sessionFactory-td1322056.html

For me, it happened because I use Ivy plugin's "get-dependencies" script explicitly to resolve dependencies. This fetched all jars into app's lib folder. Some of them might be causing the problem.(GrailsApplication.getClassLoader() was changed from returning GroovyClassLoader to ClassLoader for 1.2)
Solved it by deleting all the jars from lib folder and adding BuildConfig.groovy to grails-app/conf. Grails-1.2.1 handled the dependencies itself using its internal ivy.

3. App build successfully but when tomcat tries to start it throws
org.springframework.beans.factory.access.BootstrapException: Error executing bootstraps; nested exception is java.lang.NoSuchMethodError: javax.servlet.ServletContext.getContextPath()Ljava/lang/String;


http://youtrack.jetbrains.net/issue/IDEA-27215: There's a comment saying that
the method javax.servlet.ServletContext.getContextPath()Ljava/lang/String was added in servlet-api-2.5.jar. It's not present in the 2.4 version. Grails must be somehow loading the ServletContext class from 2.4 instead of 2.5.

Solution: Add 'javax.servlet:servlet-api:2.5' as build dependency in BuildConfig.groovy


4. If you have a hasOne relationship from domain class A to B that is supposed to be nullable, then you need to explicitly specify it as nullable in class A constraints for Grails 1.2.1. It worked without the constraint in Grails-1.1.1.