Skip to content

Commit

Permalink
Eliminate network call if state get is called after set
Browse files Browse the repository at this point in the history
  • Loading branch information
quaff committed May 9, 2024
1 parent 0a6ccdb commit 2a40cee
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 1 deletion.
61 changes: 61 additions & 0 deletions src/main/java/com/zaxxer/hikari/pool/ProxyConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* This is the proxy class for java.sql.Connection.
*
* @author Brett Wooldridge
* @author Yanming Zhou
*/
public abstract class ProxyConnection implements Connection
{
Expand Down Expand Up @@ -395,6 +396,16 @@ public void rollback(Savepoint savepoint) throws SQLException
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
Expand All @@ -404,6 +415,16 @@ public void setAutoCommit(boolean autoCommit) throws SQLException
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
Expand All @@ -414,6 +435,16 @@ public void setReadOnly(boolean readOnly) throws SQLException
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
Expand All @@ -423,6 +454,16 @@ public void setTransactionIsolation(int level) throws SQLException
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
Expand All @@ -432,6 +473,16 @@ public void setCatalog(String catalog) throws SQLException
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
Expand All @@ -441,6 +492,16 @@ public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLExc
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
Expand Down
27 changes: 26 additions & 1 deletion src/test/java/com/zaxxer/hikari/mocks/StubConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
/**
*
* @author Brett Wooldridge
* @author Yanming Zhou
*/
public class StubConnection extends StubBaseConnection
{
Expand All @@ -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);
Expand Down Expand Up @@ -142,6 +144,9 @@ public void setAutoCommit(boolean autoCommit) throws SQLException
@Override
public boolean getAutoCommit() throws SQLException
{
if (throwException) {
throw new SQLException();
}
return autoCommit;
}

Expand Down Expand Up @@ -230,6 +235,9 @@ public void setCatalog(String catalog) throws SQLException
@Override
public String getCatalog() throws SQLException
{
if (throwException) {
throw new SQLException();
}
return catalog;
}

Expand All @@ -247,6 +255,9 @@ public void setTransactionIsolation(int level) throws SQLException
@Override
public int getTransactionIsolation() throws SQLException
{
if (throwException) {
throw new SQLException();
}
return isolation;
}

Expand Down Expand Up @@ -490,12 +501,19 @@ public Struct createStruct(String typeName, Object[] attributes) throws SQLExcep
/** {@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} */
Expand All @@ -507,6 +525,9 @@ public void abort(Executor executor) throws SQLException
/** {@inheritDoc} */
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException
{
if (throwException) {
throw new SQLException();
}
if (networkTimeoutSetter != null) {
try {
networkTimeoutSetter.call();
Expand All @@ -523,6 +544,10 @@ public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLExc
/** {@inheritDoc} */
public int getNetworkTimeout() throws SQLException
{
if (throwException) {
throw new SQLException();
}

if (oldDriver) {
throw new AbstractMethodError();
}
Expand Down
122 changes: 122 additions & 0 deletions src/test/java/com/zaxxer/hikari/pool/TestStates.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}

0 comments on commit 2a40cee

Please sign in to comment.