apache / shardingsphere

Empowering Data Intelligence with Distributed SQL for Sharding, Scalability, and Security Across All Databases.
Apache License 2.0
19.95k stars 6.74k forks source link

Hello, single database sub table ,spring transaction do not work, please help to see what is the reason. #1505

Closed 1610200527 closed 5 years ago

1610200527 commented 5 years ago

Hello, I use sharding-jdbc-3.3.0, Druid, mybatis in my project The Spring annotation @transactional used in the Service layer, when I debug, I found that there was no Creating new transaction at all. This is JAVA configuration:

@MapperScan(basePackages = "xx.xx.xx.dao.main" , sqlSessionTemplateRef = "mainSqlSessionTemplate")
public class MainDataSourceConfig {

@Bean(name="mainDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DruidDataSource setDataSource() {
    return new DruidDataSource();
}

@Bean(name = "mainTransactionManager")
@Primary
public PlatformTransactionManager setTransactionManager(@Qualifier("mainDataSource") DataSource dataSource) {
    DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(this.buildDataSource(dataSource));
    return dataSourceTransactionManager;
}

@Bean(name = "mainSqlSessionFactory")
public SqlSessionFactory setSqlSessionFactory(@Qualifier("mainDataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(this.buildDataSource(dataSource));
    try{
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/main/*.xml"));
        ResourcePatternResolver resolver =new PathMatchingResourcePatternResolver();
    }catch (Exception ex){
        LoggerHelper.error(this.getClass(),ex.getMessage(),ex);
    }
    return bean.getObject();
}

@Bean(name = "mainSqlSessionTemplate")
public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("mainSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
}

public DataSource buildDataSource(@Qualifier("mainDataSource") DataSource druidDataSource) {
    DataSource dataSource = null;
    try{
        Map<String, DataSource> dataSourceMap = new HashMap<>(1);
        dataSourceMap.put("default", druidDataSource);

        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        shardingRuleConfig.getTableRuleConfigs().add(getVehicleInfoTableRuleConfiguration());
        shardingRuleConfig.getTableRuleConfigs().add(getVehilcePhotoTableRuleConfiguration());
        shardingRuleConfig.getBindingTableGroups().add("xx_info,xx_photo");
        shardingRuleConfig.setDefaultDataSourceName("default");
        shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new NoneShardingStrategyConfiguration());

        Properties properties = new Properties();
        properties.setProperty("sql.show","true");

        return ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new ConcurrentHashMap(), properties);

    }catch (Exception ex){
        Log.error(MainDataSourceConfig.class, "创建数据库链接失败!", ex);
    }
    return dataSource;
}

TableRuleConfiguration getVehicleInfoTableRuleConfiguration() {
    TableRuleConfiguration result = new TableRuleConfiguration();
    result.setLogicTable("xx_info");
    result.setActualDataNodes("default.xx_info_${0..4}");
    result.setKeyGenerator(new DefaultKeyGenerator());
    result.setTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("unified_number", new ModuleTableShardingAlgorithm()));
    result.setKeyGeneratorColumnName("id");
    return result;
}

TableRuleConfiguration getVehilcePhotoTableRuleConfiguration() {
    TableRuleConfiguration result = new TableRuleConfiguration();
    result.setLogicTable("xx_photo");
    result.setActualDataNodes("default.xx_photo_${0..4}");
    result.setTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("unified_number", new ModuleTableShardingAlgorithm()));
    result.setKeyGenerator(new DefaultKeyGenerator());
    result.setKeyGeneratorColumnName("photo_id");
    return result;
}
terrymanu commented 5 years ago

Hello, we never released version 3.3.0 for sharding-jdbc

1610200527 commented 5 years ago

sorry,I use version 3.0.0 for sharding-jdbc

1610200527 commented 5 years ago

Sorry,I have solved this problem!I create datasource by springboot- jdbc useings type is druid and set this datasource into sharding-jdbc datasource。like this: spring: datasource: name: dev url: jdbc:mysql://192.168.101.5:3306/xx?characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=true username: root password: root

使用druid数据源

driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource

============================================================================

druid configuration

============================================================================

druid: initial-size: 5 min-idle: 5 max-active: 30 max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 max-pool-prepared-statement-per-connection-size: 20 validation-query: SELECT 'x' test-while-idle: true test-on-borrow: false test-on-return: false pool-prepared-statements: false filters: stat, slf4j filter: stat: log-slow-sql: true slow-sql-millis: 10000 slf4j: result-set-log-enabled: false statement-executable-sql-log-enable: false stat-view-servlet: enabled: true reset-enable: true url-pattern: /druid/* login-username: xx login-password: xxxxx