mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved postgres support
This commit is contained in:
@@ -70,6 +70,7 @@ import com.alibaba.druid.pool.vendor.MySqlValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.vendor.OracleExceptionSorter;
|
||||
import com.alibaba.druid.pool.vendor.OracleValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.vendor.PGExceptionSorter;
|
||||
import com.alibaba.druid.pool.vendor.PGValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.vendor.SybaseExceptionSorter;
|
||||
import com.alibaba.druid.proxy.DruidDriver;
|
||||
import com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig;
|
||||
@@ -887,12 +888,14 @@ public class DruidDataSource extends DruidAbstractDataSource
|
||||
|
||||
private void initValidConnectionChecker() {
|
||||
String realDriverClassName = driver.getClass().getName();
|
||||
if (realDriverClassName.equals("com.mysql.jdbc.Driver")) {
|
||||
if (realDriverClassName.equals(JdbcConstants.MYSQL_DRIVER)) {
|
||||
this.validConnectionChecker = new MySqlValidConnectionChecker();
|
||||
} else if (realDriverClassName.equals("oracle.jdbc.driver.OracleDriver")) {
|
||||
} else if (realDriverClassName.equals(JdbcConstants.ORACLE_DRIVER)) {
|
||||
this.validConnectionChecker = new OracleValidConnectionChecker();
|
||||
} else if (realDriverClassName.equals("com.microsoft.jdbc.sqlserver.SQLServerDriver")) {
|
||||
} else if (realDriverClassName.equals(JdbcConstants.SQL_SERVER_DRIVER)) {
|
||||
this.validConnectionChecker = new MSSQLValidConnectionChecker();
|
||||
} else if (realDriverClassName.equals(JdbcConstants.POSTGRESQL_DRIVER)) {
|
||||
this.validConnectionChecker = new PGValidConnectionChecker();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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 java.io.Serializable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class PGValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker, Serializable {
|
||||
|
||||
private static final long serialVersionUID = -2227528634302168877L;
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(PGValidConnectionChecker.class);
|
||||
|
||||
private String defaultValidateQuery = "SELECT 'x' FROM DUAL";
|
||||
|
||||
public PGValidConnectionChecker(){
|
||||
configFromProperties(System.getProperties());
|
||||
}
|
||||
|
||||
public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) {
|
||||
if (validateQuery == null || validateQuery.isEmpty()) {
|
||||
validateQuery = this.defaultValidateQuery;
|
||||
}
|
||||
|
||||
try {
|
||||
if (conn.isClosed()) {
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
// skip
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
if (conn instanceof DruidPooledConnection) {
|
||||
conn = ((DruidPooledConnection) conn).getConnection();
|
||||
}
|
||||
|
||||
if (conn instanceof ConnectionProxy) {
|
||||
conn = ((ConnectionProxy) conn).getRawObject();
|
||||
}
|
||||
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
stmt = conn.createStatement();
|
||||
rs = stmt.executeQuery(validateQuery);
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Unexpected error in ping", e);
|
||||
return false;
|
||||
} finally {
|
||||
JdbcUtils.close(rs);
|
||||
JdbcUtils.close(stmt);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Unexpected error in pingDatabase", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,10 +28,12 @@ public interface JdbcConstants {
|
||||
public static final String DB2_DRIVER = "COM.ibm.db2.jdbc.app.DB2Driver";
|
||||
|
||||
public static final String POSTGRESQL = "postgresql";
|
||||
public static final String POSTGRESQL_DRIVER = "org.postgresql.Driver";
|
||||
|
||||
public static final String SYBASE = "sybase";
|
||||
|
||||
public static final String SQL_SERVER = "sqlserver";
|
||||
public static final String SQL_SERVER_DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
|
||||
|
||||
public static final String ORACLE = "oracle";
|
||||
public static final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
|
||||
|
||||
Reference in New Issue
Block a user