Skip to content

Commit

Permalink
FIR-32085: cache Connection.getDatabaseMetaData()
Browse files Browse the repository at this point in the history
  • Loading branch information
alexradzin committed Apr 11, 2024
1 parent 134b5fd commit 9e8797e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class DatabaseMetaDataTest extends IntegrationTest {
Expand All @@ -73,6 +77,17 @@ void afterEach() {
executeStatementFromFile("/statements/metadata/cleanup.sql");
}

@Test
void getMetadata() throws SQLException {
try (Connection connection = createConnection()) {
DatabaseMetaData databaseMetaData = connection.getMetaData();
assertNotNull(databaseMetaData);
assertSame(databaseMetaData, connection.getMetaData());
connection.close();
assertThat(assertThrows(SQLException.class, connection::getMetaData).getMessage(), containsString("closed"));
}
}

@Test
void shouldReturnSchema() throws SQLException {
List<String> schemas = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public abstract class FireboltConnection implements Connection {
private int networkTimeout;
private final String protocolVersion;
protected int infraVersion = 1;
private volatile DatabaseMetaData databaseMetaData;

//Properties that are used at the beginning of the connection for authentication
protected final FireboltProperties loginProperties;
Expand Down Expand Up @@ -158,6 +159,7 @@ protected void connect() throws SQLException {
// The validation of not local DB is implemented into authenticate() method itself.
assertDatabaseExisting(loginProperties.getDatabase());
}
databaseMetaData = retrieveMetaData();

log.debug("Connection opened");
}
Expand Down Expand Up @@ -234,6 +236,10 @@ public boolean isClosed() {
@Override
public DatabaseMetaData getMetaData() throws SQLException {
validateConnectionIsNotClose();
return databaseMetaData;
}

private DatabaseMetaData retrieveMetaData() {
if (!loginProperties.isSystemEngine()) {
return new FireboltDatabaseMetadata(httpConnectionUrl, this);
} else {
Expand Down Expand Up @@ -321,6 +327,7 @@ public void close() {
}
statements.clear();
}
databaseMetaData = null;
log.debug("Connection closed");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.firebolt.jdbc.connection.settings.FireboltProperties;
import com.firebolt.jdbc.exception.FireboltException;
import com.firebolt.jdbc.service.FireboltGatewayUrlService;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
Expand All @@ -17,7 +18,10 @@

import static java.lang.String.format;
import static java.util.stream.Collectors.toMap;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
Expand Down Expand Up @@ -76,6 +80,9 @@ void getMetadata(String testName, String engineParameter, boolean readOnly) thro
try (FireboltConnection connection = createConnection(format("jdbc:firebolt:db?env=dev&account=dev%s", engineParameter), connectionProperties)) {
DatabaseMetaData dbmd = connection.getMetaData();
assertEquals(readOnly, dbmd.isReadOnly());
assertSame(dbmd, connection.getMetaData());
connection.close();
assertThat(assertThrows(SQLException.class, connection::getMetaData).getMessage(), containsString("closed"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@

import static com.firebolt.jdbc.connection.FireboltConnectionUserPassword.SYSTEM_ENGINE_NAME;
import static java.lang.String.format;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -61,6 +65,9 @@ void getMetadata(String engine, boolean readOnly) throws SQLException {
try (FireboltConnection connection = createConnection(format("jdbc:firebolt:db?env=dev&engine=%s&account=dev", engine), connectionProperties)) {
DatabaseMetaData dbmd = connection.getMetaData();
assertEquals(readOnly, dbmd.isReadOnly());
assertSame(dbmd, connection.getMetaData());
connection.close();
assertThat(assertThrows(SQLException.class, connection::getMetaData).getMessage(), containsString("closed"));
}
}

Expand Down

0 comments on commit 9e8797e

Please sign in to comment.