Skip to content

Commit

Permalink
HHH-16458 Close JDBC statement when DeferredResultSetAccess fails to …
Browse files Browse the repository at this point in the history
…execute a query
  • Loading branch information
yrodiere authored and beikov committed Apr 25, 2023
1 parent 01161e6 commit 20842f8
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
Expand Up @@ -249,6 +249,12 @@ private void executeQuery() {

}
catch (SQLException e) {
try {
release();
}
catch (RuntimeException e2) {
e.addSuppressed( e2 );
}
throw executionContext.getSession().getJdbcServices().getSqlExceptionHelper().convert(
e,
"JDBC exception executing SQL [" + finalSql + "]"
Expand Down
@@ -0,0 +1,112 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.query;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.List;

import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.engine.spi.SessionFactoryImplementor;

import org.hibernate.testing.orm.domain.StandardDomainModel;
import org.hibernate.testing.orm.domain.contacts.Contact;
import org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProvider;
import org.hibernate.testing.orm.jdbc.PreparedStatementSpyConnectionProviderSettingProvider;
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.SettingProvider;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;

@Jpa(
standardModels = StandardDomainModel.CONTACTS,
settingProviders = {
@SettingProvider(settingName = AvailableSettings.CONNECTION_PROVIDER,
provider = PreparedStatementSpyConnectionProviderSettingProvider.class)
}
)
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsJdbcDriverProxying.class)
public class QuerySqlExceptionTest {

private PreparedStatementSpyConnectionProvider connectionProvider;

@BeforeAll
public void init(EntityManagerFactoryScope scope) {
connectionProvider = (PreparedStatementSpyConnectionProvider) scope.getEntityManagerFactory().getProperties()
.get( AvailableSettings.CONNECTION_PROVIDER );
}

@Test
public void sqlExceptionOnExecutionWillCloseStatement(EntityManagerFactoryScope scope) {
// We need at least one row in the "contacts" table,
// otherwise the SELECT below might not even get executed completely
// (the DB somehow detects the result will be 0 rows anyway and doesn't bother evaluating parameters).
scope.inTransaction( entityManager -> {
var contact = new Contact(
1,
new Contact.Name( "John", "Doe" ),
Contact.Gender.MALE,
LocalDate.of( 1970, 1, 1 )
);
entityManager.persist( contact );
} );
scope.inTransaction( entityManager -> {
connectionProvider.clear();
assertThatThrownBy( () -> createQueryTriggeringStatementExecutionFailure( entityManager ).getResultList() )
.satisfiesAnyOf(
// Behavior differs depending on the dialect
e -> assertThat( e ).isInstanceOf( SQLException.class ),
e -> assertThat( e ).hasCauseInstanceOf( SQLException.class ),
e -> assertThat( e ).hasRootCauseInstanceOf( SQLException.class )
);
// Checking immediately, because the JDBC driver or connection pool might "fix" statement leaks
// when the connection gets closed on transaction commit.
assertThat( connectionProvider.getPreparedStatementsAndSql().entrySet() )
.isNotEmpty()
.allSatisfy( e -> {
try {
assertThat( e.getKey().isClosed() )
.as( "Statement '" + e.getValue() + "' is closed" )
.isTrue();
}
catch (SQLException ex) {
throw new RuntimeException( ex );
}
} );
} );
}

// Creates a query that will intentionally trigger an exception
// during statement execution (not during preparation).
private Query createQueryTriggeringStatementExecutionFailure(EntityManager entityManager) {
var dialect = entityManager.getEntityManagerFactory().unwrap( SessionFactoryImplementor.class )
.getJdbcServices().getDialect();
Object badParamValue;
if ( dialect instanceof MySQLDialect ) {
// These databases are perfectly fine with the operation `"foo" / 2`
// and will happily return `0.0` without any error...
// Let's give them something even more nonsensical
// (but which we cannot pass to other DBs as they would detect the problem too early)
badParamValue = List.of( "foo", "bar" );
}
else {
badParamValue = "foo";
}
return entityManager.createNativeQuery( "select ( :param / 2 ) from contacts" )
.setParameter( "param", badParamValue );
}
}
Expand Up @@ -150,6 +150,14 @@ public List<PreparedStatement> getPreparedStatements() {
return new ArrayList<>( preparedStatementMap.keySet() );
}

/**
* @return the PreparedStatements that were executed since the last clear operation,
* along with each statement's corresponding SQL.
*/
public Map<PreparedStatement, String> getPreparedStatementsAndSql() {
return preparedStatementMap;
}

/**
* Get the PreparedStatements SQL statements.
*
Expand Down

0 comments on commit 20842f8

Please sign in to comment.