Posts

Showing posts from April, 2010

Unit testing with EasyMock and Spring

Let's come up with simple test scenario. I have two Spring Service implementations with corresponding interfaces. I like to test only the process method in FooServiceImp class in isolation. The problem is FooServiceImp.peocess() calls FooBarService.process() method which I don't want to test with this peticular Unit test. So how do I test just FooServiceImp.process() method without testing FooBarService. EasyMock is to rescue. Before the testing I can replace the reference of FooBarService to a EasyMock object and add certain expected behavior to the Mock. Behaviors I like to add to mock: 1. The return value of the Mock service. 2. Capture the parameters passed to the Mock Service. 3. Add a Exception to test Exception scenario Here are the codes. Happy Unit Testing: FooServiceTest.java import static org.junit.Assert.*; import java.util.List; import org.easymock.Capture; import org.easymock.EasyMock; import org.junit.Before; import org.junit.Test; import or...

HibernateJMX with Spring

Hibernate Mbean is nice way to get runtime Hibernate Stat. Here is how to publish Hibernet Mbean to Tomcat Mbean server with Spring. Once Mbean are published, it can be access through Jconsole. Tomcat setup: JAVA_OPTS=" -Dcom.sun.management.jmxremote.port=9002 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.awt.headless=true" Spring config: <bean id="jmxExporter" class="org.springframework.jmx.export.MBeanExporter"> <property name="beans"> <map> <entry key="Hibernate:name=statistics"> <ref local="statisticsBean"> </ref> </entry></map> </property> </bean> <bean id="statisticsBean" class="org.hibernate.jmx.StatisticsService"> <property name="statisticsEnabled"> ...

Pivot Table with SQL

One on my favourite technique with SQL is creating pivot table. Here is an easy example using Postgres: CREATE TABLE REVENUE ( YR int4, QT chat, REVENUE numeric); insert into REVENUE values (2000, 'Q1', 240); insert into REVENUE values (2000, 'Q2', 200); insert into REVENUE values (2000, 'Q3', 230); insert into REVENUE values (2000, 'Q4', 120); insert into REVENUE values (2001, 'Q1', 320); insert into REVENUE values (2001, 'Q2', 340); insert into REVENUE values (2001, 'Q3', 290); insert into REVENUE values (2001, 'Q4', 100); Pivoting SQL: SELECT YR, MAX(CASE WHEN QT = 'Q1' THEN REVENUE END) Q1, MAX(CASE WHEN QT = 'Q2' THEN REVENUE END) Q2, MAX(CASE WHEN QT = 'Q3' THEN REVENUE END) Q3, MAX(CASE WHEN QT = 'Q4' THEN REVENUE END) Q4 FROM REVENUE GROUP BY YR The result set going be pivoted ------------------------------------------- YR  |  Q1  |  Q2  |  Q3  |  Q4 ------------...

Hibernate CacheMode.IGNORE option

Recently, I have ran into a hibernate related issue in our production system. We have a nightly batch program that reads a lot of rows from one table, then does some conversion and writes to a file. It brings 5000 rows in chunk, still we saw the nightly program took the server down with outOfMemory error when processing large number of rows. After some investigation, I have found that the program brings out all the data in one session. Hibernate is holding all the objects in its session until session is closed. As a result, the GC can't clear any previous chunk of object from memory. Since the we are reading the data for readonly purpose, we set the CacheMode.IGNORE on the Query object. It prevents Hibernate to hold the reference of the objects in it's session. Here is snippet of the code. Query qry = session.createQuery(query).setCacheMode(CacheMode.IGNORE);