Reputation: 993
I am using spring jdbc with spring jdbc transaction support.
Here is my configuration.
@Configuration
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
@EnableGlobalMethodSecurity(securedEnabled = true)
@PropertySource(name = "props", value = { "classpath:common/jdbc.properties", "classpath:common/mail.properties",
"classpath:common/message.properties", "classpath:common/common.properties" })
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${jdbc.url}")
private String jdbcURL;
@Value("${jdbc.username}")
private String jdbcUsername;
@Value("${jdbc.password}")
private String jdbcPassword;
@Value("${jdbc.driver}")
private String jdbcDriver;
/**
* configure jdbc datasource
*
* @return DataSource
*/
@Bean
public DataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource(jdbcURL, jdbcUsername, jdbcPassword);
dataSource.setDriverClassName(jdbcDriver);
return dataSource;
}
/**
* configure jdbc template
*
* @return JdbcTemplate
*/
@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(getDataSource());
}
@Bean
public PlatformTransactionManager txManager() {
return new DataSourceTransactionManager(getDataSource());
}
}
With this configuration transaction doesn't work for me. I am not sure with the reason but what i could understand is following -
As you can see jdbcTemplate() and txManager() both the methods are calling getDataSource() method which inturn create jdbcDataSource. I think in both the method i am creating two jdbc dataSource, so jdbcTemplate and transaction manager both are using two different dataSource.
So my questions are -
I can see it's easy to configure it in xml but with java configuration i couldn't find an example using both jdbcTemplate and transactionManager.
Upvotes: 3
Views: 3768
Reputation: 3273
Answers
Why your transactions doesn't work? It's hard to tell since you are not providing the full context where it fails to work, but some things for considerations:
Upvotes: 1