|
From: <hib...@li...> - 2006-08-04 17:39:17
|
Author: ste...@jb...
Date: 2006-08-04 13:39:14 -0400 (Fri, 04 Aug 2006)
New Revision: 10216
Added:
trunk/Hibernate3/src/org/hibernate/jdbc/BatchFailedException.java
trunk/Hibernate3/src/org/hibernate/jdbc/BatchedTooManyRowsAffectedException.java
trunk/Hibernate3/src/org/hibernate/jdbc/TooManyRowsAffectedException.java
Modified:
trunk/Hibernate3/src/org/hibernate/jdbc/Expectations.java
trunk/Hibernate3/src/org/hibernate/persister/entity/AbstractEntityPersister.java
Log:
HHH-1959 : apply check attribute properly in non-batched scenarios
Added: trunk/Hibernate3/src/org/hibernate/jdbc/BatchFailedException.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/jdbc/BatchFailedException.java 2006-08-04 17:35:08 UTC (rev 10215)
+++ trunk/Hibernate3/src/org/hibernate/jdbc/BatchFailedException.java 2006-08-04 17:39:14 UTC (rev 10216)
@@ -0,0 +1,18 @@
+package org.hibernate.jdbc;
+
+import org.hibernate.HibernateException;
+
+/**
+ * Indicates a failed batch entry (-3 return).
+ *
+ * @author Steve Ebersole
+ */
+public class BatchFailedException extends HibernateException {
+ public BatchFailedException(String s) {
+ super( s );
+ }
+
+ public BatchFailedException(String string, Throwable root) {
+ super( string, root );
+ }
+}
Added: trunk/Hibernate3/src/org/hibernate/jdbc/BatchedTooManyRowsAffectedException.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/jdbc/BatchedTooManyRowsAffectedException.java 2006-08-04 17:35:08 UTC (rev 10215)
+++ trunk/Hibernate3/src/org/hibernate/jdbc/BatchedTooManyRowsAffectedException.java 2006-08-04 17:39:14 UTC (rev 10216)
@@ -0,0 +1,21 @@
+package org.hibernate.jdbc;
+
+/**
+ * Much like {@link TooManyRowsAffectedException}, indicates that more
+ * rows than what we were expcecting were affected. Additionally, this form
+ * occurs from a batch and carries along the batch positon that failed.
+ *
+ * @author Steve Ebersole
+ */
+public class BatchedTooManyRowsAffectedException extends TooManyRowsAffectedException {
+ private final int batchPosition;
+
+ public BatchedTooManyRowsAffectedException(String message, int expectedRowCount, int actualRowCount, int batchPosition) {
+ super( message, expectedRowCount, actualRowCount );
+ this.batchPosition = batchPosition;
+ }
+
+ public int getBatchPosition() {
+ return batchPosition;
+ }
+}
Modified: trunk/Hibernate3/src/org/hibernate/jdbc/Expectations.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/jdbc/Expectations.java 2006-08-04 17:35:08 UTC (rev 10215)
+++ trunk/Hibernate3/src/org/hibernate/jdbc/Expectations.java 2006-08-04 17:39:14 UTC (rev 10216)
@@ -54,7 +54,7 @@
}
}
else if ( rowCount == -3 ) {
- throw new HibernateException( "Batch update failed: " + batchPosition );
+ throw new BatchFailedException( "Batch update failed: " + batchPosition );
}
else {
if ( expectedRowCount > rowCount ) {
@@ -65,11 +65,10 @@
);
}
if ( expectedRowCount < rowCount ) {
- throw new HibernateException(
- "Batch update returned unexpected row count from update [" + batchPosition +
- "]; actual row count: " + rowCount +
- "; expected: " + expectedRowCount
- );
+ String msg = "Batch update returned unexpected row count from update [" +
+ batchPosition + "]; actual row count: " + rowCount +
+ "; expected: " + expectedRowCount;
+ throw new BatchedTooManyRowsAffectedException( msg, expectedRowCount, rowCount, batchPosition );
}
}
}
@@ -81,9 +80,8 @@
);
}
if ( expectedRowCount < rowCount ) {
- throw new HibernateException(
- "Unexpected row count: " + rowCount + "; expected: " + expectedRowCount
- );
+ String msg = "Unexpected row count: " + rowCount + "; expected: " + expectedRowCount;
+ throw new TooManyRowsAffectedException( msg, expectedRowCount, rowCount );
}
}
Added: trunk/Hibernate3/src/org/hibernate/jdbc/TooManyRowsAffectedException.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/jdbc/TooManyRowsAffectedException.java 2006-08-04 17:35:08 UTC (rev 10215)
+++ trunk/Hibernate3/src/org/hibernate/jdbc/TooManyRowsAffectedException.java 2006-08-04 17:39:14 UTC (rev 10216)
@@ -0,0 +1,29 @@
+package org.hibernate.jdbc;
+
+import org.hibernate.HibernateException;
+
+/**
+ * Indicates that more rows were affected then we were expecting to be.
+ * Typically indicates presence of duplicate "PK" values in the
+ * given table.
+ *
+ * @author Steve Ebersole
+ */
+public class TooManyRowsAffectedException extends HibernateException {
+ private final int expectedRowCount;
+ private final int actualRowCount;
+
+ public TooManyRowsAffectedException(String message, int expectedRowCount, int actualRowCount) {
+ super( message );
+ this.expectedRowCount = expectedRowCount;
+ this.actualRowCount = actualRowCount;
+ }
+
+ public int getExpectedRowCount() {
+ return expectedRowCount;
+ }
+
+ public int getActualRowCount() {
+ return actualRowCount;
+ }
+}
Modified: trunk/Hibernate3/src/org/hibernate/persister/entity/AbstractEntityPersister.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/persister/entity/AbstractEntityPersister.java 2006-08-04 17:35:08 UTC (rev 10215)
+++ trunk/Hibernate3/src/org/hibernate/persister/entity/AbstractEntityPersister.java 2006-08-04 17:39:14 UTC (rev 10216)
@@ -24,8 +24,10 @@
import org.hibernate.MappingException;
import org.hibernate.QueryException;
import org.hibernate.StaleObjectStateException;
+import org.hibernate.StaleStateException;
import org.hibernate.jdbc.Expectation;
import org.hibernate.jdbc.Expectations;
+import org.hibernate.jdbc.TooManyRowsAffectedException;
import org.hibernate.dialect.lock.LockingStrategy;
import org.hibernate.cache.CacheConcurrencyStrategy;
import org.hibernate.cache.CacheKey;
@@ -1683,8 +1685,11 @@
return createEntityLoader( lockMode, CollectionHelper.EMPTY_MAP );
}
- protected boolean check(int rows, Serializable id, int tableNumber) throws HibernateException {
- if ( rows < 1 ) {
+ protected boolean check(int rows, Serializable id, int tableNumber, Expectation expectation, PreparedStatement statement) throws HibernateException {
+ try {
+ expectation.verifyOutcome( rows, statement, -1 );
+ }
+ catch( StaleStateException e ) {
if ( !isNullableTable( tableNumber ) ) {
if ( getFactory().getStatistics().isStatisticsEnabled() ) {
getFactory().getStatisticsImplementor()
@@ -1693,13 +1698,16 @@
throw new StaleObjectStateException( getEntityName(), id );
}
}
- else if ( rows > 1 ) {
+ catch( TooManyRowsAffectedException e ) {
throw new HibernateException(
"Duplicate identifier in table for: " +
MessageHelper.infoString( this, id, getFactory() )
);
}
- return rows > 0; //it could be zero if we have a "nullable" table
+ catch ( Throwable t ) {
+ return false;
+ }
+ return true;
}
protected String generateUpdateString(boolean[] includeProperty, int j, boolean useRowId) {
@@ -2173,7 +2181,7 @@
session.getBatcher().addToBatch( expectation );
}
else {
- insert.executeUpdate();
+ expectation.verifyOutcome( insert.executeUpdate(), insert, -1 );
}
}
@@ -2330,7 +2338,7 @@
return true;
}
else {
- return check( update.executeUpdate(), id, j );
+ return check( update.executeUpdate(), id, j, expectation, update );
}
}
@@ -2362,11 +2370,11 @@
*/
protected void delete(
final Serializable id,
- final Object version,
- final int j,
- final Object object,
- final String sql,
- final SessionImplementor session) throws HibernateException {
+ final Object version,
+ final int j,
+ final Object object,
+ final String sql,
+ final SessionImplementor session) throws HibernateException {
if ( isInverseTable( j ) ) {
return;
@@ -2431,7 +2439,7 @@
session.getBatcher().addToBatch( expectation );
}
else {
- check( delete.executeUpdate(), id, j );
+ check( delete.executeUpdate(), id, j, expectation, delete );
}
}
|