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(records);
}


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





to



Monday, June 13, 2011

problem while using dbcp with oracle database

We have two different databases in a project; an Oracle and a MySql one. Both of them need connection pooling for a better performance. First, I add dbcp context for mysql to my application context. The settings were quite straightforward. But when I try to use the same settings (except for the connection url and the driver class name of course) for the Oracle data source I got the following exception:

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (ORA-00923: FROM keyword not found where expected

which can be translated as "there's some FROM statement missing somewhere while setting your connection". My validation query for MySql was just "SELECT 1" which annoys Oracle database. I change it to "SELECT 1 FROM DUAL" and it's fixed.

Tuesday, June 7, 2011

three screens in vlc media player while playing wmv

If you're trying to watch a video of Windows Media Video (WMV) format with vlc player but vlc is opening three windows of the video in the same time let me give you a hand. First open Video - Video Track from the menu and then select disable and then track 1. One window is better than three for a movie, right?