Monday, December 29, 2008

Java Heap Error in Maven JUnit Eclipse Environment

I encountered Java Heap Error (java.lang.outOfMemoryError) while running JUnit test cases using Maven plugin in Eclipse IDE. When maven build is run thru IDE, it forks another java process which kept running out of heap space after reaching only about 100M. After changing a number of different settings in IDE, Maven, OS environment variables and spending close to 8 hrs. Finally arrived at the solution.

First changed environment variable JAVA_OPTS to -Xms40m -Xmx1024m -XX:MaxPermSize=256m but no luck.

Second changed MAVEN_OPTS to -Xms40m -Xmx1024m -XX:MaxPermSize=256m but no luck.

Third changed eclipse.ini to have vm parameter -Xms40m -Xmx1024m -XX:MaxPermSize=256m but no luck. This file is so fragile, making changes here breaks eclipse pretty bad.

Fourth through IDE , Run Configuration -> Maven Build provided VM parameters -Xms40m -Xmx1024m -XX:MaxPermSize=256m -verbose but no luck.

Fifth changed IDE setting for JDK to use vm parameters -Xms40m -Xmx1024m -XX:MaxPermSize=256m but no luck

Sixth option worked like charmed, in maven pom.xml added following setting for the plugin that helps you run JUnit test cases. Added following to .pom file.

<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-surefire-plugin</artifactid>
<version>2.2</version>
<configuration>
<argline>-Xmx1024m</argline>
</configuration>
</plugin>

Saturday, December 13, 2008

mysql replication

MySQL provides ability to replicate a database in a Master/Slave fashion. All write and update operations are done on Master instance and are written to a binary log. This log is read by slave instances to replicate the changes. Replication is done in asynchronous mode, slaves keep track of the location within the log.

When would you use a replication ?
1) For high scalability, read off of slaves and writes on Master.
2) For backups.
3) For having a copy of data to resource intensive analytics without hindering master server performance.

Hibernate OutOfMemoryException

When working with large number of Hibernate objects returned by a query, hibernate caches these objects in 1st level cache. To avoid running out of heap space use session.flush(), session.clear() and ScrollableResults.

Hibernate Interceptor

Hibernate Interceptor
By implementing Hibernate Interceptor interface or by implementing org.hibernate.classic.Validatable or by implementing org.hibernate.classic.Lifecycle , you can receive the various callbacks from a hibernate "session".

Using these callbacks you could inspect persistent objects before they are saved, deleted, updated or loaded.
    1. Interceptor Approach
    • Interceptor could be done at a session scope or at session factory level or at persistent class level.
      • If its done at session factory level, interceptors must be thread safe, taking care to not store session-specific state since multiple sessions will use this interceptor (potentially) concurrently.
      • At session level, one instance per session.
    • To implement a Interceptor 2 ways
      • Implement interface org.hibernate.Interceptor
      • Or extend org.hibernate.EmptyInterceptor
    • Once interceptor is implemented register it programatically with session or session factory
      • Register the interceptor with Session
Session session = sf.openSession( new MyInterceptor());
      • Register the interceptor with SessionFactory
new Configuration().setInterceptor( new MyInterceptor() );
    • Integration with spring ?



<bean id="myInterceptor" class="com.company.MyInterceptor"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="entityInterceptor">
<ref bean="myInterceptor">
</ref>
</property></bean>
    • Pros & cons
  • Gives flexibility to do things at session and/or at entire application level.
  • Could be a bottleneck if interceptor is being used at SessionFactory level, code inside interceptor has to be written very-2 carefully to make sure it is efficient. --- Things we may end up doing are logging changes to specific database tables in a database or to a log file(files is not a good idea).
  • Would be nice if the interceptors could turned on and off based on configuration files, instead of programatically enabling it.? --- This could be achieved by modifying SPRING configuration file.(applicationContext.xml)
  • To log for specific list of persistence entities there is no out of box configuration, one would have to create additional configuration parameters ?

Sunday, November 16, 2008

installing and using axis2 plugin

1) Download axis2 plugin from

http://ws.apache.org/axis2/tools/index.html

2) Extract plugin in eclipse/plugins directory

3) In eclipse IDE go to menu windows -> preferences
click on option web services -> Axis2 Preferences and specify location of folder where axis2 is installed.

4) To use axis2 plugin, goto project "properties" pane, and select "Project Facets" and select "Axis2 Web Services"

Now your project is ready to create a webservice using Java to WSDL style or from WSDL to Java.

To create a WSDL from java class, right click on class name and select "Web Services" -> "Create Web Service"

Other options options is to create the web service using File -> "New" --> "Other" -> Axis2 Wizards -> Axis2 Code Generator

InvocationTargetException in Eclipse Axis2 plugin

When generating WSDL code from a java class using Eclipse/Axis2 plugin version 1.4.1 an exception is thrown( java.lang.reflect.InvocationTargetException).

This version of plugin is missing 2 jar files ( backport-util-concurrent-3.1.jar, geronimo-stax-api_1.0_spec-1.0.1.jar) in lib directory. Here are the steps to get around this problem. The jar files could be copied from lib directory under Axis2 distribution.

1) Copy jar files to eclipse/plugins/Axis2_Codegen_Wizard_1.3.0/lib directory
2) Add these jar entries to eclipse/plugins/Axis2_Codegen_Wizard_1.3.0/plugin.xml
3) Restart eclipse IDE using -clean option. (modify eclipse.ini file)

More plugin problems can be dubugged using a cool technique mentioned in blog post
http://blogiterox.wordpress.com/2008/10/24/exploring-apache-axis2-and-eclipse-plug-in-development/