Mockito thenReturn() vs thenAnswer()

Mockito is a very powerful test library for Java. In most cases, setting some mock action with thenReturn() is enough. But in some rare cases, we need to use thenAnswer(). thenReturn() example First, let's review the basic use of thenReturn() First, I want to test getResultWithParam(). Above test case succeeds. Wrong test case Next, I … Continue reading Mockito thenReturn() vs thenAnswer()

Mybatis cache mechanism

Mybatis supports internal cache. There are 2 kinds of cache. The first is session cache. (First level cache) The second is global cache. (Second level cache) Session cache (First level cache) Session cache is managed within a SqlSession. (SqlSession is the wrapper of a database connection) It can not be accessed from another SqlSession. And … Continue reading Mybatis cache mechanism

Spring Transaction with @Transactional explained

The most easy way to Spring Transaction is to use @Transactional. The following is basic step to use it. Required spring component spring-context.jar, spring-tx.jar, spring-jdbc.jar and additional dependencies Spring context <tx:annotation-driven> must be declared to enable @Transactional TransactionManager must be declared. If the id is not "transactionManager", it must be set with <tx:annotation-driven>'s transaction-manager attribute … Continue reading Spring Transaction with @Transactional explained

About Spring ViewResolver

ViewResolver is very important component of Spring MVC. Without proper understanding, it is difficult to analyze MVC components, especially when taking over other's source code. The following explanations is based on Spring Framework 4.3.18. What is ViewResolver? (org.springframework.web.servlet.ViewResolver) ViewResolver is the translator which converts view name into View object View is the component which renders … Continue reading About Spring ViewResolver

Oracle JDBC slow connection on some virtual server

This issue is found on Oracle JDBC driver 11.2.x. Environment : Linux virtual server, Oracle JDBC driver 11.2.x Issue : Oracle connection takes long time or occurs timeout error Cause : Oracle JDBC driver, when it connect to database server, calls class. SecureRandom uses /dev/random on Linux or Unix server. /dev/random pseudo device uses … Continue reading Oracle JDBC slow connection on some virtual server

Java runtime parameters to enable JMX

To enable JMX for some java process, the following option is required. The important two ports are and If JMX client is connecting through firewall, the two ports must be opened.

The inner working of DispatcherServlet

The main program of Spring MVC is DispatcherServlet. Most Spring MVC webapp's web.xml is as follows. <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation=""> <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> … Continue reading The inner working of DispatcherServlet

How to analyze high cpu usage of a Java program?

Many Java daemons are multi threaded. If some program shows high cpu usage, how will you analyze it? I'm showing the tip based on Linux environment. 1) Detect problemetic thread Usually linux "top" shows total cpu usage of a process. Therefore we need to separate problematic thread. While running top, press "H" or start top … Continue reading How to analyze high cpu usage of a Java program?