HHH-8687 Better exception message for PostgreSQL81Dialect and named REF_CURSOR parameter binding

This commit is contained in:
Dmytro Bondar 2016-07-11 10:44:26 +03:00 committed by Vlad Mihalcea
parent fa00cb3f41
commit 63ea1f812a
2 changed files with 30 additions and 10 deletions

View File

@ -587,7 +587,7 @@ public ResultSet getResultSet(CallableStatement statement, int position) throws
@Override @Override
public ResultSet getResultSet(CallableStatement statement, String name) throws SQLException { public ResultSet getResultSet(CallableStatement statement, String name) throws SQLException {
throw new UnsupportedOperationException( "PostgreSQL only supports accessing REF_CURSOR parameters by name" ); throw new UnsupportedOperationException( "PostgreSQL only supports accessing REF_CURSOR parameters by position" );
} }
@Override @Override

View File

@ -6,23 +6,29 @@
*/ */
package org.hibernate.dialect; package org.hibernate.dialect;
import java.sql.BatchUpdateException;
import java.sql.CallableStatement;
import java.sql.SQLException;
import org.hibernate.JDBCException; import org.hibernate.JDBCException;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.LockOptions; import org.hibernate.LockOptions;
import org.hibernate.PessimisticLockException; import org.hibernate.PessimisticLockException;
import org.hibernate.exception.LockAcquisitionException; import org.hibernate.exception.LockAcquisitionException;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate; import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test; import org.junit.Test;
import java.sql.BatchUpdateException; import org.mockito.Mockito;
import java.sql.SQLException;
import static junit.framework.TestCase.assertEquals;
import static org.hamcrest.core.Is.is; import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/** /**
* Testing of patched support for PostgreSQL Lock error detection. HHH-7251 * Testing of patched support for PostgreSQL Lock error detection. HHH-7251
@ -30,27 +36,27 @@
* @author Bryan Varner * @author Bryan Varner
*/ */
public class PostgreSQL81DialectTestCase extends BaseUnitTestCase { public class PostgreSQL81DialectTestCase extends BaseUnitTestCase {
@Test @Test
public void testDeadlockException() { public void testDeadlockException() {
PostgreSQL81Dialect dialect = new PostgreSQL81Dialect(); PostgreSQL81Dialect dialect = new PostgreSQL81Dialect();
SQLExceptionConversionDelegate delegate = dialect.buildSQLExceptionConversionDelegate(); SQLExceptionConversionDelegate delegate = dialect.buildSQLExceptionConversionDelegate();
assertNotNull(delegate); assertNotNull(delegate);
JDBCException exception = delegate.convert(new SQLException("Deadlock Detected", "40P01"), "", ""); JDBCException exception = delegate.convert(new SQLException("Deadlock Detected", "40P01"), "", "");
assertTrue(exception instanceof LockAcquisitionException); assertTrue(exception instanceof LockAcquisitionException);
} }
@Test @Test
public void testTimeoutException() { public void testTimeoutException() {
PostgreSQL81Dialect dialect = new PostgreSQL81Dialect(); PostgreSQL81Dialect dialect = new PostgreSQL81Dialect();
SQLExceptionConversionDelegate delegate = dialect.buildSQLExceptionConversionDelegate(); SQLExceptionConversionDelegate delegate = dialect.buildSQLExceptionConversionDelegate();
assertNotNull(delegate); assertNotNull(delegate);
JDBCException exception = delegate.convert(new SQLException("Lock Not Available", "55P03"), "", ""); JDBCException exception = delegate.convert(new SQLException("Lock Not Available", "55P03"), "", "");
assertTrue(exception instanceof PessimisticLockException); assertTrue(exception instanceof PessimisticLockException);
} }
/** /**
* Tests that getForUpdateString(String aliases, LockOptions lockOptions) will return a String * Tests that getForUpdateString(String aliases, LockOptions lockOptions) will return a String
* that will effect the SELECT ... FOR UPDATE OF tableAlias1, ..., tableAliasN * that will effect the SELECT ... FOR UPDATE OF tableAlias1, ..., tableAliasN
@ -60,10 +66,10 @@ public void testGetForUpdateStringWithAliasesAndLockOptions() {
PostgreSQL81Dialect dialect = new PostgreSQL81Dialect(); PostgreSQL81Dialect dialect = new PostgreSQL81Dialect();
LockOptions lockOptions = new LockOptions(); LockOptions lockOptions = new LockOptions();
lockOptions.setAliasSpecificLockMode("tableAlias1", LockMode.PESSIMISTIC_WRITE); lockOptions.setAliasSpecificLockMode("tableAlias1", LockMode.PESSIMISTIC_WRITE);
String forUpdateClause = dialect.getForUpdateString("tableAlias1", lockOptions); String forUpdateClause = dialect.getForUpdateString("tableAlias1", lockOptions);
assertTrue("for update of tableAlias1".equals(forUpdateClause)); assertTrue("for update of tableAlias1".equals(forUpdateClause));
lockOptions.setAliasSpecificLockMode("tableAlias2", LockMode.PESSIMISTIC_WRITE); lockOptions.setAliasSpecificLockMode("tableAlias2", LockMode.PESSIMISTIC_WRITE);
forUpdateClause = dialect.getForUpdateString("tableAlias1,tableAlias2", lockOptions); forUpdateClause = dialect.getForUpdateString("tableAlias1,tableAlias2", lockOptions);
assertTrue("for update of tableAlias1,tableAlias2".equals(forUpdateClause)); assertTrue("for update of tableAlias1,tableAlias2".equals(forUpdateClause));
@ -78,4 +84,18 @@ public void testExtractConstraintName() {
String constraintName = dialect.getViolatedConstraintNameExtracter().extractConstraintName(batchUpdateException); String constraintName = dialect.getViolatedConstraintNameExtracter().extractConstraintName(batchUpdateException);
assertThat(constraintName, is("uk_4bm1x2ultdmq63y3h5r3eg0ej")); assertThat(constraintName, is("uk_4bm1x2ultdmq63y3h5r3eg0ej"));
} }
@Test
@TestForIssue(jiraKey = "HHH-8687")
public void testMessageException() throws SQLException {
PostgreSQL81Dialect dialect = new PostgreSQL81Dialect();
try {
dialect.getResultSet( Mockito.mock( CallableStatement.class), "abc" );
fail( "Expected UnsupportedOperationException" );
}
catch (Exception e) {
assertTrue( e instanceof UnsupportedOperationException );
assertEquals( "PostgreSQL only supports accessing REF_CURSOR parameters by position", e.getMessage() );
}
}
} }