Druid连接池更换Guess数据源 dbType not support : nul
1. 遇到的问题
工作中有个db国产化的需求,需要在一个老项目中把DB从mysql更换为openGuess,将对应的驱动和Druid数据源改造后启动程序报错如下:
java.lang.IllegalStateException: dbType not support : nul, url jdbc:opengauss://127.0.0.1:15400/testdb1?autoBalance=roundrobin&targetServerType=master&loggerLevel=off
at com.alibaba.druid.wall.WallFilter.init(WallFilter.java:167)
at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:827)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
...
排查后发现:com.alibaba.druid.util.JdbcConstants中没有openGuess的类型
解决办法:使用 openGuess底层的PostgreSQL类型,下面有详细的操作步骤
2. 更换数据源操作步骤
1.添加pom依赖
<dependency>
<groupId>org.opengauss</groupId>
<artifactId>opengauss-jdbc</artifactId>
<version>5.0.0-og</version>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
2.更改jdbc.properties
druid.driverClassName = org.opengauss.Driver
druid.url = jdbc:opengauss://127.0.0.1:15400/testdb1?autoBalance=roundrobin&targetServerType=master&loggerLevel=off
druid.username = lihw
druid.password = 123456
druid.initialSize = 1
druid.minIdle = 1
druid.maxActive = 3
druid.maxWait = 900
druid.timeBetweenEvictionRunsMillis = 2000
druid.minEvictableIdleTimeMillis = 30000
druid.validationQuery = select 1
druid.testWhileIdle = true
druid.testOnBorrow = false
druid.testOnReturn = false
druid.poolPreparedStatements = false
druid.maxPoolPreparedStatementPerConnectionSize = 20
#druid.filters = stat,wall,slf4j
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=false
注意:驱动和方言的变化
- 数据库驱动:org.opengauss.Driver
- 方言:org.hibernate.dialect.PostgreSQLDialect
3.初始化Druid连接池
<bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
<property name="dbType" value="postgresql"/>
</bean>
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
</bean>
<bean id="slf4j-filter" class="com.alibaba.druid.filter.logging.Slf4jLogFilter">
</bean>
<!--druid openGuess-->
<bean id="dataSourceTarget" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password ,driverClassName-->
<property name="driverClassName" value="${druid.driverClassName}"/>
<property name="url" value="${druid.url}" />
<property name="username" value="${druid.username}" />
<property name="password" value="${druid.password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${druid.initialSize}" />
<property name="minIdle" value="${druid.minIdle}"/>
<property name="maxActive" value="${druid.maxActive}" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${druid.maxWait}" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${druid.validationQuery}" />
<property name="testWhileIdle" value="${druid.testWhileIdle}" />
<property name="testOnBorrow" value="${druid.testOnBorrow}" />
<property name="testOnReturn" value="${druid.testOnReturn}" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<!-- 如果用Oracle,则把poolPreparedStatements配置为true,
mysql可以配置为false。分库分表较多的数据库,建议配置为false。 -->
<property name="poolPreparedStatements" value="${druid.poolPreparedStatements}" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="${druid.maxPoolPreparedStatementPerConnectionSize}" />
<!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
<!-- <property name="filters" value="${druid.filters}" />-->
<property name="proxyFilters">
<list>
<ref bean="wall-filter"/>
<ref bean="stat-filter"/>
<ref bean="slf4j-filter"/>
</list>
</property>
</bean>
注意:
druid-1.1.16版本未兼容高斯的DB,使用proxyFilters过滤器,类型设置为postgresql
<bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
<property name="dbType" value="postgresql"/>
</bean>