PDA

View Full Version : Confusion about jdbcTemplate



colin_young
Mar 27th, 2006, 09:16 PM
I'm attempting to follow the instructions in "Spring In Action" for jdbc data access, and I'm a bit confused about the proper way to do it. The book defines the class like this:


public class Deals implements JdbcDAO
{
private JdbcTemplate jdbcTemplate;

public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
{
this.jdbcTemplate = jdbcTemplate;
}


But the downloaded code derives from JdbcDaoSupport (which provides a setter for jdbcTemplate). In any case, neither version works. The first version causes a null pointer exception when I try to access the jdbcTemplate methods (e.g. execute, query, etc.) and the second version doesn't do anything as far as I can tell (if I could get logging to work I might have more info -- I have another post about that problem).

It appears almost as if Spring is not wiring things up properly if I don't extend the JdbcDaoSupport class. Ideally, I'd like to be able to set 2 data sources, so I'd really like to be able to use provide 2 setters.

Is there a good document somewhere that can get me going? Not that SIA isn't good, but I'm obviously missing something.

Thanks for any pointers.

Colin

Costin Leau
Mar 28th, 2006, 02:26 AM
JdbcDaoSupport setJdbcTemplate is final - subclasses can't override it. What is your application context?
For two datasources you can use either two jdbcTemplates (define for example another setSecondJdbcTemplate) or use a multiplexed (thread scoped) datasource that at runtime can switch the underlying datasource. However, the latter solution is more complicated and not suitable for a beginner.
Besides the reference documentation and the samples (which show a lot of Spring features) you can take a look at the rest of the books from www.springframework.org/documentation.

harry
Mar 28th, 2006, 02:38 AM
Hi Colin,

You are getting nullpointer exception because you are not setting the datasource in the JdbcTemplate object.The extended class should be JdbcDaoSupport.This class has setter and getter for both jdbctemplate and datasource.

You can set the datasource in xml and in the constructor use createJdbcTemplate method to create a JdbcTemplate with the set datasource.

Hope that helps.

colin_young
Mar 28th, 2006, 08:06 AM
I don't believe it...I think (I'll need to look at the code tonight when I have access to the computer it's on) that I am directly creating my objects in the unit tests rather than using a Spring factory which would explain both why I'm getting a null poiner exception and why nothing is being logged.

That should teach me to code whithout getting enough sleep...

You've been very helpful. I think now I'll be able to get things going now (and with your response to my logging query in the core forum I'll be able to tidy up my log4j configuration).

As for the 2 datasources, I've already coded up the first approach (2 templates) and I was just confused by the fact that the first one wasn't being set to anything. Maybe giving Spring a chance to actually create and wire up my objects will give me better results :).

Thanks for your help.


JdbcDaoSupport setJdbcTemplate is final - subclasses can't override it. What is your application context?
For two datasources you can use either two jdbcTemplates (define for example another setSecondJdbcTemplate) or use a multiplexed (thread scoped) datasource that at runtime can switch the underlying datasource. However, the latter solution is more complicated and not suitable for a beginner.
Besides the reference documentation and the samples (which show a lot of Spring features) you can take a look at the rest of the books from www.springframework.org/documentation.