HHH-13926 Propagate the original SQL to the Expectation so it can be logged

This commit is contained in:
Sanne Grinovero 2020-07-09 15:07:05 +01:00
parent 6c787d27bd
commit 3a2283335e
8 changed files with 36 additions and 31 deletions

View File

@ -112,7 +112,7 @@ public class BatchingBatch extends AbstractBatchImpl {
final JdbcObserver observer = getJdbcCoordinator().getJdbcSessionOwner().getJdbcSessionContext().getObserver(); final JdbcObserver observer = getJdbcCoordinator().getJdbcSessionOwner().getJdbcSessionContext().getObserver();
try { try {
for ( Map.Entry<String,PreparedStatement> entry : getStatements().entrySet() ) { for ( Map.Entry<String,PreparedStatement> entry : getStatements().entrySet() ) {
String sql = entry.getKey(); final String sql = entry.getKey();
try { try {
final PreparedStatement statement = entry.getValue(); final PreparedStatement statement = entry.getValue();
final int[] rowCounts; final int[] rowCounts;
@ -123,7 +123,7 @@ public class BatchingBatch extends AbstractBatchImpl {
finally { finally {
observer.jdbcExecuteBatchEnd(); observer.jdbcExecuteBatchEnd();
} }
checkRowCounts( rowCounts, statement ); checkRowCounts( rowCounts, statement, sql );
} }
catch ( SQLException e ) { catch ( SQLException e ) {
abortBatch(); abortBatch();
@ -142,13 +142,13 @@ public class BatchingBatch extends AbstractBatchImpl {
} }
} }
private void checkRowCounts(int[] rowCounts, PreparedStatement ps) throws SQLException, HibernateException { private void checkRowCounts(int[] rowCounts, PreparedStatement ps, String statementSQL) throws SQLException, HibernateException {
final int numberOfRowCounts = rowCounts.length; final int numberOfRowCounts = rowCounts.length;
if ( batchPosition != 0 && numberOfRowCounts != batchPosition / getStatements().size() ) { if ( batchPosition != 0 && numberOfRowCounts != batchPosition / getStatements().size() ) {
LOG.unexpectedRowCounts(); LOG.unexpectedRowCounts();
} }
for ( int i = 0; i < numberOfRowCounts; i++ ) { for ( int i = 0; i < numberOfRowCounts; i++ ) {
getKey().getExpectation().verifyOutcome( rowCounts[i], ps, i ); getKey().getExpectation().verifyOutcome( rowCounts[i], ps, i, statementSQL );
} }
} }
} }

View File

@ -40,16 +40,17 @@ public class NonBatchingBatch extends AbstractBatchImpl {
public void addToBatch() { public void addToBatch() {
notifyObserversImplicitExecution(); notifyObserversImplicitExecution();
for ( Map.Entry<String,PreparedStatement> entry : getStatements().entrySet() ) { for ( Map.Entry<String,PreparedStatement> entry : getStatements().entrySet() ) {
final String statementSQL = entry.getKey();
try { try {
final PreparedStatement statement = entry.getValue(); final PreparedStatement statement = entry.getValue();
final int rowCount = jdbcCoordinator.getResultSetReturn().executeUpdate( statement ); final int rowCount = jdbcCoordinator.getResultSetReturn().executeUpdate( statement );
getKey().getExpectation().verifyOutcome( rowCount, statement, 0 ); getKey().getExpectation().verifyOutcome( rowCount, statement, 0, statementSQL );
jdbcCoordinator.getResourceRegistry().release( statement ); jdbcCoordinator.getResourceRegistry().release( statement );
jdbcCoordinator.afterStatementExecution(); jdbcCoordinator.afterStatementExecution();
} }
catch ( SQLException e ) { catch ( SQLException e ) {
abortBatch(); abortBatch();
throw sqlExceptionHelper().convert( e, "could not execute non-batched batch statement", entry.getKey() ); throw sqlExceptionHelper().convert( e, "could not execute non-batched batch statement", statementSQL );
} }
catch (JDBCException e) { catch (JDBCException e) {
abortBatch(); abortBatch();

View File

@ -23,10 +23,11 @@ public interface Expectation {
* @param rowCount The RDBMS reported "number of rows affected". * @param rowCount The RDBMS reported "number of rows affected".
* @param statement The statement representing the operation * @param statement The statement representing the operation
* @param batchPosition The position in the batch (if batching) * @param batchPosition The position in the batch (if batching)
* @param statementSQL The SQL backing the prepared statement, for logging purposes
* @throws SQLException Exception from the JDBC driver * @throws SQLException Exception from the JDBC driver
* @throws HibernateException Problem processing the outcome. * @throws HibernateException Problem processing the outcome.
*/ */
public void verifyOutcome(int rowCount, PreparedStatement statement, int batchPosition) throws SQLException, HibernateException; public void verifyOutcome(int rowCount, PreparedStatement statement, int batchPosition, String statementSQL) throws SQLException, HibernateException;
/** /**
* Perform any special statement preparation. * Perform any special statement preparation.

View File

@ -45,17 +45,17 @@ public class Expectations {
} }
} }
public final void verifyOutcome(int rowCount, PreparedStatement statement, int batchPosition) { public final void verifyOutcome(int rowCount, PreparedStatement statement, int batchPosition, String statementSQL) {
rowCount = determineRowCount( rowCount, statement ); rowCount = determineRowCount( rowCount, statement );
if ( batchPosition < 0 ) { if ( batchPosition < 0 ) {
checkNonBatched( rowCount, statement ); checkNonBatched( rowCount, statement, statementSQL );
} }
else { else {
checkBatched( rowCount, batchPosition, statement ); checkBatched( rowCount, batchPosition, statement, statementSQL );
} }
} }
private void checkBatched(int rowCount, int batchPosition, PreparedStatement statement) { private void checkBatched(int rowCount, int batchPosition, PreparedStatement statement, String statementSQL) {
if ( rowCount == -2 ) { if ( rowCount == -2 ) {
LOG.debugf( "Success of batch update unknown: %s", batchPosition ); LOG.debugf( "Success of batch update unknown: %s", batchPosition );
} }
@ -68,7 +68,7 @@ public class Expectations {
"Batch update returned unexpected row count from update [" "Batch update returned unexpected row count from update ["
+ batchPosition + "]; actual row count: " + rowCount + batchPosition + "]; actual row count: " + rowCount
+ "; expected: " + expectedRowCount + "; statement executed: " + "; expected: " + expectedRowCount + "; statement executed: "
+ statement + statementSQL
); );
} }
if ( expectedRowCount < rowCount ) { if ( expectedRowCount < rowCount ) {
@ -80,11 +80,11 @@ public class Expectations {
} }
} }
private void checkNonBatched(int rowCount, PreparedStatement statement) { private void checkNonBatched(int rowCount, PreparedStatement statement, String statementSQL) {
if ( expectedRowCount > rowCount ) { if ( expectedRowCount > rowCount ) {
throw new StaleStateException( throw new StaleStateException(
"Unexpected row count: " + rowCount + "; expected: " + expectedRowCount "Unexpected row count: " + rowCount + "; expected: " + expectedRowCount
+ "; statement executed: " + statement + "; statement executed: " + statementSQL
); );
} }
if ( expectedRowCount < rowCount ) { if ( expectedRowCount < rowCount ) {
@ -150,7 +150,7 @@ public class Expectations {
// Various Expectation instances ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Various Expectation instances ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static final Expectation NONE = new Expectation() { public static final Expectation NONE = new Expectation() {
public void verifyOutcome(int rowCount, PreparedStatement statement, int batchPosition) { public void verifyOutcome(int rowCount, PreparedStatement statement, int batchPosition, String statementSQL) {
// explicitly doAfterTransactionCompletion no checking... // explicitly doAfterTransactionCompletion no checking...
} }

View File

@ -1218,7 +1218,7 @@ public abstract class AbstractCollectionPersister
Expectation expectation = Expectations.appropriateExpectation( getDeleteAllCheckStyle() ); Expectation expectation = Expectations.appropriateExpectation( getDeleteAllCheckStyle() );
boolean callable = isDeleteAllCallable(); boolean callable = isDeleteAllCallable();
boolean useBatch = expectation.canBeBatched(); boolean useBatch = expectation.canBeBatched();
String sql = getSQLDeleteString(); final String sql = getSQLDeleteString();
if ( useBatch ) { if ( useBatch ) {
if ( removeBatchKey == null ) { if ( removeBatchKey == null ) {
removeBatchKey = new BasicBatchKey( removeBatchKey = new BasicBatchKey(
@ -1249,7 +1249,7 @@ public abstract class AbstractCollectionPersister
.addToBatch(); .addToBatch();
} }
else { else {
expectation.verifyOutcome( session.getJdbcCoordinator().getResultSetReturn().executeUpdate( st ), st, -1 ); expectation.verifyOutcome( session.getJdbcCoordinator().getResultSetReturn().executeUpdate( st ), st, -1, sql );
} }
} }
catch ( SQLException sqle ) { catch ( SQLException sqle ) {
@ -1319,7 +1319,7 @@ public abstract class AbstractCollectionPersister
final PreparedStatement st; final PreparedStatement st;
boolean callable = isInsertCallable(); boolean callable = isInsertCallable();
boolean useBatch = expectation.canBeBatched(); boolean useBatch = expectation.canBeBatched();
String sql = getSQLInsertRowString(); final String sql = getSQLInsertRowString();
if ( useBatch ) { if ( useBatch ) {
if ( recreateBatchKey == null ) { if ( recreateBatchKey == null ) {
@ -1358,7 +1358,7 @@ public abstract class AbstractCollectionPersister
} }
else { else {
expectation.verifyOutcome( jdbcCoordinator expectation.verifyOutcome( jdbcCoordinator
.getResultSetReturn().executeUpdate( st ), st, -1 ); .getResultSetReturn().executeUpdate( st ), st, -1, sql );
} }
collection.afterRowInsert( this, entry, i ); collection.afterRowInsert( this, entry, i );
@ -1435,7 +1435,7 @@ public abstract class AbstractCollectionPersister
final PreparedStatement st; final PreparedStatement st;
boolean callable = isDeleteCallable(); boolean callable = isDeleteCallable();
boolean useBatch = expectation.canBeBatched(); boolean useBatch = expectation.canBeBatched();
String sql = getSQLDeleteRowString(); final String sql = getSQLDeleteRowString();
if ( useBatch ) { if ( useBatch ) {
if ( deleteBatchKey == null ) { if ( deleteBatchKey == null ) {
@ -1481,7 +1481,7 @@ public abstract class AbstractCollectionPersister
.addToBatch(); .addToBatch();
} }
else { else {
expectation.verifyOutcome( session.getJdbcCoordinator().getResultSetReturn().executeUpdate( st ), st, -1 ); expectation.verifyOutcome( session.getJdbcCoordinator().getResultSetReturn().executeUpdate( st ), st, -1, sql );
} }
count++; count++;
} }
@ -1593,7 +1593,7 @@ public abstract class AbstractCollectionPersister
session.getJdbcCoordinator().getBatch( insertBatchKey ).addToBatch(); session.getJdbcCoordinator().getBatch( insertBatchKey ).addToBatch();
} }
else { else {
expectation.verifyOutcome( session.getJdbcCoordinator().getResultSetReturn().executeUpdate( st ), st, -1 ); expectation.verifyOutcome( session.getJdbcCoordinator().getResultSetReturn().executeUpdate( st ), st, -1, sql );
} }
collection.afterRowInsert( this, entry, i ); collection.afterRowInsert( this, entry, i );
count++; count++;

View File

@ -309,7 +309,7 @@ public class BasicCollectionPersister extends AbstractCollectionPersister {
expectation.verifyOutcome( expectation.verifyOutcome(
session.getJdbcCoordinator().getResultSetReturn().executeUpdate( session.getJdbcCoordinator().getResultSetReturn().executeUpdate(
st st
), st, -1 ), st, -1, sql
); );
} }
} }

View File

@ -269,7 +269,7 @@ public class OneToManyPersister extends AbstractCollectionPersister {
expectation.verifyOutcome( expectation.verifyOutcome(
session.getJdbcCoordinator() session.getJdbcCoordinator()
.getResultSetReturn() .getResultSetReturn()
.executeUpdate( st ), st, -1 .executeUpdate( st ), st, -1, sql
); );
} }
} }
@ -378,7 +378,7 @@ public class OneToManyPersister extends AbstractCollectionPersister {
deleteExpectation.verifyOutcome( deleteExpectation.verifyOutcome(
session.getJdbcCoordinator() session.getJdbcCoordinator()
.getResultSetReturn() .getResultSetReturn()
.executeUpdate( st ), st, -1 .executeUpdate( st ), st, -1, sql
); );
} }
count++; count++;
@ -450,7 +450,7 @@ public class OneToManyPersister extends AbstractCollectionPersister {
insertExpectation.verifyOutcome( insertExpectation.verifyOutcome(
session.getJdbcCoordinator() session.getJdbcCoordinator()
.getResultSetReturn() .getResultSetReturn()
.executeUpdate( st ), st, -1 .executeUpdate( st ), st, -1, sql
); );
} }
count++; count++;

View File

@ -2591,9 +2591,10 @@ public abstract class AbstractEntityPersister
Serializable id, Serializable id,
int tableNumber, int tableNumber,
Expectation expectation, Expectation expectation,
PreparedStatement statement) throws HibernateException { PreparedStatement statement,
String statementSQL) throws HibernateException {
try { try {
expectation.verifyOutcome( rows, statement, -1 ); expectation.verifyOutcome( rows, statement, -1, statementSQL );
} }
catch (StaleStateException e) { catch (StaleStateException e) {
if ( !isNullableTable( tableNumber ) ) { if ( !isNullableTable( tableNumber ) ) {
@ -3251,7 +3252,7 @@ public abstract class AbstractEntityPersister
expectation.verifyOutcome( expectation.verifyOutcome(
session.getJdbcCoordinator() session.getJdbcCoordinator()
.getResultSetReturn() .getResultSetReturn()
.executeUpdate( insert ), insert, -1 .executeUpdate( insert ), insert, -1, sql
); );
} }
} }
@ -3450,7 +3451,8 @@ public abstract class AbstractEntityPersister
id, id,
j, j,
expectation, expectation,
update update,
sql
); );
} }
@ -3575,7 +3577,8 @@ public abstract class AbstractEntityPersister
id, id,
j, j,
expectation, expectation,
delete delete,
sql
); );
} }