fyuan1030 commented on issue #8475:
URL: https://github.com/apache/shardingsphere/issues/8475#issuecomment-737695117
> Get it. Is the problem resolved now?
> @fyuan1030
no,to be honest,I think it as this class which in version 4.1.1 was changed;
in version 4.1.1
```
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.shardingsphere.shardingjdbc.spring.boot;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.core.yaml.swapper.MasterSlaveRuleConfigurationYamlSwapper;
import
org.apache.shardingsphere.core.yaml.swapper.ShardingRuleConfigurationYamlSwapper;
import
org.apache.shardingsphere.core.yaml.swapper.impl.ShadowRuleConfigurationYamlSwapper;
import
org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.shardingjdbc.api.EncryptDataSourceFactory;
import
org.apache.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.ShadowDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import
org.apache.shardingsphere.shardingjdbc.spring.boot.common.SpringBootPropertiesConfigurationProperties;
import
org.apache.shardingsphere.shardingjdbc.spring.boot.encrypt.EncryptRuleCondition;
import
org.apache.shardingsphere.shardingjdbc.spring.boot.encrypt.SpringBootEncryptRuleConfigurationProperties;
import
org.apache.shardingsphere.shardingjdbc.spring.boot.masterslave.MasterSlaveRuleCondition;
import
org.apache.shardingsphere.shardingjdbc.spring.boot.masterslave.SpringBootMasterSlaveRuleConfigurationProperties;
import
org.apache.shardingsphere.shardingjdbc.spring.boot.shadow.ShadowRuleCondition;
import
org.apache.shardingsphere.shardingjdbc.spring.boot.shadow.SpringBootShadowRuleConfigurationProperties;
import
org.apache.shardingsphere.shardingjdbc.spring.boot.sharding.ShardingRuleCondition;
import
org.apache.shardingsphere.shardingjdbc.spring.boot.sharding.SpringBootShardingRuleConfigurationProperties;
import
org.apache.shardingsphere.spring.boot.datasource.DataSourcePropertiesSetterHolder;
import org.apache.shardingsphere.spring.boot.util.DataSourceUtil;
import org.apache.shardingsphere.spring.boot.util.PropertyUtil;
import
org.apache.shardingsphere.transaction.spring.ShardingTransactionTypeScanner;
import
org.apache.shardingsphere.underlying.common.config.inline.InlineExpressionParser;
import
org.apache.shardingsphere.underlying.common.exception.ShardingSphereException;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import
org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.jndi.JndiObjectFactoryBean;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Spring boot starter configuration.
*/
@Configuration
@ComponentScan("org.apache.shardingsphere.spring.boot.converter")
@EnableConfigurationProperties({
SpringBootShardingRuleConfigurationProperties.class,
SpringBootMasterSlaveRuleConfigurationProperties.class,
SpringBootEncryptRuleConfigurationProperties.class,
SpringBootPropertiesConfigurationProperties.class,
SpringBootShadowRuleConfigurationProperties.class})
@ConditionalOnProperty(prefix = "spring.shardingsphere", name = "enabled",
havingValue = "true", matchIfMissing = true)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@RequiredArgsConstructor
public class SpringBootConfiguration implements EnvironmentAware {
private final SpringBootShardingRuleConfigurationProperties shardingRule;
private final SpringBootMasterSlaveRuleConfigurationProperties
masterSlaveRule;
private final SpringBootEncryptRuleConfigurationProperties encryptRule;
private final SpringBootShadowRuleConfigurationProperties shadowRule;
private final SpringBootPropertiesConfigurationProperties props;
private final Map<String, DataSource> dataSourceMap = new
LinkedHashMap<>();
private final String jndiName = "jndi-name";
/**
* Get sharding data source bean.
*
* @return data source bean
* @throws SQLException SQL exception
*/
@Bean
@Conditional(ShardingRuleCondition.class)
public DataSource shardingDataSource() throws SQLException {
return ShardingDataSourceFactory.createDataSource(dataSourceMap, new
ShardingRuleConfigurationYamlSwapper().swap(shardingRule), props.getProps());
}
/**
* Get master-slave data source bean.
*
* @return data source bean
* @throws SQLException SQL exception
*/
@Bean
@Conditional(MasterSlaveRuleCondition.class)
public DataSource masterSlaveDataSource() throws SQLException {
return MasterSlaveDataSourceFactory.createDataSource(dataSourceMap,
new MasterSlaveRuleConfigurationYamlSwapper().swap(masterSlaveRule),
props.getProps());
}
/**
* Get encrypt data source bean.
*
* @return data source bean
* @throws SQLException SQL exception
*/
@Bean
@Conditional(EncryptRuleCondition.class)
public DataSource encryptDataSource() throws SQLException {
return
EncryptDataSourceFactory.createDataSource(dataSourceMap.values().iterator().next(),
new EncryptRuleConfigurationYamlSwapper().swap(encryptRule), props.getProps());
}
/**
* Get shadow data source bean.
*
* @return data source bean
* @throws SQLException SQL exception
*/
@Bean
@Conditional(ShadowRuleCondition.class)
public DataSource shadowDataSource() throws SQLException {
return ShadowDataSourceFactory.createDataSource(dataSourceMap, new
ShadowRuleConfigurationYamlSwapper().swap(shadowRule), props.getProps());
}
/**
* Create sharding transaction type scanner.
*
* @return sharding transaction type scanner
*/
@Bean
public ShardingTransactionTypeScanner shardingTransactionTypeScanner() {
return new ShardingTransactionTypeScanner();
}
@Override
public final void setEnvironment(final Environment environment) {
String prefix = "spring.shardingsphere.datasource.";
for (String each : getDataSourceNames(environment, prefix)) {
try {
dataSourceMap.put(each, getDataSource(environment, prefix,
each));
} catch (final ReflectiveOperationException ex) {
throw new ShardingSphereException("Can't find datasource
type!", ex);
} catch (final NamingException namingEx) {
throw new ShardingSphereException("Can't find JNDI
datasource!", namingEx);
}
}
}
private List<String> getDataSourceNames(final Environment environment,
final String prefix) {
StandardEnvironment standardEnv = (StandardEnvironment) environment;
standardEnv.setIgnoreUnresolvableNestedPlaceholders(true);
return null == standardEnv.getProperty(prefix + "name")
? new InlineExpressionParser(standardEnv.getProperty(prefix
+ "names")).splitAndEvaluate() :
Collections.singletonList(standardEnv.getProperty(prefix + "name"));
}
@SuppressWarnings("unchecked")
private DataSource getDataSource(final Environment environment, final
String prefix, final String dataSourceName) throws
ReflectiveOperationException, NamingException {
Map<String, Object> dataSourceProps =
PropertyUtil.handle(environment, prefix + dataSourceName.trim(), Map.class);
Preconditions.checkState(!dataSourceProps.isEmpty(), "Wrong
datasource properties!");
if (dataSourceProps.containsKey(jndiName)) {
return
getJndiDataSource(dataSourceProps.get(jndiName).toString());
}
DataSource result =
DataSourceUtil.getDataSource(dataSourceProps.get("type").toString(),
dataSourceProps);
DataSourcePropertiesSetterHolder.getDataSourcePropertiesSetterByType(dataSourceProps.get("type").toString()).ifPresent(
dataSourcePropertiesSetter ->
dataSourcePropertiesSetter.propertiesSet(environment, prefix, dataSourceName,
result));
return result;
}
private DataSource getJndiDataSource(final String jndiName) throws
NamingException {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
bean.setResourceRef(true);
bean.setJndiName(jndiName);
bean.setProxyInterface(DataSource.class);
bean.afterPropertiesSet();
return (DataSource) bean.getObject();
}
}
```
there is a
```
@ConditionalOnProperty(prefix = "spring.shardingsphere", name = "enabled",
havingValue = "true", matchIfMissing = true)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
```
so the datasource can inject before spring‘s autoconfig
in versin 5.0.0
```
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.shardingsphere.spring.boot.datasource;
import com.google.common.base.Preconditions;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import
org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineExpressionParser;
import
org.apache.shardingsphere.spring.boot.datasource.prop.impl.DataSourcePropertiesSetterHolder;
import org.apache.shardingsphere.spring.boot.util.DataSourceUtil;
import org.apache.shardingsphere.spring.boot.util.PropertyUtil;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.jndi.JndiObjectFactoryBean;
import org.springframework.util.StringUtils;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Data source map setter.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DataSourceMapSetter {
private static final String PREFIX = "spring.shardingsphere.datasource.";
private static final String COMMON_PREFIX =
"spring.shardingsphere.datasource.common.";
private static final String DATA_SOURCE_NAME = "name";
private static final String DATA_SOURCE_NAMES = "names";
private static final String DATA_SOURCE_TYPE = "type";
private static final String JNDI_NAME = "jndi-name";
/**
* Get data source map.
*
* @param environment spring boot environment
* @return data source map
*/
@SuppressWarnings("unchecked")
public static Map<String, DataSource> getDataSourceMap(final Environment
environment) {
Map<String, DataSource> result = new LinkedHashMap<>();
Map<String, Object> dataSourceCommonProps =
PropertyUtil.handle(environment, COMMON_PREFIX, Map.class);
for (String each : getDataSourceNames(environment, PREFIX)) {
try {
result.put(each, getDataSource(environment, PREFIX, each,
dataSourceCommonProps));
} catch (final ReflectiveOperationException ex) {
throw new ShardingSphereException("Can't find data source
type.", ex);
} catch (final NamingException ex) {
throw new ShardingSphereException("Can't find JNDI data
source.", ex);
}
}
return result;
}
private static List<String> getDataSourceNames(final Environment
environment, final String prefix) {
StandardEnvironment standardEnv = (StandardEnvironment) environment;
standardEnv.setIgnoreUnresolvableNestedPlaceholders(true);
String dataSourceNames = standardEnv.getProperty(prefix +
DATA_SOURCE_NAME);
if (StringUtils.isEmpty(dataSourceNames)) {
dataSourceNames = standardEnv.getProperty(prefix +
DATA_SOURCE_NAMES);
}
return new
InlineExpressionParser(dataSourceNames).splitAndEvaluate();
}
@SuppressWarnings("unchecked")
private static DataSource getDataSource(final Environment environment,
final String prefix, final String dataSourceName,
final Map<String, Object>
dataSourceCommonProps) throws ReflectiveOperationException, NamingException {
Map<String, Object> dataSourceProps =
mergedDataSourceProps(PropertyUtil.handle(environment, prefix +
dataSourceName.trim(), Map.class), dataSourceCommonProps);
Preconditions.checkState(!dataSourceProps.isEmpty(),
String.format("Wrong datasource [%s] properties.", dataSourceName));
if (dataSourceProps.containsKey(JNDI_NAME)) {
return
getJNDIDataSource(dataSourceProps.get(JNDI_NAME).toString());
}
DataSource result =
DataSourceUtil.getDataSource(dataSourceProps.get(DATA_SOURCE_TYPE).toString(),
dataSourceProps);
DataSourcePropertiesSetterHolder.getDataSourcePropertiesSetterByType(dataSourceProps.get(DATA_SOURCE_TYPE).toString()).ifPresent(
propsSetter -> propsSetter.propertiesSet(environment, prefix,
dataSourceName, result));
return result;
}
private static Map<String, Object> mergedDataSourceProps(final
Map<String, Object> dataSourceProps, final Map<String, Object>
dataSourceCommonProps) {
if (!dataSourceCommonProps.isEmpty()) {
dataSourceCommonProps.putAll(dataSourceProps);
return dataSourceCommonProps;
} else {
return dataSourceProps;
}
}
private static DataSource getJNDIDataSource(final String jndiName)
throws NamingException {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
bean.setResourceRef(true);
bean.setJndiName(jndiName);
bean.setProxyInterface(DataSource.class);
bean.afterPropertiesSet();
return (DataSource) bean.getObject();
}
}
```
I want to change version to 4.1.1,because my project is will be deploy
soon,I can't delay;
this is why I update version to 5.0.0 #8416 ;
when I have some time,I will continue find the reason
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]