Yes, much cleaner...

This commit is contained in:
Bryan Varner 2012-04-19 16:36:54 -04:00 committed by Steve Ebersole
parent c1849d56f6
commit cb1c485774
2 changed files with 52 additions and 40 deletions

View File

@ -23,21 +23,25 @@
*/
package org.hibernate.dialect;
import java.sql.SQLException;
import java.sql.Types;
import org.jboss.logging.Logger;
import org.hibernate.JDBCException;
import org.hibernate.PessimisticLockException;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.function.AvgWithArgumentCastFunction;
import org.hibernate.dialect.function.NoArgSQLFunction;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.dialect.function.VarArgsSQLFunction;
import org.hibernate.exception.LockAcquisitionException;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtracter;
import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.type.StandardBasicTypes;
import org.jboss.logging.Logger;
import java.sql.SQLException;
import java.sql.Types;
/**
* A dialect compatible with the H2 database.
@ -292,6 +296,33 @@ public class H2Dialect extends Dialect {
return constraintName;
}
};
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
SQLExceptionConversionDelegate delegate = super.buildSQLExceptionConversionDelegate();
if (delegate == null) {
delegate = new SQLExceptionConversionDelegate() {
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
JDBCException exception = null;
int errorCode = JdbcExceptionHelper.extractErrorCode(sqlException);
if (40001 == errorCode) { // DEADLOCK DETECTED
exception = new LockAcquisitionException(message, sqlException, sql);
}
if (50200 == errorCode) { // LOCK NOT AVAILABLE
exception = new PessimisticLockException(message, sqlException, sql);
}
return exception;
}
};
}
return delegate;
}
@Override
public boolean supportsTemporaryTables() {

View File

@ -49,6 +49,7 @@ import org.hibernate.ejb.AvailableSettings;
import org.hibernate.ejb.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.TestForIssue;
@ -93,7 +94,6 @@ public class LockTest extends BaseEntityManagerFunctionalTestCase {
@Test
@TestForIssue( jiraKey = "HHH-7252" )
// @FailureExpected( jiraKey = "HHH-7252" )
@RequiresDialectFeature( value = DialectChecks.SupportsLockTimeouts.class,
comment = "Test verifies proper exception throwing when a lock timeout is specified.",
jiraKey = "HHH-7252" )
@ -123,11 +123,10 @@ public class LockTest extends BaseEntityManagerFunctionalTestCase {
} catch (PessimisticLockException pe) {
assertTrue("Find with immediate timeout should have thrown LockTimeoutException.", false);
} catch (PersistenceException pe) {
log.warn("EntityManager.find() for PESSIMISTIC_WRITE with timeout of 0 threw a PersistenceException.\n" +
"This is likely a consequence of the Dialect not properly mapping SQL errors into the correct HibernateException subtypes.\n" +
log.info("EntityManager.find() for PESSIMISTIC_WRITE with timeout of 0 threw a PersistenceException.\n" +
"This is likely a consequence of " + getDialect().getClass().getName() + " not properly mapping SQL errors into the correct HibernateException subtypes.\n" +
"See HHH-7251 for an example of one such situation.", pe);
// Ideally, I'd like to see this here. It would ensure that the proper lock exceptions were being mapped for each dialect.
//assertTrue("EntityManager should be throwing LockTimeoutException.", false);
assertTrue("EntityManager should be throwing LockTimeoutException.", false);
} finally {
if (em3.getTransaction().getRollbackOnly()) {
em3.getTransaction().rollback();
@ -454,14 +453,11 @@ public class LockTest extends BaseEntityManagerFunctionalTestCase {
}
@Test
@RequiresDialect( value = Oracle10gDialect.class )
@RequiresDialectFeature( value = DialectChecks.SupportsLockTimeouts.class )
public void testContendedPessimisticReadLockTimeout() throws Exception {
EntityManager em = getOrCreateEntityManager();
final EntityManager em2 = createIsolatedEntityManager();
// TODO: replace dialect instanceof test with a Dialect.hasCapability (e.g. supportsPessimisticLockTimeout)
if ( !( getDialect() instanceof Oracle10gDialect ) ) {
log.info( "skipping testContendedPessimisticReadLockTimeout" );
return;
}
Lock lock = new Lock();
Thread t = null;
FutureTask<Boolean> bgTask = null;
@ -550,15 +546,12 @@ public class LockTest extends BaseEntityManagerFunctionalTestCase {
}
@Test
@RequiresDialect( value = Oracle10gDialect.class )
@RequiresDialectFeature( value = DialectChecks.SupportsLockTimeouts.class )
public void testContendedPessimisticWriteLockTimeout() throws Exception {
EntityManager em = getOrCreateEntityManager();
final EntityManager em2 = createIsolatedEntityManager();
// TODO: replace dialect instanceof test with a Dialect.hasCapability (e.g. supportsPessimisticLockTimeout)
if ( !( getDialect() instanceof Oracle10gDialect ) ) {
log.info( "skipping testContendedPessimisticWriteLockTimeout" );
return;
}
Lock lock = new Lock();
Thread t = null;
FutureTask<Boolean> bgTask;
@ -642,15 +635,12 @@ public class LockTest extends BaseEntityManagerFunctionalTestCase {
}
@Test
@RequiresDialect( value = Oracle10gDialect.class )
@RequiresDialectFeature( value = DialectChecks.SupportsLockTimeouts.class )
public void testContendedPessimisticWriteLockNoWait() throws Exception {
EntityManager em = getOrCreateEntityManager();
final EntityManager em2 = createIsolatedEntityManager();
// TODO: replace dialect instanceof test with a Dialect.hasCapability (e.g. supportsPessimisticLockTimeout)
if ( !( getDialect() instanceof Oracle10gDialect ) ) {
log.info( "skipping testContendedPessimisticWriteLockNoWait" );
return;
}
Lock lock = new Lock();
Thread t = null;
FutureTask<Boolean> bgTask;
@ -734,15 +724,12 @@ public class LockTest extends BaseEntityManagerFunctionalTestCase {
}
@Test
@RequiresDialect( value = Oracle10gDialect.class )
@RequiresDialectFeature( value = DialectChecks.SupportsLockTimeouts.class )
public void testQueryTimeout() throws Exception {
EntityManager em = getOrCreateEntityManager();
final EntityManager em2 = createIsolatedEntityManager();
// TODO: replace dialect instanceof test with a Dialect.hasCapability
if ( !( getDialect() instanceof Oracle10gDialect ) ) {
log.info( "skipping testQueryTimeout" );
return;
}
Lock lock = new Lock();
Thread t = null;
FutureTask<Boolean> bgTask;
@ -830,12 +817,9 @@ public class LockTest extends BaseEntityManagerFunctionalTestCase {
}
@Test
@RequiresDialect( value = Oracle10gDialect.class )
@RequiresDialectFeature( value = DialectChecks.SupportsLockTimeouts.class )
public void testQueryTimeoutEMProps() throws Exception {
// TODO: replace dialect instanceof test with a Dialect.hasCapability
if ( !( getDialect() instanceof Oracle10gDialect ) ) {
log.info( "skipping testQueryTimeout" );
return;
}
EntityManager em = getOrCreateEntityManager();
Map<String, Object> queryTimeoutProps = new HashMap<String, Object>();
queryTimeoutProps.put( "javax.persistence.query.timeout", 500 ); // 1 sec timeout (should round up)
@ -926,17 +910,14 @@ public class LockTest extends BaseEntityManagerFunctionalTestCase {
}
@Test
@RequiresDialect( value = Oracle10gDialect.class )
@RequiresDialectFeature( value = DialectChecks.SupportsLockTimeouts.class )
public void testLockTimeoutEMProps() throws Exception {
EntityManager em = getOrCreateEntityManager();
Map<String, Object> TimeoutProps = new HashMap<String, Object>();
TimeoutProps.put( "javax.persistence.lock.timeout", 1000 ); // 1 second timeout
final EntityManager em2 = createIsolatedEntityManager( TimeoutProps );
// TODO: replace dialect instanceof test with a Dialect.hasCapability (e.g. supportsPessimisticLockTimeout)
if ( !( getDialect() instanceof Oracle10gDialect ) ) {
log.info( "skipping testLockTimeoutEMProps" );
return;
}
Lock lock = new Lock();
Thread t = null;
FutureTask<Boolean> bgTask;