下面列出了org.hibernate.ObjectNotFoundException#org.springframework.dao.InvalidDataAccessResourceUsageException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public RedisSentinelConnection getSentinelConnection() {
if (!redisson.getConfig().isSentinelConfig()) {
throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode");
}
SentinelConnectionManager manager = ((SentinelConnectionManager)((Redisson)redisson).getConnectionManager());
for (RedisClient client : manager.getSentinels()) {
org.redisson.client.RedisConnection connection = client.connect();
try {
String res = connection.sync(RedisCommands.PING);
if ("pong".equalsIgnoreCase(res)) {
return new RedissonSentinelConnection(connection);
}
} catch (Exception e) {
log.warn("Can't connect to " + client, e);
connection.closeAsync();
}
}
throw new InvalidDataAccessResourceUsageException("Sentinels are not found");
}
@Override
public RedisSentinelConnection getSentinelConnection() {
if (!redisson.getConfig().isSentinelConfig()) {
throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode");
}
SentinelConnectionManager manager = ((SentinelConnectionManager)((Redisson)redisson).getConnectionManager());
for (RedisClient client : manager.getSentinels()) {
org.redisson.client.RedisConnection connection = client.connect();
try {
String res = connection.sync(RedisCommands.PING);
if ("pong".equalsIgnoreCase(res)) {
return new RedissonSentinelConnection(connection);
}
} catch (Exception e) {
log.warn("Can't connect to " + client, e);
connection.closeAsync();
}
}
throw new InvalidDataAccessResourceUsageException("Sentinels are not found");
}
@Override
public RedisSentinelConnection getSentinelConnection() {
if (!redisson.getConfig().isSentinelConfig()) {
throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode");
}
SentinelConnectionManager manager = ((SentinelConnectionManager)((Redisson)redisson).getConnectionManager());
for (RedisClient client : manager.getSentinels()) {
org.redisson.client.RedisConnection connection = client.connect();
try {
String res = connection.sync(RedisCommands.PING);
if ("pong".equalsIgnoreCase(res)) {
return new RedissonSentinelConnection(connection);
}
} catch (Exception e) {
log.warn("Can't connect to " + client, e);
connection.closeAsync();
}
}
throw new InvalidDataAccessResourceUsageException("Sentinels are not found");
}
@Override
public RedisSentinelConnection getSentinelConnection() {
if (!redisson.getConfig().isSentinelConfig()) {
throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode");
}
SentinelConnectionManager manager = ((SentinelConnectionManager)((Redisson)redisson).getConnectionManager());
for (RedisClient client : manager.getSentinels()) {
org.redisson.client.RedisConnection connection = client.connect();
try {
String res = connection.sync(RedisCommands.PING);
if ("pong".equalsIgnoreCase(res)) {
return new RedissonSentinelConnection(connection);
}
} catch (Exception e) {
log.warn("Can't connect to " + client, e);
connection.closeAsync();
}
}
throw new InvalidDataAccessResourceUsageException("Sentinels are not found");
}
@Override
public RedisSentinelConnection getSentinelConnection() {
if (!redisson.getConfig().isSentinelConfig()) {
throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode");
}
SentinelConnectionManager manager = ((SentinelConnectionManager)((Redisson)redisson).getConnectionManager());
for (RedisClient client : manager.getSentinels()) {
org.redisson.client.RedisConnection connection = client.connect();
try {
String res = connection.sync(RedisCommands.PING);
if ("pong".equalsIgnoreCase(res)) {
return new RedissonSentinelConnection(connection);
}
} catch (Exception e) {
log.warn("Can't connect to " + client, e);
connection.closeAsync();
}
}
throw new InvalidDataAccessResourceUsageException("Sentinels are not found");
}
@Override
public RedisSentinelConnection getSentinelConnection() {
if (!redisson.getConfig().isSentinelConfig()) {
throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode");
}
SentinelConnectionManager manager = ((SentinelConnectionManager)((Redisson)redisson).getConnectionManager());
for (RedisClient client : manager.getSentinels()) {
org.redisson.client.RedisConnection connection = client.connect();
try {
String res = connection.sync(RedisCommands.PING);
if ("pong".equalsIgnoreCase(res)) {
return new RedissonSentinelConnection(connection);
}
} catch (Exception e) {
log.warn("Can't connect to " + client, e);
connection.closeAsync();
}
}
throw new InvalidDataAccessResourceUsageException("Sentinels are not found");
}
@Override
public RedisSentinelConnection getSentinelConnection() {
if (!redisson.getConfig().isSentinelConfig()) {
throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode");
}
SentinelConnectionManager manager = ((SentinelConnectionManager)((Redisson)redisson).getConnectionManager());
for (RedisClient client : manager.getSentinels()) {
org.redisson.client.RedisConnection connection = client.connect();
try {
String res = connection.sync(RedisCommands.PING);
if ("pong".equalsIgnoreCase(res)) {
return new RedissonSentinelConnection(connection);
}
} catch (Exception e) {
log.warn("Can't connect to " + client, e);
connection.closeAsync();
}
}
throw new InvalidDataAccessResourceUsageException("Sentinels are not found");
}
@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
if (ex instanceof DataAccessException) {
return (DataAccessException) ex;
} else if (ex instanceof DiscoveryException) {
return translateImpl((Neo4jException) ex, TransientDataAccessResourceException::new);
} else if (ex instanceof DatabaseException) {
return translateImpl((Neo4jException) ex, NonTransientDataAccessResourceException::new);
} else if (ex instanceof ServiceUnavailableException) {
return translateImpl((Neo4jException) ex, NonTransientDataAccessResourceException::new);
} else if (ex instanceof SessionExpiredException) {
return translateImpl((Neo4jException) ex, RecoverableDataAccessException::new);
} else if (ex instanceof ProtocolException) {
return translateImpl((Neo4jException) ex, NonTransientDataAccessResourceException::new);
} else if (ex instanceof TransientException) {
return translateImpl((Neo4jException) ex, TransientDataAccessResourceException::new);
} else if (ex instanceof ValueException) {
return translateImpl((Neo4jException) ex, InvalidDataAccessApiUsageException::new);
} else if (ex instanceof AuthenticationException) {
return translateImpl((Neo4jException) ex, PermissionDeniedDataAccessException::new);
} else if (ex instanceof ResultConsumedException) {
return translateImpl((Neo4jException) ex, InvalidDataAccessApiUsageException::new);
} else if (ex instanceof FatalDiscoveryException) {
return translateImpl((Neo4jException) ex, NonTransientDataAccessResourceException::new);
} else if (ex instanceof TransactionNestingException) {
return translateImpl((Neo4jException) ex, InvalidDataAccessApiUsageException::new);
} else if (ex instanceof ClientException) {
return translateImpl((Neo4jException) ex, InvalidDataAccessResourceUsageException::new);
}
log.warn(() -> String.format("Don't know how to translate exception of type %s", ex.getClass()));
return null;
}
@Test
void shouldKeepErrorCodeIntact() {
Neo4jPersistenceExceptionTranslator translator = new Neo4jPersistenceExceptionTranslator();
DataAccessException dataAccessException = translator
.translateExceptionIfPossible(
new ClientException("Neo.ClientError.Statement.EntityNotFound", "Something went wrong."));
assertThat(dataAccessException).isNotNull().isInstanceOf(InvalidDataAccessResourceUsageException.class);
assertThat(dataAccessException.getMessage())
.isEqualTo("Something went wrong.; Error code 'Neo.ClientError.Statement.EntityNotFound'");
}
@Override
public void setResponse(NamedList<Object> response) {
super.setResponse(response);
try {
root = new ObjectMapper().readTree((String) getResponse().get("json"));
} catch (Exception e) {
throw new InvalidDataAccessResourceUsageException("Unable to parse json from response.", e);
}
}
@Override
public NamedList<Object> processResponse(InputStream body, String encoding) {
NamedList<Object> result = new NamedList<Object>();
try {
result.add("json", StreamUtils.copyToString(body, Charset.forName(encoding)));
} catch (IOException e) {
throw new InvalidDataAccessResourceUsageException("Unable to read json from stream.", e);
}
return result;
}
@Test
public void shouldNotStopSchemaExportOnError() throws Exception {
initializeMappingContext(Person.class);
when(crateOperations.execute(isA(CrateAction.class))).thenThrow(new NoSuchTableException("table does not exist", null)).
thenThrow(new InvalidDataAccessResourceUsageException("Error!!"));
CratePersistentEntitySchemaManager manager = new CratePersistentEntitySchemaManager(crateOperations, CREATE);
manager.setIgnoreFailures(true);
manager.afterPropertiesSet();
verify(crateOperations, times(2)).execute(any(CrateAction.class));
}
@Override
public RedisClusterConnection getClusterConnection() {
if (!redisson.getConfig().isClusterConfig()) {
throw new InvalidDataAccessResourceUsageException("Redisson is not in Cluster mode");
}
return new RedissonClusterConnection(redisson);
}
@Override
public RedisClusterConnection getClusterConnection() {
if (!redisson.getConfig().isClusterConfig()) {
throw new InvalidDataAccessResourceUsageException("Redisson is not in Cluster mode");
}
return new RedissonClusterConnection(redisson);
}
@Override
public RedisClusterConnection getClusterConnection() {
if (!redisson.getConfig().isClusterConfig()) {
throw new InvalidDataAccessResourceUsageException("Redisson is not in Cluster mode");
}
return new RedissonClusterConnection(redisson);
}
@Override
public RedisClusterConnection getClusterConnection() {
if (!redisson.getConfig().isClusterConfig()) {
throw new InvalidDataAccessResourceUsageException("Redisson is not in Cluster mode");
}
return new RedissonClusterConnection(redisson);
}
@Override
public RedisClusterConnection getClusterConnection() {
if (!redisson.getConfig().isClusterConfig()) {
throw new InvalidDataAccessResourceUsageException("Redisson is not in Cluster mode");
}
return new RedissonClusterConnection(redisson);
}
@Override
public RedisClusterConnection getClusterConnection() {
if (!redisson.getConfig().isClusterConfig()) {
throw new InvalidDataAccessResourceUsageException("Redisson is not in Cluster mode");
}
return new RedissonClusterConnection(redisson);
}
@Test(expected = InvalidDataAccessResourceUsageException.class)
public void whenRetrievingDataUserNoSelectRights_thenInvalidResourceUsageException() {
final JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
jdbcTemplate.execute("revoke select from tutorialuser");
try {
fooService.findAll();
} finally {
jdbcTemplate.execute("grant select to tutorialuser");
}
}
@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
if(ex instanceof NoNodeAvailableException) {
return new DataAccessResourceFailureException(ex.getMessage(), ex);
}
if(ex instanceof SQLActionException) {
int errorCode = ((SQLActionException)ex).errorCode();
switch (errorCode) {
case DUPLICATE_PRIMARY_KEY:
return new DuplicateKeyException(ex.getMessage(), ex);
case VERSION_CONFLICT:
return new OptimisticLockingFailureException(ex.getMessage(), ex);
case FIELD_VALIDATION_FAILED:
return new DataIntegrityViolationException(ex.getMessage(), ex);
case DUPLICATE_TABLE_NAME:
return new DuplicateTableException(ex.getMessage(), ex);
case UNKNOWN_TABLE:
return new NoSuchTableException(ex.getMessage(), ex);
case INVALID_SQL_STATEMENT_OR_SYNTAX:
case INVALID_TABLE_NAME:
case UNKNOWN_ANALYZER:
case UNKNOWN_COLUMN:
case UNKNOWN_TYPE:
case UNKNOWN_SCHEMA:
case UNKNOWN_PARTITION:
case COLUMN_ALIAS_AMBIGUOUS:
case FEATURE_NOT_SUPPORTED_YET:
case INVALID_ANALYZER_DEFINITION:
case ALTER_TABLE_ALIAS_NOT_SUPPORTED:
case TABLE_ALIAS_CONTAINS_TABLES_WITH_DIFFERENT_SCHEMA:
return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
case UNHANDLED_SERVER_ERROR:
case TASKS_EXECUTION_FAILED:
case SHARDS_NOT_AVAILABLE:
case QUERY_FAILED_ON_SHARDS:
return new DataAccessResourceFailureException(ex.getMessage(), ex);
default:
return new UncategorizedCrateException(ex.getMessage(), ex);
}
}
return null;
}
@Test(expected=DataAccessException.class)
public void shouldNotAlterTableOnError() throws Exception {
initializeMappingContext(Person.class);
List<ColumnMetadata> columns = asList(createColumnMetadata("['name']", STRING),
createColumnMetadata("['age']", INTEGER));
TableMetadata metadata = new TableMetadata("person", columns, DEFAULT_PARAMS);
when(crateOperations.execute(isA(ColumnMetadataAction.class),
isA(ColumnMetadataAction.class))).thenReturn(columns);
when(crateOperations.execute(isA(TableMetadataAction.class),
isA(TableMetadataAction.class))).thenReturn(metadata);
when(crateOperations.execute(isA(CrateAction.class))).thenThrow(new InvalidDataAccessResourceUsageException("Error!!"));
CratePersistentEntitySchemaManager manager = new CratePersistentEntitySchemaManager(crateOperations, UPDATE);
manager.afterPropertiesSet();
}