使用shardingsphere對SQLServer坑的解決
背景:最近一個使用SQLServer的項目,業(yè)務量太大,開始對業(yè)務有影響了,因此用戶要求升級改造,技術上采用shardingsphere進行分庫分表。
經過一系列調研,設計。。。哐哐一頓操作之后開始動刀改造。pom依賴如下:
<!--sharding--> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.8</version> </dependency>
改造后查詢和寫入都各種報錯:
Caused by: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType NVARCHAR . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLFeatureNotSupportedException: setNString
at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:75)
at org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:87)
... 47 common frames omitted
Caused by: java.sql.SQLFeatureNotSupportedException: setNString
at org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationPreparedStatement.setNString(AbstractUnsupportedOperationPreparedStatement.java:57)
at org.apache.ibatis.type.NStringTypeHandler.setNonNullParameter(NStringTypeHandler.java:31)
at org.apache.ibatis.type.NStringTypeHandler.setNonNullParameter(NStringTypeHandler.java:26)
at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:73)
... 48 common frames omitted
核心錯誤:Caused by: java.sql.SQLFeatureNotSupportedException: setNString
問題分析:
網上尋了千百度,驀然回首,還是沒有找到問題,(┭┮﹏┭┮) 最后debug斷點跟了源碼發(fā)現:
操作數據庫的PreparedStatement 是ShardingPreparedStatement
然后setNString支持SQLServerPreparedStatement 不支持ShardingPreparedStatement(改造前沒問題,改造后出問題的原因)
問題解決:
找到問題了,下面就是解決問題了,既然沒有setNString的實現,那就實現一個唄;
第一步 實現NVarcharTypeHandler:
package cn.preserve.config.mybatis; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedJdbcTypes; import org.apache.ibatis.type.TypeException; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * 將 nvarchar 轉成 varchar sharingJDBC不支持nvarchar * 主要是NStringTypeHandler中,沒有setNString() */ @MappedJdbcTypes(JdbcType.NVARCHAR) public class NVarcharTypeHandler extends BaseTypeHandler<String> { @Override public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { if(parameter == null) { if(jdbcType == null) { throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters."); } try { ps.setNull(i, jdbcType.TYPE_CODE); } catch (SQLException var7) { throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . " + "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. " + "Cause: " + var7, var7); } } else { try { this.setNonNullParameter(ps, i, parameter, jdbcType); } catch (Exception var6) { throw new TypeException("Error setting non null for parameter #" + i + " with JdbcType " + jdbcType + " . " + "Try setting a different JdbcType for this parameter or a different configuration property. " + "Cause: " + var6, var6); } } } /** * 這里使用setNString而不是setString * @param ps * @param i * @param parameter * @param jdbcType * @throws SQLException */ @Override public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, parameter); } @Override public String getNullableResult(ResultSet rs, String columnName) throws SQLException { return rs.getString(columnName); } @Override public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return rs.getString(columnIndex); } @Override public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return cs.getString(columnIndex); } }
第二步 實現加入數據庫配置:
由于我是代理實現的數據庫,所有在代碼中加入即可
@Configuration public class DataSourceConfig { @Bean(name = "sqlSessionFactory") @Primary public SqlSessionFactory memberDb1SqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = getSqlSessionFactoryBean(dataSource); bean.setTypeHandlers(new TypeHandler[] {new NVarcharTypeHandler()}); return bean.getObject(); } // ******* 其他實現 }
PS:如果是配置只修要在mybatis-config.xml中配置一下
<typeHandlers> <typeHandler handler="cn.preserve.config.mybatis.NVarcharTypeHandler"/> </typeHandlers>
配置完成后,測試以前功能全部正常(#^.^#)
如果嫌麻煩,有另外一種解決方案:將mapper.xml中的NVARCHAR替換從VARCHAR也可以哦
到此這篇關于使用shardingsphere對SQLServer坑的解決的文章就介紹到這了,更多相關shardingsphere SQLServer內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot整合Docker實現一次構建到處運行的操作方法
本文講解的是 SpringBoot 引入容器化技術 Docker 實現一次構建到處運行,包括鏡像構建、Docker倉庫搭建使用、Docker倉庫可視化UI等內容,需要的朋友可以參考下2022-10-10詳解在spring boot中消息推送系統(tǒng)設計與實現
這篇文章主要介紹了詳解在spring boot中消息推送系統(tǒng)設計與實現,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05