codingapi / tx-lcn

LCN distributed transaction framework, compatible with dubbo, spring cloud and Motan framework, supports various relational databases
https://www.codingapi.com
Apache License 2.0
4.22k stars 1.46k forks source link

集成shardingsphere后出现异常 java.sql.SQLFeatureNotSupportedException: getCatalog #386

Open archx opened 5 years ago

archx commented 5 years ago

1.问题描述

在demo中集成 shardingsphere 3.1.0 后出现异常

java.sql.SQLFeatureNotSupportedException: getCatalog

shardingsphere

maven pom

<dependency>
    <groupId>io.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>3.1.0</version>
</dependency>

spring boot application.yaml

sharding:
  jdbc:
    datasource:
      names: ds0,ds1
      ds0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/txlcn-demo?useUnicode=true&amp;characterEncoding=utf-8
        username: root
        password: 123456
        filters: stat,wall,slf4j
      ds1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/txlcn-demo?useUnicode=true&amp;characterEncoding=utf-8
        username: root
        password: 123456
        filters: stat,wall,slf4j
    config:
      masterslave:
        name: ms
        load-balance-algorithm-type: round_robin
        master-data-source-name: ds0
        slave-data-source-names: ds1
      props:
        sql:
          show: true

2. 环境:

3.异常信息

### Error updating database.  Cause: java.lang.RuntimeException: java.sql.SQLFeatureNotSupportedException: getCatalog
### The error may involve com.codingapi.example.demod.DDemoMapper.save-Inline
### The error occurred while setting parameters
### SQL: insert into t_demo(kid, demo_field, group_id, unit_id, create_time,app_name) values(?, ?, ?, ?, ?,?)
### Cause: java.lang.RuntimeException: java.sql.SQLFeatureNotSupportedException: getCatalog] with root cause

java.sql.SQLFeatureNotSupportedException: getCatalog
    at io.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationConnection.getCatalog(AbstractUnsupportedOperationConnection.java:91) ~[sharding-jdbc-core-3.1.0.jar:na]
    at com.codingapi.txlcn.tc.core.transaction.txc.analy.TableStructAnalyser.analyse(TableStructAnalyser.java:51) ~[txlcn-tc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
    at com.codingapi.txlcn.tc.core.transaction.txc.analy.TxcSqlExecuteInterceptor.lambda$postInsert$6(TxcSqlExecuteInterceptor.java:147) ~[txlcn-tc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
    at com.codingapi.txlcn.tc.core.context.DefaultGlobalContext.tableStruct(DefaultGlobalContext.java:122) ~[txlcn-tc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
    at com.codingapi.txlcn.tc.core.transaction.txc.analy.TxcSqlExecuteInterceptor.postInsert(TxcSqlExecuteInterceptor.java:146) ~[txlcn-tc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
    at com.codingapi.txlcn.tc.core.transaction.txc.resource.TxcJdbcEventListener.onAfterExecute(TxcJdbcEventListener.java:81) ~[txlcn-tc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
    at com.codingapi.txlcn.tc.core.transaction.txc.resource.CompoundJdbcEventListener.onAfterExecute(CompoundJdbcEventListener.java:93) ~[txlcn-tc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
    at com.codingapi.txlcn.tc.support.p6spy.wrapper.PreparedStatementWrapper.execute(PreparedStatementWrapper.java:358) ~[txlcn-tc-5.0.1.RELEASE.jar:5.0.1.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_171]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
    at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59) ~[mybatis-3.4.6.jar:3.4.6]
    at com.sun.proxy.$Proxy144.execute(Unknown Source) ~[na:na]
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:46) ~[mybatis-3.4.6.jar:3.4.6]
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74) ~[mybatis-3.4.6.jar:3.4.6]
archx commented 5 years ago

原因

使用 @TxcTransaction(propagation = DTXPropagation.SUPPORTS)注解

TxcSqlExecuteInterceptor -> TableStructAnalyser -> structRs = connection.getMetaData().getPrimaryKeys(connection.getCatalog(), null, table);

@Component
public class TableStructAnalyser {
    // ...
    public TableStruct analyse(Connection connection, String table) throws SQLException {
        ResultSet structRs = null;
        ResultSet columnSet = null;
        TableStruct tableStruct = new TableStruct(table);
        try {
            structRs = connection.getMetaData().getPrimaryKeys(connection.getCatalog(), null, table);
            columnSet = connection.getMetaData().getColumns(null, "%", table, "%");
            while (structRs.next()) {
                tableStruct.getPrimaryKeys().add(structRs.getString("COLUMN_NAME"));
            }
            while (columnSet.next()) {
                tableStruct.getColumns().put(columnSet.getString("COLUMN_NAME"), columnSet.getString("TYPE_NAME"));
            }
        } catch (SQLException e) {
            try {
                DbUtils.close(structRs);
                DbUtils.close(columnSet);
            } catch (SQLException ignored) {
            }
            throw e;
        }
        return tableStruct;
    }
    // ....
}