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 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
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
jmap -J-d64 -dump:format=b,file=/path/to/dump/file java_pid * -J-d64 : in case of 64bit jdk
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
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 java.security.SecureRandom 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
To enable JMX for some java process, the following option is required. -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=9099 -Dcom.sun.management.jmxremote.rmi.port=9199 The important two ports are com.sun.management.jmxremote.port and com.sun.management.jmxremote.rmi.port. If JMX client is connecting through firewall, the two ports must be opened.
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="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 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
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?