Eliminate network call if state get is called after set (#2199)

Fix GH-2198
pull/2241/head
Yanming Zhou 5 months ago committed by Brett Wooldridge
parent b3e6d23dc5
commit 1d173513be

@ -395,6 +395,16 @@ public abstract class ProxyConnection implements Connection
isCommitStateDirty = false;
}
/** {@inheritDoc} */
@Override
public boolean getAutoCommit() throws SQLException
{
if ((dirtyBits & DIRTY_BIT_AUTOCOMMIT) != 0) {
return isAutoCommit;
}
return delegate.getAutoCommit();
}
/** {@inheritDoc} */
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException
@ -404,6 +414,16 @@ public abstract class ProxyConnection implements Connection
dirtyBits |= DIRTY_BIT_AUTOCOMMIT;
}
/** {@inheritDoc} */
@Override
public boolean isReadOnly() throws SQLException
{
if ((dirtyBits & DIRTY_BIT_READONLY) != 0) {
return isReadOnly;
}
return delegate.isReadOnly();
}
/** {@inheritDoc} */
@Override
public void setReadOnly(boolean readOnly) throws SQLException
@ -414,6 +434,16 @@ public abstract class ProxyConnection implements Connection
dirtyBits |= DIRTY_BIT_READONLY;
}
/** {@inheritDoc} */
@Override
public int getTransactionIsolation() throws SQLException
{
if ((dirtyBits & DIRTY_BIT_ISOLATION) != 0) {
return transactionIsolation;
}
return delegate.getTransactionIsolation();
}
/** {@inheritDoc} */
@Override
public void setTransactionIsolation(int level) throws SQLException
@ -423,6 +453,16 @@ public abstract class ProxyConnection implements Connection
dirtyBits |= DIRTY_BIT_ISOLATION;
}
/** {@inheritDoc} */
@Override
public String getCatalog() throws SQLException
{
if ((dirtyBits & DIRTY_BIT_CATALOG) != 0) {
return dbcatalog;
}
return delegate.getCatalog();
}
/** {@inheritDoc} */
@Override
public void setCatalog(String catalog) throws SQLException
@ -432,6 +472,16 @@ public abstract class ProxyConnection implements Connection
dirtyBits |= DIRTY_BIT_CATALOG;
}
/** {@inheritDoc} */
@Override
public int getNetworkTimeout() throws SQLException
{
if ((dirtyBits & DIRTY_BIT_NETTIMEOUT) != 0) {
return networkTimeout;
}
return delegate.getNetworkTimeout();
}
/** {@inheritDoc} */
@Override
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException
@ -441,6 +491,16 @@ public abstract class ProxyConnection implements Connection
dirtyBits |= DIRTY_BIT_NETTIMEOUT;
}
/** {@inheritDoc} */
@Override
public String getSchema() throws SQLException
{
if ((dirtyBits & DIRTY_BIT_SCHEMA) != 0) {
return dbschema;
}
return delegate.getSchema();
}
/** {@inheritDoc} */
@Override
public void setSchema(String schema) throws SQLException

@ -41,6 +41,7 @@ import com.zaxxer.hikari.util.UtilityElf;
/**
*
* @author Brett Wooldridge
* @author Yanming Zhou
*/
public class StubConnection extends StubBaseConnection
{
@ -54,6 +55,7 @@ public class StubConnection extends StubBaseConnection
private boolean autoCommit;
private int isolation = Connection.TRANSACTION_READ_COMMITTED;
private String catalog;
private String schema;
private long waitTimeout;
private static ScheduledExecutorService connectionWaitTimeout = new ScheduledThreadPoolExecutor(1);
@ -142,6 +144,9 @@ public class StubConnection extends StubBaseConnection
@Override
public boolean getAutoCommit() throws SQLException
{
if (throwException) {
throw new SQLException();
}
return autoCommit;
}
@ -230,6 +235,9 @@ public class StubConnection extends StubBaseConnection
@Override
public String getCatalog() throws SQLException
{
if (throwException) {
throw new SQLException();
}
return catalog;
}
@ -247,6 +255,9 @@ public class StubConnection extends StubBaseConnection
@Override
public int getTransactionIsolation() throws SQLException
{
if (throwException) {
throw new SQLException();
}
return isolation;
}
@ -490,12 +501,19 @@ public class StubConnection extends StubBaseConnection
/** {@inheritDoc} */
public void setSchema(String schema) throws SQLException
{
if (throwException) {
throw new SQLException();
}
this.schema = schema;
}
/** {@inheritDoc} */
public String getSchema() throws SQLException
{
return null;
if (throwException) {
throw new SQLException();
}
return schema;
}
/** {@inheritDoc} */
@ -507,6 +525,9 @@ public class StubConnection extends StubBaseConnection
/** {@inheritDoc} */
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException
{
if (throwException) {
throw new SQLException();
}
if (networkTimeoutSetter != null) {
try {
networkTimeoutSetter.call();
@ -523,6 +544,10 @@ public class StubConnection extends StubBaseConnection
/** {@inheritDoc} */
public int getNetworkTimeout() throws SQLException
{
if (throwException) {
throw new SQLException();
}
if (oldDriver) {
throw new AbstractMethodError();
}

@ -0,0 +1,122 @@
/*
* Copyright (C) 2013, 2014 Brett Wooldridge
*
* 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.zaxxer.hikari.pool;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.mocks.StubConnection;
import org.junit.Test;
import java.sql.Connection;
import java.sql.SQLException;
import static com.zaxxer.hikari.pool.TestElf.newHikariConfig;
import static org.junit.Assert.fail;
/**
* @author Yanming Zhou
*/
public class TestStates
{
@Test
public void testGetBeforeSet() throws SQLException
{
HikariConfig config = newHikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(1);
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
Connection conn = ds.getConnection();
StubConnection stub = conn.unwrap(StubConnection.class);
stub.throwException = true;
try {
conn.isReadOnly();
fail();
}
catch (SQLException e) {
// pass
}
try {
conn.getAutoCommit();
fail();
}
catch (SQLException e) {
// pass
}
try {
conn.getTransactionIsolation();
fail();
}
catch (SQLException e) {
// pass
}
try {
conn.getCatalog();
fail();
}
catch (SQLException e) {
// pass
}
try {
conn.getNetworkTimeout();
fail();
}
catch (SQLException e) {
// pass
}
try {
conn.getSchema();
fail();
}
catch (SQLException e) {
// pass
}
}
}
@Test
public void testGetAfterSet() throws SQLException
{
HikariConfig config = newHikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(1);
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
Connection conn = ds.getConnection();
StubConnection stub = conn.unwrap(StubConnection.class);
conn.setReadOnly(true);
conn.setAutoCommit(true);
conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
conn.setCatalog("catalog");
conn.setNetworkTimeout(null, 10);
conn.setSchema("schema");
stub.throwException = true;
// should not throw exception even if stub throws exception
conn.isReadOnly();
conn.getAutoCommit();
conn.getTransactionIsolation();
conn.getCatalog();
conn.getNetworkTimeout();
conn.getSchema();
}
}
}
Loading…
Cancel
Save