Java源码示例:org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException
示例1
@Test
public void testExternalBatches()
{
assertFalse(dao.externalBatchExists("foo"));
assertFalse(dao.externalBatchExists("bar"));
dao.insertExternalBatch("foo");
assertTrue(dao.externalBatchExists("foo"));
assertFalse(dao.externalBatchExists("bar"));
try {
dao.insertExternalBatch("foo");
fail("expected exception");
}
catch (UnableToExecuteStatementException e) {
assertInstanceOf(e.getCause(), SQLException.class);
assertTrue(((SQLException) e.getCause()).getSQLState().startsWith("23"));
}
}
示例2
public <T> T catchConflict(NewResourceAction<T> function,
String messageFormat, Object... messageParameters)
throws ResourceConflictException
{
try {
return function.call();
}
catch (UnableToExecuteStatementException ex) {
if (ex.getCause() instanceof SQLException) {
SQLException sqlEx = (SQLException) ex.getCause();
if (isConflictException(sqlEx)) {
throw new ResourceConflictException("Resource already exists: " + String.format(messageFormat, messageParameters));
}
}
throw ex;
}
}
示例3
public <T> T catchForeignKeyNotFound(NewResourceAction<T> function,
String messageFormat, Object... messageParameters)
throws ResourceNotFoundException, ResourceConflictException
{
try {
return function.call();
}
catch (UnableToExecuteStatementException ex) {
if (ex.getCause() instanceof SQLException) {
SQLException sqlEx = (SQLException) ex.getCause();
if (isForeignKeyException(sqlEx)) {
throw new ResourceNotFoundException("Resource not found: " + String.format(messageFormat, messageParameters));
}
}
throw ex;
}
}
示例4
private boolean takeLead(UUID leaderId, Duration ttl) {
if (null != jdbi) {
try (Handle h = jdbi.open()) {
try {
int rowsInserted = getPostgresStorage(h).insertLeaderEntry(
leaderId,
reaperInstanceId,
AppContext.REAPER_INSTANCE_ADDRESS
);
if (rowsInserted == 1) { // insert should modify exactly 1 row
return true;
}
} catch (UnableToExecuteStatementException e) {
if (JdbiExceptionUtil.isDuplicateKeyError(e)) {
// if it's a duplicate key error, then try to update it
int rowsUpdated = getPostgresStorage(h).updateLeaderEntry(
leaderId,
reaperInstanceId,
AppContext.REAPER_INSTANCE_ADDRESS,
getExpirationTime(ttl)
);
if (rowsUpdated == 1) { // if row updated, took ownership from an expired leader
LOG.debug("Took lead from expired entry for segment {}", leaderId);
return true;
}
}
return false;
}
}
}
LOG.warn("Unknown error occurred while taking lead on segment {}", leaderId);
return false;
}
示例5
@Test(expected = UnableToExecuteStatementException.class)
public void openThrowsUnableToExecuteStatementExceptionIfQueryThrowsSqlException() throws Exception {
when(mockPreparedStatement.execute()).thenThrow(new SQLException());
getBasicDTOSqlExtractor();
}
示例6
public static boolean isDuplicateKeyError(UnableToExecuteStatementException exc) {
if (exc.getCause() instanceof PSQLException) {
return ((PSQLException) exc.getCause()).getSQLState().equals(DUPLICATE_KEY_CODE);
}
return false;
}