Tuesday, 30 June 2009

Dependency Injection bad practices

"If all you have is a hammer, everything looks like a nail" - Abraham Maslow To make it clear - I don't claim that DI is evil. I think it's really useful tool. Just a tool like a hammer. Unfortunately very often I can see DI being misused. People tend to put DI in each piece of the application what is often either an overkill or at least unnecessary complication. One of the bad practices I've recognized is programming in XML. That happens for example when (hierarchies of) objects (including abstract objects) are defined in Spring configuration files and then used as injected prototypes in Java code which is coupled to their properties. Simple and clear solution is to introduce factory class to create objects directly in Java code. That's rather rare case but don't worry there are also other more spectacular like for example Anemic Domain Model. Which is very common and appreciated by some people. It decouples basically everything including the data and the logic. In my opinion DI encourages to use this approach because it enforces to drop the encapsulation and lets to do anyone anything - you have to set the properties somehow, right? Of course there are solutions like constructor injection and factory beans but they are used rarely. As far as I remember Spring 1.0 has been announced as a holy grail of JEE development. People dropped EJB2 and started learning Spring. I must admit EJB2 didn't have much in common with object oriented programming as well. The goal of Spring was to simplify JEE project and it has been achieved. Unfortunately Spring has inherited EJB2 functional approach - or Spring users did that. Did you ever seen a Java project written by someone who is dazzled by DI? It's easy to recognize such a project. If during the review you have to analyze call hierarchies defined mostly in DI configuration and distributed among several XML files - that's it. DI is not a golden hammer of software development and we also must remember about the design patterns. The art is to join them together and find the balance to not see everything as a nail anymore.

Wednesday, 24 June 2009

"From Concurrent to Parallel" by Brian Goetz

Brian Goetz is talking about the fork-join framework and parallel array types coming in Java SE 7. For me it looks absolutely great. See the presentation

Friday, 19 June 2009

Spring JDBC - don't forget to call compile()

If you're using Spring JDBC StoredProcedure and if your application produces errors like that below from time to time. ERROR [...] CallableStatementCallback; uncategorized SQLException for SQL [{? = call foo.bar(??)})}?)}]; SQL state [99999]; error code [17041]; Missing IN or OUT parameter at at index:: 3; nested exception is java.sql.SQLException: Missing IN or OUT parameter at index:: 3 Then check if you call method compile() after defining your IN and OUT parameters otherwise you might expect such an error being thrown during deployment to live system. If compile() is not called explicitly then Spring builds SQL string lazily at first call. If this stored procedure is called in more than one thread in parallel (which is very common scenario) then a race starts. SQL string variable is shared between the threads and as there is no synchronization mechanism involved all threads are appending to it.

Tuesday, 16 June 2009

Subversion 1.6 for Ubuntu Jaunty

Subversion 1.6 is now available in the launchpad repository. deb http://ppa.launchpad.net/anders-kaseorg/subversion-1.6/ubuntu jaunty main deb-src http://ppa.launchpad.net/anders-kaseorg/subversion-1.6/ubuntu jaunty main

Monday, 15 June 2009

MAT - Eclipse Memory Analyzer

Eclipse Galileo is just around the corner and brings MAT - heap memory analyzing tool on board. Let's give it a try. Here is very nice introduction.