Skip to content

SqlSessionFactory rebuild when "configLocation" and "configuration" are both null #440

Closed
@Flaming-Lin

Description

@Flaming-Lin

Version Info

mybatis-spring:2.0.3

Problem

I wanna register a "DefaultEnumTypeHandler" when creating bean "sqlSessionFactory". but if "configLocation" and "configuration" are both null, the "sqlSessionFacotry" will rebuild and my settings are invalid.

Bean register code:

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(@Qualifier("blogDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        // if there is no "configLocation" or "configuration"
        // factoryBean.setConfiguration(new org.apache.ibatis.session.Configuration());

        SqlSessionFactory factory = factoryBean.getObject();
        if (null == factory) {
            throw new Exception("No sql session factory found!");
        }
        factory.getConfiguration().getTypeHandlerRegistry().setDefaultEnumTypeHandler(AutoEnumTypeHandler.class);
        return factoryBean;
    }

Source code:

Method "afterPropertiesSet" will be called when getting the bean instance. Then it will try to build a "sqlSessionFactory".

@Override
  public void afterPropertiesSet() throws Exception {
    notNull(dataSource, "Property 'dataSource' is required");
    notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");
    state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
        "Property 'configuration' and 'configLocation' can not specified with together");

    this.sqlSessionFactory = buildSqlSessionFactory();
  }

Here is the root cause, "targetConfiguration" will be a new instance of "Configuration" when "configLocation" and "configuration" are both null. Then the "DefaultEnumTypeHandler" will initialize and my settings turn invalid.

protected SqlSessionFactory buildSqlSessionFactory() throws Exception {

    final Configuration targetConfiguration;

    XMLConfigBuilder xmlConfigBuilder = null;
    if (this.configuration != null) {
      targetConfiguration = this.configuration;
      if (targetConfiguration.getVariables() == null) {
        targetConfiguration.setVariables(this.configurationProperties);
      } else if (this.configurationProperties != null) {
        targetConfiguration.getVariables().putAll(this.configurationProperties);
      }
    } else if (this.configLocation != null) {
      xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties);
      targetConfiguration = xmlConfigBuilder.getConfiguration();
    } else {
      LOGGER.debug(
          () -> "Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration");
      // 【Here】
      targetConfiguration = new Configuration();
      Optional.ofNullable(this.configurationProperties).ifPresent(targetConfiguration::setVariables);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions