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 ?