Friday, July 15, 2011

mockito tutorial video

I recently find a very good screencast about mockito, a great mocking framework. There it is in three parts:

Thursday, July 14, 2011

why my new transaction is not created spring?

I was inserting a single data from table A to table B and then removing the single instance from table A. I don't want to have a problem if insert happens but the delete does not. So for this action I needed a transactional behavior.

I'm using Spring declaratively and thus using TransactionManager for implementing transactional behavior. I declared my method's propagation as REQUIRES_NEW which mean I absolutely want a new transaction for this method.

In my Service class' process() method I call insertAndDeleteTrxNew().

public void process(){
Record records = stuff();

insertAndDeleteTrxNew() method inserts from table A to table B and deletes from table A. Spring's log tells me that it did not create a transaction for my method.
What I exactly get is:
Don't need to create transaction for [Service.insertAndDeleteTrxNew()]: This method isn't transactional.

After few time searching I found out the root cause of my problem. Spring build a proxy for my Service class and any transactional behavior will work on this level. If I set process() transactional and call it first I'll implicitly go to the proxy and call proxy's process() method then proxy will call the instance's process() method which's why transactional behavior will work: I first meet with the proxy. My call is handled by the proxy. Why my insertAndDeleteTrxNew() does not work? This is because when I do this call from my process() method, I'm calling this specific instance's method not the proxy's method.

I fixed this problem by extracting this method to another Service. Now, Service X's process() method calls Service Y's insertAndDeleteTrxNew() method which implicitly calls Y's proxy and as you can guess it works.

Monday, July 11, 2011

surprise with spring while doing dependency injection to a child object

My child class (ChildDaoImpl) extends a parent class (ParentDaoImpl) and they are both managed by Spring. When I try to call a parent functionality from the child class with super.parentsMethod() I get NullPointerException.
I debug and see that the parent's injections are not complete at all. The DataSource field that should be injected in ParentDaoImpl is null. But the injection definitions seem ok in application context so where's the problem? I call the parent's bean separately and it works!

Spring does not trace the whole object hierarchy from the child class to the first ancestor which means that it does not do the necessary dependency injections to the parent chain. Thus my DataSource field in the ParentDaoImpl is not injected.
If you extend some class in your java code and you want the child class to work properly with parents' functionality you have to tell Spring explicitly that your child class has a parent.

So your application context should go from