Archive for the ‘Articles in English’ Category

Selects are IN

Saturday, May 10th, 2008

Today it will be short. I will write about one small Spring feature that I have discovered recently. It is another nice functionality of SimpleJdbcTemplate.

Imagine that you want to find all account with given account numbers. Since we want to gain maximal performance we want to do it in one SQL statement.

SELECT ACCOUNT_NUMBER, BALANCE FROM ACCOUNT WHERE ACCOUNT_NUMBER IN (?, ?)

Using Spring it is easy. The only trouble is, that I do not know number of account numbers beforehand. Therefore I do not know the number of question marks needed in the SQL query. Of course I can create the statement dynamically using string manipulation, but the code would be messy. Fortunately Spring comes to rescue. If I use named parameters in SimpleJdbcTemplate, Spring will automatically do everything for me. So the code will be nice and simple as it should be

	public List<Account> findAccounts(Set<String> accountNumbers)
	{
		return getSimpleJdbcTemplate().query(
				"SELECT ACCOUNT_NUMBER, BALANCE FROM ACCOUNT WHERE ACCOUNT_NUMBER IN (:accountNumbers)",
				ACCOUNT_ROW_MAPPER,
				Collections.singletonMap("accountNumbers", accountNumbers)
		);
	}

Nice, isn’t it? Source code can be found in SVN. More details are in the Spring documentation.

Tomcat killer demystified

Wednesday, March 26th, 2008

This entry contains solution of the mystery mentioned in the previous one.

Big Detective: Dear Watson, do you know who is the killer?
Dr. Watson: No, I have no idea, it is so mysterious. I think, there has to be some bug in the JVM.
Big Detective: Interesting hypothesis. But not plausible.
Dr. Watson: But as you say “Eliminate all other factors, and the one which remains must be the truth.” I do not see any other solution.
Big Detective: We will see. Let’s arrange all the facts. First of all, the virtual server has only 256 megabytes of memory. What happens when it is not enough?
Dr. Watson: Well, the swap space is used.
Big Detective: Usually yes, but not in our case. Unfortunately the configuration of the virtual server does not enable swap.
Dr. Watson: I see, but what happens when the server does not have enough memory?
Big Detective: That’s a good question. Linux has something called OOM killer. As you can read in Who is Who: “It is the job of the linux ‘oom killer’ to sacrifice one or more processes in order to free up memory for the system when all else fails.”
Dr Watson: So OOM killer kills Tomcat when Linux does not have enough memory! But it means that adding more heap to the Tomcat JVM makes the trouble even worse!
Big Detective: Exactly,Watson.

Help! Someone is killing my Tomcat

Monday, March 24th, 2008

Today I have a detective story for you. Let’s start with basic information. Victim is well known Tomcat 5.5 server. He lives in a virtual server, with 8 processors and 256 megabytes of memory. There are also several servants living in the house. First of all, there is Debian who does basically all the housekeeping. Then there is native Apache servant who communicates with the outside world. In the cellar lives MySql daemon who keeps all important data.

The main occupation of the Tomcat server is to run my great mvnindex.org project. It is quite easy job. First of all, no one except me is using it. Secondly, the main task of the server is calling the database and returning the results. And of course once per day he has to index the repository.

Even thou his task is simple, sometimes I find Tomcat lying dead. Sometimes he manages to write his last words to his log.

Java HotSpot(TM) Client VM warning: Exception java.lang.OutOfMemoryError occurred dispatching signal SIGTERM to handler- the VM may need to be forcibly terminated

Sometimes he dies without any whisper. At first glance it looks like a natural death caused by lack of heap memory. But there is some circumstantial evidence against this theory. First of all, this mystical sudden death usually happens when there is some visit in the house. For example when I am building new version of the application. Imagine that. I am running mvn clean install command and Tomcat, who is running in a separate process, in separate JVM (1.6.0-b105) under another user suddenly dies. That is strange.

Moreover, adding heap memory using java -Xmx parameter does not help. Sometimes Tomcat lives for several days, sometimes he dies twice per day.

Now you have all the data you need to solve the mystery. Is it a murder? Is it a suicide? Is there a memory leak in the application? Is it a sabotage?

I will not tell you the solution right know. I am sure that someone will solve it in the discussion below. If not, I will publish the solution in few days.