DruidDataSource出現UnsupportedOperationException異常的情況

在開發過程中發現了這個問題,當我們配置druid數據源的時候是以下面的這種方式配置的

@Bean
@ConfigurationProperties("spring.datasource.druid.two")
public DataSource dataSourceTwo(){
    return DruidDataSourceBuilder.create().build();
}

但是在發生 EnvironmentChangeEvent事件的時候,報出瞭如下錯誤:

Caused by: org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 1 errors
Field error in object 'spring.datasource.druid' on field 'url': rejected value [jdbc:mysql://XXXXXX]; codes [methodInvocation.spring.datasource.druid.url,methodInvocation.url,methodInvocation.java.lang.String,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.datasource.druid.url,url]; arguments []; default message [url]]; default message [Property 'url' threw exception; nested exception is java.lang.UnsupportedOperationException]
	at org.springframework.boot.bind.PropertiesConfigurationFactory.checkForBindingErrors(PropertiesConfigurationFactory.java:359)
	at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:276)
	at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240)
	at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:330)
	... 27 more

https://github.com/alibaba/druid/issues/2178,有人已經提出過這個bug,但是原因講的不是很正確

DruidDataSource初始化的時候,會將inited字段設置爲true,之後就不會再執行init方法。
public void init() throws SQLException {
        if (inited) { //初始化過一次之後,不會再執行
            return;
        }

        。。。

        } catch (SQLException e) {
            LOG.error("{dataSource-" + this.getID() + "} init error", e);
            throw e;
        } catch (InterruptedException e) {
            throw new SQLException(e.getMessage(), e);
        } finally {
            inited = true; //初始化完成之後,inited爲true
            lock.unlock();

            if (init && LOG.isInfoEnabled()) {
                LOG.info("{dataSource-" + this.getID() + "} inited");
            }
        }
    }

Spring在設置 DruidDataSource屬性的時候,比如setUrl()方法,下面的代碼可以看出,如果已經初始化過,就會直接報錯

public void setUrl(String jdbcUrl) {
        if (inited) { // 如果已經初始化過,直接報錯
            throw new UnsupportedOperationException();
        }

        if (jdbcUrl != null) {
            jdbcUrl = jdbcUrl.trim();
        }

        this.jdbcUrl = jdbcUrl;

        // if (jdbcUrl.startsWith(ConfigFilter.URL_PREFIX)) {
        // this.filters.add(new ConfigFilter());
        // }
    }

當我們在方法上添加@ConfigurationProperties("")註解的時候,會將bean加入到ConfigurationPropertiesRebinder的beans屬性裏,一旦發生EnvironmentChangeEvent事件,ConfigurationPropertiesRebinder由於監聽該事件,執行onApplicationEvent方法,調用rebind,會將beans的屬性重新綁定,而我們的DruidDataSource的屬性是不允許修改的(上面的setUrl方法),因此會拋出UnsupportedOperationException異常,所以我們必須避免使用@ConfigurationProperties註解來初始化DruidDataSource。

DruidDataSourceBuilder.create().build()創建的是DruidDataSourceWrapper,而DruidDataSourceWrapper也是被@ConfigurationProperties所標註,因此我們也不能使用DruidDataSourceWrapper。

 

  • druid-spring-boot-starter: 1.1.6
  • spring-boot:1.5.10.RELEASE
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章