maven依赖地址:
文档地址:
https://shardingsphere.apache.org/document/legacy/3.x/document/cn/manual/sharding-jdbc/configuration/config-spring-namespace/
https://shardingsphere.apache.org/document/legacy/4.x/document/cn/manual/sharding-jdbc/configuration/config-spring-namespace/
PoolingDataSource$PoolGuardConnectionWrapper.getSchema()
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTemplate' defined in file [D:\XXXXXX\target\XXXXXXXX\WEB-INF\classes\spring\applicationContext-sharding-jdbc.xml]: Cannot resolve reference to bean 'shardingDataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shardingDataSource': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.shardingsphere.shardingjdbc.spring.datasource.SpringShardingDataSource]: Constructor threw exception; nested exception is java.lang.AbstractMethodError: org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.getSchema()Ljava/lang/String;
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1531) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByName(AbstractAutowireCapableBeanFactory.java:1294) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1244) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
... 90 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shardingDataSource': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.shardingsphere.shardingjdbc.spring.datasource.SpringShardingDataSource]: Constructor threw exception; nested exception is java.lang.AbstractMethodError: org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.getSchema()Ljava/lang/String;
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:279) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
... 102 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.shardingsphere.shardingjdbc.spring.datasource.SpringShardingDataSource]: Constructor threw exception; nested exception is java.lang.AbstractMethodError: org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.getSchema()Ljava/lang/String;
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:122) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:271) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
... 111 common frames omitted
Caused by: java.lang.AbstractMethodError: org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.getSchema()Ljava/lang/String;
at org.apache.shardingsphere.sql.parser.binder.metadata.util.JdbcUtil.getSchema(JdbcUtil.java:41) ~[shardingsphere-sql-parser-binder-4.1.1.jar:4.1.1]
at org.apache.shardingsphere.sql.parser.binder.metadata.column.ColumnMetaDataLoader.isTableExist(ColumnMetaDataLoader.java:108) ~[shardingsphere-sql-parser-binder-4.1.1.jar:4.1.1]
at org.apache.shardingsphere.sql.parser.binder.metadata.column.ColumnMetaDataLoader.load(ColumnMetaDataLoader.java:56) ~[shardingsphere-sql-parser-binder-4.1.1.jar:4.1.1]
at org.apache.shardingsphere.sql.parser.binder.metadata.table.TableMetaDataLoader.load(TableMetaDataLoader.java:46) ~[shardingsphere-sql-parser-binder-4.1.1.jar:4.1.1]
at org.apache.shardingsphere.core.metadata.ShardingMetaDataLoader.load(ShardingMetaDataLoader.java:83) ~[sharding-core-common-4.1.1.jar:4.1.1]
at org.apache.shardingsphere.core.metadata.ShardingMetaDataLoader.loadShardingSchemaMetaData(ShardingMetaDataLoader.java:134) ~[sharding-core-common-4.1.1.jar:4.1.1]
at org.apache.shardingsphere.core.metadata.ShardingMetaDataLoader.load(ShardingMetaDataLoader.java:125) ~[sharding-core-common-4.1.1.jar:4.1.1]
at org.apache.shardingsphere.shardingjdbc.jdbc.core.context.ShardingRuntimeContext.loadSchemaMetaData(ShardingRuntimeContext.java:65) ~[sharding-jdbc-core-4.1.1.jar:4.1.1]
at org.apache.shardingsphere.shardingjdbc.jdbc.core.context.MultipleDataSourcesRuntimeContext.createMetaData(MultipleDataSourcesRuntimeContext.java:57) ~[sharding-jdbc-core-4.1.1.jar:4.1.1]
at org.apache.shardingsphere.shardingjdbc.jdbc.core.context.MultipleDataSourcesRuntimeContext.<init>(MultipleDataSourcesRuntimeContext.java:51) ~[sharding-jdbc-core-4.1.1.jar:4.1.1]
at org.apache.shardingsphere.shardingjdbc.jdbc.core.context.ShardingRuntimeContext.<init>(ShardingRuntimeContext.java:49) ~[sharding-jdbc-core-4.1.1.jar:4.1.1]
at org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource.<init>(ShardingDataSource.java:54) ~[sharding-jdbc-core-4.1.1.jar:4.1.1]
at org.apache.shardingsphere.shardingjdbc.spring.datasource.SpringShardingDataSource.<init>(SpringShardingDataSource.java:35) ~[sharding-jdbc-spring-namespace-4.1.1.jar:4.1.1]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_231]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_231]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_231]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_231]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
两者版本不兼容导致的异常,如果只是想解决问题,直接拉到最底,替换下对应jar版本即可。
如上异常,sharing-jdbc-core和commons-dbcp版本
<!-- Sharing JDBC -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>4.1.1</version>
</dependency>
<!--dbcp连接池-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.3</version>
</dependency>
sharing-jdbc-core 4.1.1版本里面使用到Connection#getSchema方法,
1.3版本的dbcp PoolingDataSource$PoolGuardConnectionWrapper里没有实现该方法,两者版本不兼容,导致AbstractMethodError异常。
第一次版本替换,将sharding-jdbc-core降到4.0.1,
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>4.0.1</version>
</dependency>
<!--dbcp连接池-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.3</version>
</dependency>
又报如下异常
Exception in thread "main" java.lang.AbstractMethodError: Method org/apache/commons/dbcp/DelegatingDatabaseMetaData.supportsStoredFunctionsUsingCallSyntax()Z is abstract
at org.apache.commons.dbcp.DelegatingDatabaseMetaData.supportsStoredFunctionsUsingCallSyntax(DelegatingDatabaseMetaData.java)
at org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.metadata.CachedDatabaseMetaData.<init>(CachedDatabaseMetaData.java:430)
at org.apache.shardingsphere.shardingjdbc.jdbc.core.context.ShardingRuntimeContext.createCachedDatabaseMetaData(ShardingRuntimeContext.java:68)
at org.apache.shardingsphere.shardingjdbc.jdbc.core.context.ShardingRuntimeContext.<init>(ShardingRuntimeContext.java:60)
at org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource.<init>(ShardingDataSource.java:48)
at org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory.createDataSource(ShardingDataSourceFactory.java:50)
at com.alipay.datasource.test.sharding.jdbc.Main.main(Main.java:46)
还是版本不兼容导致的,1.3版本dbcp对应的supportsStoredFunctionsUsingCallSyntax
方法被注释掉了。
经过测试,目前兼容的版本如下:
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>4.0.1</version>
</dependency>
<!--dbcp连接池-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:sharding="http://shardingsphere.apache.org/schema/shardingsphere/sharding"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://shardingsphere.apache.org/schema/shardingsphere/sharding
http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://shardingsphere.apache.org/schema/shardingsphere/sharding
http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd">
<!-- 数据源配置, 使用应用中的DBCP数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.datasource.driverClassName}"/>
<property name="url" value="${jdbc.datasource.url}"/>
<property name="username" value="${jdbc.datasource.username}"/>
<property name="password" value="${jdbc.datasource.password}"/>
<!-- 初始化连接大小 -->
<property name="initialSize" value="${jdbc.datasource.initialSize}"/>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${jdbc.datasource.maxActive}"/>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${jdbc.datasource.maxIdle}"/>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${jdbc.datasource.minIdle}"/>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${jdbc.datasource.maxWait}"/>
<!-- 每2分钟运行一次空闲连接回收器 -->
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.datasource.timeBetweenEvictionRunsMillis}"/>
<!--池中的连接空闲10分钟后被回收,默认值是30分钟。 -->
<property name="minEvictableIdleTimeMillis" value="${jdbc.datasource.minEvictableIdleTimeMillis}"/>
<property name="defaultAutoCommit" value="${jdbc.datasource.defaultAutoCommit}"/>
<!-- 指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除-->
<property name="testWhileIdle" value="${jdbc.datasource.testWhileIdle}"/>
<!--优化 :验证连接是否可用,使用的SQL语句-->
<property name="validationQuery" value="${jdbc.datasource.validationQuery}"/>
<property name="testOnBorrow" value="${jdbc.datasource.testOnBorrow}"/>
<property name="testOnReturn" value="${jdbc.datasource.testOnReturn}"/>
<property name="numTestsPerEvictionRun" value="${jdbc.datasource.numTestsPerEvictionRun}"/>
<property name="validationQueryTimeout" value="${jdbc.datasource.validationQueryTimeout}"/>
<!-- 是否自动回收超时连接-->
<property name="removeAbandoned" value="${jdbc.datasource.removeAbandoned}"/>
<!-- 超时时间(以秒数为单位)-->
<property name="removeAbandonedTimeout" value="${jdbc.datasource.removeAbandonedTimeout}"/>
</bean>
<!-- 可能不同的业务表有不同的业务规则,会出现好多个分库,分表策略-->
<bean id="preciseModuloTableShardingAlgorithm" class="com.bilibili.sdk.sharding.xxxxxxxxTableShardingStrategy"/>
<bean id="thirdAccountTableShardingAlgorithm" class="com.bilibili.sdk.sharding.xxxxxxxxxxTableShardingStrategy"/>
<bean id="sealBetaAccountTableShardingAlgorithm"
class="com.bilibili.sdk.sharding.xxxxxxxxTableShardingStrategy"/>
<!-- 可能不同的业务表有不同的业务规则,会出现好多个分库,分表策略,这块指定了-->
<sharding:standard-strategy id="tableShardingStrategy" sharding-column="source"
precise-algorithm-ref="preciseModuloTableShardingAlgorithm"/>
<sharding:standard-strategy id="thirdAccountTablesShardingStrategy" sharding-column="source"
precise-algorithm-ref="thirdAccountTableShardingAlgorithm"/>
<sharding:standard-strategy id="sealBetaAccountTableShardingStrategy" sharding-column="game_id"
precise-algorithm-ref="sealBetaAccountTableShardingAlgorithm"/>
<sharding:data-source id="shardingDataSource">
<sharding:sharding-rule data-source-names="dataSource">
<sharding:table-rules>
<!--logic-table: 逻辑表名(未分表时的表名);actual-data-nodes:真实数据节点,由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持inline表达式-->
<sharding:table-rule logic-table="xxxxx"
actual-data-nodes="dataSource.xxxxxx_$->{['7']}"
table-strategy-ref="tableShardingStrategy"
/>
<!-- 原字段 generate-key-column-name="source"-->
<sharding:table-rule logic-table="xxxxxxx"
actual-data-nodes="dataSource.XXXXXX_$->{['7']}"
table-strategy-ref="tableShardingStrategy"
/>
<!-- 原字段 generate-key-column-name="source"-->
<!--generate-key-column-name (?) 属性 自增列名称,缺省表示不使用自增主键生成器-->
<!-- 第三方登录数据表 -->
<sharding:table-rule logic-table="third_account_info"
actual-data-nodes="dataSource.third_account_info_$->{['jp']}"
table-strategy-ref="thirdAccountTablesShardingStrategy"
/>
<!-- 原字段 generate-key-column-name="source"-->
<sharding:table-rule logic-table="third_id_relation"
actual-data-nodes="dataSource.third_id_relation_$->{['jp']}"
table-strategy-ref="thirdAccountTablesShardingStrategy"
/>
<!-- 原字段 generate-key-column-name="source"-->
<!-- 封测用户表 -->
<sharding:table-rule logic-table="seal_beta_account"
actual-data-nodes="dataSource.seal_beta_account_$->{['jp']}"
table-strategy-ref="sealBetaAccountTableShardingStrategy"
/>
<!-- 原字段 generate-key-column-name="game_id"-->
</sharding:table-rules>
</sharding:sharding-rule>
<sharding:props>
<!-- 打印sharding执行的sql语句,可查看逻辑表和真实表执行情况;还需配置pom.xml并添加logback.xml -->
<prop key="sql.show">true</prop>
</sharding:props>
</sharding:data-source>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bilibili.sdk.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" name="sqlSessionFactory">
<property name="dataSource" ref="shardingDataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="mapperLocations">
<list>
<value>classpath:mapper/*.xml</value>
</list>
</property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
<!-- jdbc的方言,提供类似hibernate的数据库分页查询,其它方言:PostgreSQLDialect,MySQLDialect -->
<bean id="dialect" class="cn.org.rapid_framework.jdbc.dialect.MySQLDialect"/>
<!--TransactionManager-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="shardingDataSource"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="shardingDataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="cacheManagerFactory"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="cacheManagerFactory"/>
<!-- 声明cacheManager -->
<cache:annotation-driven cache-manager="cacheManager"/>
</beans>
显示sharding sql的具体情况
<sharding:props>
<!-- 打印sharding执行的sql语句,可查看逻辑表和真实表执行情况;还需配置pom.xml并添加logback.xml -->
<prop key="sql.show">true</prop>
</sharding:props>