Skip to content

SimpleJdbcDaoSupport.getSimpleJdbcTemplate() returns null [SPR-1964] #6657

@spring-projects-issues

Description

@spring-projects-issues

Patrick Dumontel opened SPR-1964 and commented

I have a standalone class that extends SimpleJdbcDaoSupport so that I can do a quick import of test data into the database:
public class Migrator extends JdbcDaoSupport {
..
public static void main(String[] args) {
Migrator m = new Migrator();
DriverManagerDataSource ds = new DriverManagerDataSource([connection url, user, etc. go here]);
m.setDataSource(ds);
m.migrate;
}

private void migrate() {
SimpleJdbcTemplate jt = getSimpleJdbcTemplate();
jt.update([some sql here]);
}

Using a debugger, I saw that the simpleJdbcTemplate is null, although the jdbcTemplate that should be wrapped is fine. I worked around this by calling m.afterPropertiesSet(). Could it be that SimpleJdbcDaoSupport only creates the simpleJdbcTemplate if used within an application context? This did not happen in M3, in fact the SimpleJdbcTemplate's source is quite different. This method was in M3 but removed in M4:

protected JdbcTemplate createJdbcTemplate(DataSource dataSource) {
JdbcTemplate jt = new JdbcTemplate(dataSource);
this.simpleJdbcTemplate = new SimpleJdbcTemplate(jt);
return jt;
}

M4 instead has:

protected void checkDaoConfig() {
super.checkDaoConfig();
this.simpleJdbcTemplate = new SimpleJdbcTemplate(getJdbcTemplate());
}
which apparently doesn't get called in a standalone instantiation.


Affects: 2.0 M4

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions