improved jdk7 support & postgresql support.

This commit is contained in:
wenshao
2017-07-11 15:48:00 +08:00
parent 1e0ce32284
commit 15ac64e501
10 changed files with 134 additions and 9 deletions
@@ -54,6 +54,7 @@ import java.sql.Timestamp;
import java.sql.Wrapper;
import java.util.Calendar;
import java.util.Properties;
import java.util.concurrent.Executor;
/**
* @author wenshao [szujobs@hotmail.com]
@@ -188,6 +189,15 @@ public interface Filter extends Wrapper {
Struct connection_createStruct(FilterChain chain, ConnectionProxy connection, String typeName, Object[] attributes)
throws SQLException;
String connection_getSchema(FilterChain chain, ConnectionProxy connection) throws SQLException;
void connection_setSchema(FilterChain chain, ConnectionProxy connection, String schema) throws SQLException;
void connection_abort(FilterChain chain, ConnectionProxy connection, Executor executor) throws SQLException;
void connection_setNetworkTimeout(FilterChain chain, ConnectionProxy connection, Executor executor, int milliseconds) throws SQLException;
int connection_getNetworkTimeout(FilterChain chain, ConnectionProxy connection) throws SQLException;
// ///////////////
boolean resultSet_next(FilterChain chain, ResultSetProxy resultSet) throws SQLException;
@@ -51,6 +51,7 @@ import java.sql.Wrapper;
import java.util.Calendar;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
/**
* 提供JdbcFilter的基本实现,使得实现一个JdbcFilter更容易。
@@ -1037,6 +1038,27 @@ public abstract class FilterAdapter extends NotificationBroadcasterSupport imple
chain.connection_setTypeMap(connection, map);
}
@Override
public String connection_getSchema(FilterChain chain, ConnectionProxy connection) throws SQLException {
return chain.connection_getSchema(connection);
}
@Override
public void connection_setSchema(FilterChain chain, ConnectionProxy connection, String schema) throws SQLException {
chain.connection_setSchema(connection, schema);
}
public void connection_abort(FilterChain chain, ConnectionProxy connection, Executor executor) throws SQLException {
chain.connection_abort(connection, executor);
}
public void connection_setNetworkTimeout(FilterChain chain, ConnectionProxy connection, Executor executor, int milliseconds) throws SQLException {
chain.connection_setNetworkTimeout(connection, executor, milliseconds);
}
public int connection_getNetworkTimeout(FilterChain chain, ConnectionProxy connection) throws SQLException {
return chain.connection_getNetworkTimeout(connection);
}
@Override
public boolean isWrapperFor(FilterChain chain, Wrapper wrapper, Class<?> iface) throws SQLException {
return chain.isWrapperFor(wrapper, iface);
@@ -40,6 +40,7 @@ import java.sql.Struct;
import java.sql.Wrapper;
import java.util.Calendar;
import java.util.Properties;
import java.util.concurrent.Executor;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
@@ -175,6 +176,11 @@ public interface FilterChain {
Struct connection_createStruct(ConnectionProxy connection, String typeName, Object[] attributes)
throws SQLException;
String connection_getSchema(ConnectionProxy connection) throws SQLException;
void connection_setSchema(ConnectionProxy connection, String schema) throws SQLException;
void connection_abort(ConnectionProxy connection, Executor executor) throws SQLException;
void connection_setNetworkTimeout(ConnectionProxy connection, Executor executor, int milliseconds) throws SQLException;
int connection_getNetworkTimeout(ConnectionProxy connection) throws SQLException;
// ---------
// ///////////////
@@ -44,6 +44,7 @@ import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
@@ -693,6 +694,51 @@ public class FilterChainImpl implements FilterChain {
connection.getRawObject().setTypeMap(map);
}
@Override
public String connection_getSchema(ConnectionProxy connection) throws SQLException {
if (this.pos < filterSize) {
return nextFilter().connection_getSchema(this, connection);
}
return connection.getRawObject().getSchema();
}
@Override
public void connection_setSchema(ConnectionProxy connection, String schema) throws SQLException {
if (this.pos < filterSize) {
nextFilter().connection_setSchema(this, connection, schema);
return;
}
connection.getRawObject().setSchema(schema);
}
public void connection_abort(ConnectionProxy connection, Executor executor) throws SQLException {
if (this.pos < filterSize) {
nextFilter().connection_abort(this, connection, executor);
return;
}
connection.getRawObject().abort(executor);
}
public void connection_setNetworkTimeout(ConnectionProxy connection, Executor executor, int milliseconds) throws SQLException {
if (this.pos < filterSize) {
nextFilter().connection_setNetworkTimeout(this, connection, executor, milliseconds);
return;
}
connection.getRawObject().setNetworkTimeout(executor, milliseconds);
}
public int connection_getNetworkTimeout(ConnectionProxy connection) throws SQLException {
if (this.pos < filterSize) {
return nextFilter().connection_getNetworkTimeout(this, connection);
}
return connection.getRawObject().getNetworkTimeout();
}
// ///////////////////////////////////////
@Override
@@ -1635,7 +1635,8 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
stmt.execute(sql);
}
if (JdbcConstants.MYSQL.equals(dbType)) {
if (JdbcConstants.MYSQL.equals(dbType)
||JdbcConstants.ALIYUN_ADS.equals(dbType)) {
if (variables != null) {
ResultSet rs = null;
try {
@@ -700,8 +700,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
this.dbType = JdbcUtils.getDbType(jdbcUrl, null);
}
if (JdbcConstants.MYSQL.equals(this.dbType) || //
JdbcConstants.MARIADB.equals(this.dbType)) {
if (JdbcConstants.MYSQL.equals(this.dbType)
|| JdbcConstants.MARIADB.equals(this.dbType)
|| JdbcConstants.ALIYUN_ADS.equals(this.dbType)) {
boolean cacheServerConfigurationSet = false;
if (this.connectProperties.containsKey("cacheServerConfiguration")) {
cacheServerConfigurationSet = true;
@@ -203,6 +203,10 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql.
@Override
public Connection getConnection() {
if (!holder.isUnderlyingAutoCommit()) {
createTransactionInfo();
}
return conn;
}
@@ -755,6 +759,12 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql.
return transactionInfo;
}
protected void createTransactionInfo() {
DruidAbstractDataSource dataSource = holder.getDataSource();
dataSource.incrementStartTransactionCount();
transactionInfo = new TransactionInfo(dataSource.createTransactionId());
}
@Override
public void rollback() throws SQLException {
if (transactionInfo == null) {
@@ -859,6 +869,10 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql.
public DatabaseMetaData getMetaData() throws SQLException {
checkState();
if (!holder.isUnderlyingAutoCommit()) {
createTransactionInfo();
}
return conn.getMetaData();
}
@@ -983,6 +997,10 @@ public class DruidPooledConnection extends PoolableWrapper implements javax.sql.
public int getHoldability() throws SQLException {
checkState();
if (!holder.isUnderlyingAutoCommit()) {
createTransactionInfo();
}
return conn.getHoldability();
}
@@ -40,6 +40,7 @@ import java.util.Calendar;
import com.alibaba.druid.pool.PreparedStatementPool.MethodType;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.util.OracleUtils;
/**
@@ -623,6 +624,10 @@ public class DruidPooledPreparedStatement extends DruidPooledStatement implement
public ResultSetMetaData getMetaData() throws SQLException {
checkOpen();
if (!conn.holder.isUnderlyingAutoCommit()) {
conn.createTransactionInfo();
}
try {
return stmt.getMetaData();
} catch (Throwable t) {
@@ -689,6 +694,10 @@ public class DruidPooledPreparedStatement extends DruidPooledStatement implement
public ParameterMetaData getParameterMetaData() throws SQLException {
checkOpen();
if (!conn.holder.isUnderlyingAutoCommit()) {
conn.createTransactionInfo();
}
try {
return stmt.getParameterMetaData();
} catch (Throwable t) {
@@ -68,7 +68,7 @@ public class DruidXADataSource extends DruidDataSource implements XADataSource {
}
}
if (JdbcUtils.MYSQL.equals(dbType)) {
if (JdbcUtils.MYSQL.equals(dbType) || JdbcUtils.MARIADB.equals(dbType)) {
return MySqlUtils.createXAConnection(driver, physicalConn);
}
@@ -497,23 +497,35 @@ public class ConnectionProxyImpl extends WrapperProxyImpl implements ConnectionP
}
public void setSchema(String schema) throws SQLException {
throw new SQLFeatureNotSupportedException();
FilterChainImpl chain = createChain();
chain.connection_setSchema(this, schema);
recycleFilterChain(chain);
}
public String getSchema() throws SQLException {
throw new SQLFeatureNotSupportedException();
FilterChainImpl chain = createChain();
String schema = chain.connection_getSchema(this);
recycleFilterChain(chain);
return schema;
}
public void abort(Executor executor) throws SQLException {
throw new SQLFeatureNotSupportedException();
FilterChainImpl chain = createChain();
chain.connection_abort(this, executor);
recycleFilterChain(chain);
}
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
throw new SQLFeatureNotSupportedException();
FilterChainImpl chain = createChain();
chain.connection_setNetworkTimeout(this, executor, milliseconds);
recycleFilterChain(chain);
}
public int getNetworkTimeout() throws SQLException {
throw new SQLFeatureNotSupportedException();
FilterChainImpl chain = createChain();
int networkTimeout = chain.connection_getNetworkTimeout(this);
recycleFilterChain(chain);
return networkTimeout;
}
@SuppressWarnings("unchecked")