optimize for oracle

This commit is contained in:
wenshao
2011-12-30 09:23:03 +08:00
parent 4d59388633
commit 8c4471c4c9
9 changed files with 207 additions and 15 deletions
@@ -1457,6 +1457,8 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
public Date getCreatedTime() {
return createdTime;
}
public abstract boolean isOracle();
public abstract int getRawDriverMajorVersion();
@@ -108,6 +108,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
private String initStackTrace;
private boolean isOracle = false;
public DruidDataSource(){
}
@@ -286,6 +288,10 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
this.dbType = JdbcUtils.getDbType(jdbcUrl, driverClass.getClass().getName());
if ("oracle".equals(this.dbType)) {
isOracle = true;
}
String realDriverClassName = driver.getClass().getName();
if (realDriverClassName.equals("com.mysql.jdbc.Driver")) {
this.validConnectionChecker = new MySqlValidConnectionChecker();
@@ -1314,6 +1320,10 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
}
public boolean isOracle() {
return isOracle;
}
@Override
public String getVersion() {
return VERSION.MajorVersion + "." + VERSION.MinorVersion + "." + VERSION.RevisionVersion + "-2011-12-27 14:45";
@@ -1,17 +1,40 @@
package com.alibaba.druid.pool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.driver.OracleStatement;
public class OracleUtils {
public static void clearDefines(PoolablePreparedStatement stmt) throws SQLException {
PreparedStatement raw = stmt.getRawPreparedStatement();
if (raw instanceof OracleStatement) {
OracleStatement oracleStmt = (OracleStatement) raw;
oracleStmt.clearDefines();
OracleStatement oracleStmt = stmt.unwrap(OracleStatement.class);
oracleStmt.clearDefines();
}
public static int getRowPrefetch(PreparedStatement stmt) throws SQLException {
OracleStatement oracleStmt = stmt.unwrap(OracleStatement.class);
return oracleStmt.getRowPrefetch();
}
public static void setRowPrefetch(PreparedStatement stmt, int value) throws SQLException {
OracleStatement oracleStmt = stmt.unwrap(OracleStatement.class);
oracleStmt.setRowPrefetch(value);
}
public static void setDefaultRowPrefetch(Connection conn, int value) throws SQLException {
OracleConnection oracleConn = conn.unwrap(OracleConnection.class);
oracleConn.setDefaultRowPrefetch(value);
}
public static int getDefaultRowPrefetch(Connection conn, int value) throws SQLException {
if (conn instanceof OracleConnection) {
OracleConnection oracleConn = (OracleConnection) conn;
return oracleConn.getDefaultRowPrefetch();
}
return -1;
}
}
@@ -97,8 +97,8 @@ public class PoolableConnection implements PooledConnection, Connection {
throw new SQLException("Error", t);
}
private boolean isOracle() {
return "oracle".equals(holder.getDataSource().getDbType());
public boolean isOracle() {
return holder.getDataSource().isOracle();
}
void closePoolableStatement(PoolablePreparedStatement stmt) throws SQLException {
@@ -81,7 +81,7 @@ public class PoolablePreparedStatement extends PoolableStatement implements Prep
public PreparedStatementHolder getPreparedStatementHolder() {
return holder;
}
public int getHitCount() {
return holder.getHitCount();
}
@@ -173,6 +173,8 @@ public class PoolablePreparedStatement extends PoolableStatement implements Prep
incrementExecuteCount();
transactionRecord(sql);
oracleSetRowPrefetch();
try {
ResultSet rs = stmt.executeQuery();
@@ -427,6 +429,8 @@ public class PoolablePreparedStatement extends PoolableStatement implements Prep
incrementExecuteCount();
transactionRecord(sql);
oracleSetRowPrefetch();
try {
return stmt.execute();
} catch (Throwable t) {
@@ -434,6 +438,43 @@ public class PoolablePreparedStatement extends PoolableStatement implements Prep
}
}
private void oracleSetRowPrefetch() throws SQLException {
if (!conn.isOracle()) {
return;
}
if (holder.getHitCount() == 0) {
return;
}
int fetchRowPeak = holder.getFetchRowPeak();
if (fetchRowPeak < 0) {
return;
}
if (holder.getDefaultRowPretch() == -1) {
int defaultRowPretch = OracleUtils.getRowPrefetch(this);
holder.setDefaultRowPretch(defaultRowPretch);
holder.setRowPrefetch(defaultRowPretch);
}
int rowPrefetch;
if (fetchRowPeak <= 1) {
rowPrefetch = 1;
} else if (fetchRowPeak > holder.getDefaultRowPretch()) {
rowPrefetch = holder.getDefaultRowPretch();
} else {
rowPrefetch = fetchRowPeak;
}
if (rowPrefetch != holder.getRowPrefetch()) {
OracleUtils.setRowPrefetch(this, rowPrefetch);
holder.setRowPrefetch(rowPrefetch);
}
}
@Override
public void addBatch() throws SQLException {
checkOpen();
@@ -35,6 +35,10 @@ public class PoolableWrapper implements Wrapper {
return false;
}
if (iface.isInstance(wrapper)) {
return true;
}
if (iface.isInstance(this)) {
return true;
}
@@ -49,6 +53,10 @@ public class PoolableWrapper implements Wrapper {
return null;
}
if (iface.isInstance(wrapper)) {
return (T) wrapper;
}
if (iface.isInstance(this)) {
return (T) this;
}
@@ -26,15 +26,34 @@ public final class PreparedStatementHolder {
private final PreparedStatementKey key;
private final PreparedStatement statement;
private int hitCount = 0;
private int hitCount = 0;
private int fetchRowPeak = -1;
private int fetchRowPeak = -1;
private int defaultRowPretch = -1;
private int rowPrefetch = -1;
public PreparedStatementHolder(PreparedStatementKey key, PreparedStatement stmt){
this.key = key;
this.statement = stmt;
}
public int getDefaultRowPretch() {
return defaultRowPretch;
}
public void setDefaultRowPretch(int defaultRowPretch) {
this.defaultRowPretch = defaultRowPretch;
}
public int getRowPrefetch() {
return rowPrefetch;
}
public void setRowPrefetch(int rowPrefetch) {
this.rowPrefetch = rowPrefetch;
}
public int getFetchRowPeak() {
return fetchRowPeak;
}