美文网首页
事务管理器,数据源,JdbcTemplate

事务管理器,数据源,JdbcTemplate

作者: Nisus_Liu | 来源:发表于2018-06-12 15:36 被阅读0次

使用jdbcTemplate需要引入依赖:

<!--jdbc template-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

然后用一个配置类生成JdbcTemplate实例, 交给spring容器

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        //!<1> 注意这里有坑, 看下文!
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password); //totest
        dataSource.setInitialSize(10);
        
        return new JdbcTemplate(dataSource);
    }

到这里, 启动, spring会报错:

org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).

[解决] autoconfigure需要排除.
@SpringBootApplication(exclude = {
        DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class
})

这时一切貌似美好起来了.

but....'

当你需要使用事务管理时.

按照教程, 使用两个注解:

@EnableTransactionManagement
@Transactional

然后, 你会碰到:

`PlatformTransactionManager`找不到

这是因为: 参考 Spring Boot 事务的使用

关于事务管理器,不管是JPA还是JDBC等都实现自接口 PlatformTransactionManager 如果你添加的是 spring-boot-starter-jdbc 依赖,框架会默认注入 DataSourceTransactionManager 实例。如果你添加的是 spring-boot-starter-data-jpa 依赖,框架会默认注入 JpaTransactionManager 实例。

因为上文, DataSource是我自己在方法内部new的, 没有交给spring管理, 所以, 它在创建事务管理器时, 由于找不到可用的DataSource, 就不会给我创建了. 这时@Transactional在需要使用事务管理器, 就报找不到了.

彻底解决

  • 自己生产DataSource, 交个spring管理
  • 若更进一步, 可以自己生产PlatformTransactionManager 实现类(没有必要了)
@Configuration
@PropertySource("classpath:mysql.properties")
@Slf4j
public class JdbcConf {
    @Value("${jdbc.mysql.driver}")
    private String driver;
    @Value("${jdbc.mysql.url}")
    private String url;
    @Value("${jdbc.mysql.username}")
    private String username;
    @Value("${jdbc.mysql.password}")
    private String password;


    @Bean     //声明其为Bean实例
    @Primary  //在同样的DataSource中,首先使用被标注的DataSource
    public DataSource dataSource() {
        //生成 DataSource
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password); //totest
        dataSource.setInitialSize(5);

        return dataSource;
    }



    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    // 其中 dataSource 框架会自动为我们注入
//    @Bean
//    public PlatformTransactionManager txManager(DataSource dataSource) {
//        return new DataSourceTransactionManager(dataSource);
//    }

    @Bean
    public Object testBean(PlatformTransactionManager platformTransactionManager){
        log.debug(">>Springboot为本项目注入了事务管理器: " + platformTransactionManager.getClass().getName());
        return new Object();
    }
}

相关文章

网友评论

      本文标题:事务管理器,数据源,JdbcTemplate

      本文链接:https://www.haomeiwen.com/subject/owxreftx.html