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();
try {
for ( Map.Entry<String,PreparedStatement> entry : getStatements().entrySet() ) {
String sql = entry.getKey();
final String sql = entry.getKey();
try {
final PreparedStatement statement = entry.getValue();
final int[] rowCounts;
@ -123,7 +123,7 @@ public class BatchingBatch extends AbstractBatchImpl {
finally {
observer.jdbcExecuteBatchEnd();
}
checkRowCounts( rowCounts, statement );
checkRowCounts( rowCounts, statement, sql );
}
catch ( SQLException e ) {
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;
if ( batchPosition != 0 && numberOfRowCounts != batchPosition / getStatements().size() ) {
LOG.unexpectedRowCounts();
}
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() {
notifyObserversImplicitExecution();
for ( Map.Entry<String,PreparedStatement> entry : getStatements().entrySet() ) {
final String statementSQL = entry.getKey();
try {
final PreparedStatement statement = entry.getValue();
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.afterStatementExecution();
}
catch ( SQLException e ) {
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) {
abortBatch();

View File

@ -23,10 +23,11 @@ public interface Expectation {
* @param rowCount The RDBMS reported "number of rows affected".
* @param statement The statement representing the operation
* @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 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.

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 );
if ( batchPosition < 0 ) {
checkNonBatched( rowCount, statement );
checkNonBatched( rowCount, statement, statementSQL );
}
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 ) {
LOG.debugf( "Success of batch update unknown: %s", batchPosition );
}
@ -68,7 +68,7 @@ public class Expectations {
"Batch update returned unexpected row count from update ["
+ batchPosition + "]; actual row count: " + rowCount
+ "; expected: " + expectedRowCount + "; statement executed: "
+ statement
+ statementSQL
);
}
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 ) {
throw new StaleStateException(
"Unexpected row count: " + rowCount + "; expected: " + expectedRowCount
+ "; statement executed: " + statement
+ "; statement executed: " + statementSQL
);
}
if ( expectedRowCount < rowCount ) {
@ -150,7 +150,7 @@ public class Expectations {
// Various Expectation instances ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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...
}

View File

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

View File

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

View File

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

View File

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