Add OceanBaseValidConnectionChecker and fix oceanbase dbtype

This commit is contained in:
xuping.lz
2022-02-11 14:28:35 +08:00
parent f937ecd433
commit 4786e105d6
4 changed files with 72 additions and 2 deletions
@@ -1333,7 +1333,12 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|| realDriverClassName.equals(JdbcConstants.ENTERPRISEDB_DRIVER)
|| realDriverClassName.equals(JdbcConstants.POLARDB_DRIVER)) {
this.validConnectionChecker = new PGValidConnectionChecker();
} else if (realDriverClassName.equals(JdbcConstants.OCEANBASE_DRIVER)
|| (realDriverClassName.equals(JdbcConstants.OCEANBASE_DRIVER2))) {
DbType dbType = DbType.of(this.dbTypeName);
this.validConnectionChecker = new OceanBaseValidConnectionChecker(dbType);
}
}
private void initExceptionSorter() {
@@ -0,0 +1,63 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.pool.vendor;
import com.alibaba.druid.DbType;
import com.alibaba.druid.pool.ValidConnectionChecker;
import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
import com.alibaba.druid.util.JdbcUtils;
import sun.security.pkcs11.Secmod;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class OceanBaseValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker {
private String oracleModeValidateQuery = "SELECT 'x' FROM DUAL";
private String mysqlModeValidateQuery = "SELECT 'x'";
private DbType dbType;
public OceanBaseValidConnectionChecker() { configFromProperties(System.getProperties()); dbType = null; }
public OceanBaseValidConnectionChecker(DbType dbType) {this.dbType = dbType;configFromProperties(System.getProperties()); }
public boolean isValidConnection(final Connection c, String validateQuery, int validationQueryTimeout) throws Exception {
if (c.isClosed()) {
return false;
}
if (validateQuery == null || validateQuery.isEmpty()) {
if(dbType != null) {
if(dbType == DbType.oceanbase) {
validateQuery = mysqlModeValidateQuery;
} else {
validateQuery = oracleModeValidateQuery;
}
}
}
Statement stmt = null;
try {
stmt = c.createStatement();
if (validationQueryTimeout > 0) {
stmt.setQueryTimeout(validationQueryTimeout);
}
stmt.execute(validateQuery);
return true;
} catch (SQLException e) {
throw e;
} finally {
JdbcUtils.close(stmt);
}
}
}
@@ -85,6 +85,7 @@ public interface JdbcConstants {
DbType OCEANBASE = DbType.oceanbase;
DbType OCEANBASE_ORACLE = DbType.oceanbase_oracle;
String OCEANBASE_DRIVER = "com.alipay.oceanbase.jdbc.Driver";
String OCEANBASE_DRIVER2 = "com.oceanbase.jdbc.Driver";
DbType INFORMIX = DbType.informix;
@@ -150,4 +151,5 @@ public interface JdbcConstants {
*/
DbType OSCAR = DbType.oscar;
String OSCAR_DRIVER = "com.oscar.Driver";
}
@@ -928,13 +928,13 @@ public final class JdbcUtils implements JdbcConstants {
public static boolean isOracleDbType(String dbType) {
return DbType.oracle.name().equals(dbType) || //
DbType.oceanbase.name().equals(dbType) || //
DbType.oceanbase_oracle.name().equals(dbType) || //
DbType.ali_oracle.name().equalsIgnoreCase(dbType);
}
public static boolean isOracleDbType(DbType dbType) {
return DbType.oracle == dbType || //
DbType.oceanbase == dbType || //
DbType.oceanbase_oracle == dbType || //
DbType.ali_oracle == dbType;
}