Skip to content

Commit

Permalink
Merge branch 'jdk8'
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorndarri committed Sep 12, 2023
2 parents 6577c43 + b9a1201 commit 9a90951
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 80 deletions.
5 changes: 4 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9583,4 +9583,7 @@
DefaultLocalEntityConnection refactored, more try with resources, less code within synchronized blocks
DefaultLocalEntityConnection.dependencies() bug fixed, now synchronized due to usage of non-thread-safe access to nonSoftForeignKeyReferenceCache, now throws exception if entities are not of the same type
LoadTestPanel, applications tab now first, scenarios second, overview third, automatic refresh of applications true by default
LoadTestModel refactored, application initialization improved
LoadTestModel refactored, application initialization improved
DefaultRemoteClient.withDatabaseUser() bug fixed, copy constructor improved
ResultIterator no longer calls close() on exhaustion or error, related refactoring
Database.closeSilently() removed
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ public void close() {
catch (SQLException ex) {
System.err.println("DefaultDatabaseConnection.close(), connection invalid");
}
Database.closeSilently(connection);
try {
connection.close();
}
catch (Exception ignored) {/*ignored*/}
connection = null;
transactionOpen = false;
}
Expand Down
13 changes: 0 additions & 13 deletions common/db/src/main/java/is/codion/common/db/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,19 +270,6 @@ static Database instance() {
return AbstractDatabase.instance();
}

/**
* Closes the given {@link AutoCloseable} instance, suppressing any Exceptions that may occur.
* @param closeable the closeable to close
*/
static void closeSilently(AutoCloseable closeable) {
try {
if (closeable != null) {
closeable.close();
}
}
catch (Exception ignored) {/*ignored*/}
}

/**
* Counts queries for statistics.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@

/**
* Iterates through a ResultSet fetching instances of T.
* Closes the ResultSet when iteration is finished or in case of an exception.
* Use try with resources or remember to call {@link #close()} in order to close underlying resources.
* @param <T> the type to fetch from the result set
*/
public interface ResultIterator<T> extends AutoCloseable {

/**
* Returns true if a row is available in the underlying result set.
* Calls {@link #close()} before returning false when iteration has been completed.
* @return true if a row is available in the underlying result set
* @throws SQLException in case of an exception
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@

public class DatabaseTest {

@Test
void closeSilently() {
Database.closeSilently(null);
Database.closeSilently(null);
Database.closeSilently(null);
}

@Test
void validateWithQuery() throws DatabaseException, SQLException {
Database testDatabase = new TestDatabase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ final class DefaultRemoteClient implements RemoteClient, Serializable {
private final ConnectionRequest connectionRequest;
private final User databaseUser;
private final String clientHost;
private final LocalDateTime creationTime = LocalDateTime.now();
private final LocalDateTime creationTime;

DefaultRemoteClient(ConnectionRequest connectionRequest, User databaseUser, String clientHost) {
this(connectionRequest, databaseUser, clientHost, LocalDateTime.now());
}

DefaultRemoteClient(ConnectionRequest connectionRequest, User databaseUser, String clientHost, LocalDateTime creationTime) {
this.connectionRequest = requireNonNull(connectionRequest, "connectionRequest");
this.databaseUser = requireNonNull(databaseUser, "databaseUser");
this.clientHost = clientHost;
this.creationTime = creationTime;
}

@Override
Expand Down Expand Up @@ -93,7 +98,7 @@ public String clientHost() {

@Override
public RemoteClient withDatabaseUser(User databaseUser) {
return new DefaultRemoteClient(connectionRequest, databaseUser, clientHost);
return new DefaultRemoteClient(connectionRequest, databaseUser, clientHost, creationTime);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
import java.util.function.Predicate;

import static is.codion.common.db.connection.DatabaseConnection.SQL_STATE_NO_DATA;
import static is.codion.common.db.database.Database.closeSilently;
import static is.codion.framework.db.condition.Condition.*;
import static is.codion.framework.db.local.Queries.*;
import static is.codion.framework.domain.entity.OrderBy.ascending;
Expand Down Expand Up @@ -1361,7 +1360,7 @@ private static <T> ResultPacker<T> resultPacker(ColumnDefinition<T> columnDefini
}

private static PreparedStatement setParameterValues(PreparedStatement statement, List<ColumnDefinition<?>> statementColumns,
List<?> statementValues) throws SQLException {
List<?> statementValues) throws SQLException {
if (statementValues.isEmpty()) {
return statement;
}
Expand Down Expand Up @@ -1494,6 +1493,13 @@ private static void verifyColumnIsSelectable(ColumnDefinition<?> columnDefinitio
}
}

private static void closeSilently(AutoCloseable closeable) {
try {
closeable.close();
}
catch (Exception ignored) {/*ignored*/}
}

static Database configureDatabase(Database database, Domain domain) throws DatabaseException {
new DatabaseConfiguration(requireNonNull(domain), requireNonNull(database)).configure();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import java.sql.Statement;
import java.util.NoSuchElementException;

import static is.codion.common.db.database.Database.closeSilently;

final class EntityResultIterator implements ResultIterator<Entity> {

private final Statement statement;
Expand All @@ -23,11 +21,6 @@ final class EntityResultIterator implements ResultIterator<Entity> {
private boolean hasNext;
private boolean hasNextCalled;

/**
* @param statement the Statement, closed on exception or exhaustion
* @param resultSet the ResultSet, closed on exception or exhaustion
* @param resultPacker the ResultPacker
*/
EntityResultIterator(Statement statement, ResultSet resultSet, ResultPacker<Entity> resultPacker) {
this.statement = statement;
this.resultSet = resultSet;
Expand All @@ -39,20 +32,10 @@ public boolean hasNext() throws SQLException {
if (hasNextCalled) {
return hasNext;
}
try {
hasNext = resultSet.next();
hasNextCalled = true;
if (hasNext) {
return true;
}
close();
hasNext = resultSet.next();
hasNextCalled = true;

return false;
}
catch (SQLException e) {
close();
throw e;
}
return hasNext;
}

@Override
Expand All @@ -61,18 +44,20 @@ public Entity next() throws SQLException {
throw new NoSuchElementException();
}
hasNextCalled = false;
try {
return resultPacker.get(resultSet);
}
catch (SQLException e) {
close();
throw e;
}

return resultPacker.get(resultSet);
}

@Override
public void close() {
closeSilently(resultSet);
closeSilently(statement);
}

private static void closeSilently(AutoCloseable closeable) {
try {
closeable.close();
}
catch (Exception ignored) {/*ignored*/}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,17 @@ public interface LocalEntityConnection extends EntityConnection {
DatabaseConnection databaseConnection();

/**
* Returns a result set iterator based on the given query condition, this iterator closes all underlying
* resources in case of an exception and when it finishes iterating.
* Calling {@link ResultIterator#close()} is required if the iterator has not been exhausted and is always recommended.
* Returns a result set iterator based on the given query condition.
* Remember to use try with resources or to call {@link ResultIterator#close()} in order to close underlying resources.
* @param condition the query condition
* @return an iterator for the given query condition
* @throws DatabaseException in case of an exception
*/
ResultIterator<Entity> iterator(Condition condition) throws DatabaseException;

/**
* Returns a result set iterator based on the given select, this iterator closes all underlying
* resources in case of an exception and when it finishes iterating.
* Calling {@link ResultIterator#close()} is required if the iterator has not been exhausted and is always recommended.
* Returns a result set iterator based on the given select.
* Remember to use try with resources or to call {@link ResultIterator#close()} in order to close underlying resources.
* @param select the query select
* @return an iterator for the given query select
* @throws DatabaseException in case of an exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,8 @@ void optimisticLockingBlob() throws Exception {
void iterator() throws Exception {
try (LocalEntityConnection connection = createConnection()) {
Condition condition = all(Employee.TYPE);
Iterator<Entity> iterator = connection.iterator(condition).iterator();
ResultIterator<Entity> resultIterator = connection.iterator(condition);
Iterator<Entity> iterator = resultIterator.iterator();
//calling hasNext() should be idempotent and not lose rows
assertTrue(iterator.hasNext());
assertTrue(iterator.hasNext());
Expand All @@ -886,7 +887,9 @@ void iterator() throws Exception {
assertThrows(NoSuchElementException.class, iterator::next);
int rowCount = connection.count(condition);
assertEquals(rowCount, counter);
iterator = connection.iterator(condition).iterator();
resultIterator.close();
resultIterator = connection.iterator(condition);
iterator = resultIterator.iterator();
counter = 0;
try {
while (true) {
Expand All @@ -902,14 +905,14 @@ void iterator() throws Exception {

@Test
void dualIterator() throws Exception {
try (LocalEntityConnection connection = createConnection()) {
ResultIterator<Entity> deptIterator =
connection.iterator(all(Department.TYPE));
try (LocalEntityConnection connection = createConnection();
ResultIterator<Entity> deptIterator = connection.iterator(all(Department.TYPE))) {
while (deptIterator.hasNext()) {
ResultIterator<Entity> empIterator =
connection.iterator(foreignKey(Employee.DEPARTMENT_FK).equalTo(deptIterator.next()));
while (empIterator.hasNext()) {
empIterator.next();
try (ResultIterator<Entity> empIterator =
connection.iterator(foreignKey(Employee.DEPARTMENT_FK).equalTo(deptIterator.next()))) {
while (empIterator.hasNext()) {
empIterator.next();
}
}
}
}
Expand Down Expand Up @@ -1113,29 +1116,29 @@ void queryCache() throws DatabaseException {
assertSame(result, result2);

result2 = connection.select(Select.where(column(Department.DEPTNO)
.greaterThanOrEqualTo(20))
.greaterThanOrEqualTo(20))
.orderBy(descending(Department.DEPTNO))
.build());
assertNotSame(result, result2);

result = connection.select(Select.where(column(Department.DEPTNO)
.greaterThanOrEqualTo(20))
.greaterThanOrEqualTo(20))
.orderBy(descending(Department.DEPTNO))
.build());
assertSame(result, result2);

result2 = connection.select(Select.where(column(Department.DEPTNO)
.greaterThanOrEqualTo(20))
.greaterThanOrEqualTo(20))
.orderBy(OrderBy.ascending(Department.DEPTNO))
.build());
assertNotSame(result, result2);

result = connection.select(Select.where(column(Department.DEPTNO)
.greaterThanOrEqualTo(20))
.greaterThanOrEqualTo(20))
.forUpdate()
.build());
result2 = connection.select(Select.where(column(Department.DEPTNO)
.greaterThanOrEqualTo(20))
.greaterThanOrEqualTo(20))
.forUpdate()
.build());
assertNotSame(result, result2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ private void returnConnection() {
private void returnConnectionToPool() {
Connection connection = poolEntityConnection.databaseConnection().getConnection();
if (connection != null) {
Database.closeSilently(connection);
closeSilently(connection);
poolEntityConnection.databaseConnection().setConnection(null);
}
}
Expand Down Expand Up @@ -324,6 +324,13 @@ private static void rollbackSilently(DatabaseConnection databaseConnection) {
catch (SQLException e) {/*Silently*/}
}

private static void closeSilently(AutoCloseable closeable) {
try {
closeable.close();
}
catch (Exception ignored) {/*ignored*/}
}

static final class RequestCounter {

private static final int DEFAULT_REQUEST_COUNTER_UPDATE_INTERVAL = 2500;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ protected final void perform(QueryApplication application) throws Exception {
throw e;
}
finally {
Database.closeSilently(connection);
Database.closeSilently(resultSet);
Database.closeSilently(statement);
closeSilently(connection);
closeSilently(resultSet);
closeSilently(statement);
}
}

Expand Down Expand Up @@ -169,4 +169,11 @@ private static void fetchResult(ResultSet resultSet) throws SQLException {
}
}
}

private static void closeSilently(AutoCloseable closeable) {
try {
closeable.close();
}
catch (Exception ignored) {/*ignored*/}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ public ValueObserver<String> domainSourceObserver() {
}

public void close() {
Database.closeSilently(connection);
try {
connection.close();
}
catch (Exception ignored) {/*ignored*/}
}

private void bindEvents() {
Expand Down

0 comments on commit 9a90951

Please sign in to comment.