Ravi Kumar
Ravi Kumar

Reputation: 993

Java configuration for jdbctemplate and transaction management in spring

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 -

  1. Is it true jdbcTemplate and transactionManager are using two different datasource or @Bean can handle this situation.
  2. If they are both using two different dataSource then how to configure them so that they use the same dataSource.

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

Answers (1)

Ruslan
Ruslan

Reputation: 3273

Answers

  1. @Bean handles this. Since you have @Configuration on top of the class then you are operating in full (versus lite) mode and thus those java @Bean-annotated methods are intercepted by Spring and it is made sure that the method is invoked only once. BTW, I would call it dataSource() instead of getDataSource(), because the method names are for the bean names. If in doubt then put some logging and see the console to confirm.
  2. Question 2. becomes irrelevant.

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:

  1. Did you forget to put @Transactional for your methods/classes?
  2. You are using a not-so-common approach of native AspectJ weaving (versus the more common and simpler JDK proxy-based approach). I haven't used this, but according to the docs you'll have to compile/build your code differently since it is not a pure-java approach.

Upvotes: 1

Related Questions