Java Code Examples for org.springframework.dao.DataAccessException#getCause()
The following examples show how to use
org.springframework.dao.DataAccessException#getCause() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DBLogger.java From spring-basics-course-project with MIT License | 6 votes |
private void createDBSchema() { try { jdbcTemplate.update("CREATE SCHEMA " + schema); } catch (DataAccessException e) { Throwable causeException = e.getCause(); if (causeException instanceof SQLException) { SQLException sqlException = (SQLException) causeException; if (sqlException.getSQLState().equals(SQL_ERROR_STATE_SCHEMA_EXISTS)) { System.out.println("Schema already exists"); } else { throw e; } } else { throw e; } } }
Example 2
Source File: DBLogger.java From spring-basics-course-project with MIT License | 6 votes |
private void createTableIfNotExists() { try { jdbcTemplate.update("CREATE TABLE t_event (" + "id INT NOT NULL PRIMARY KEY," + "date TIMESTAMP," + "msg VARCHAR(255)" + ")"); System.out.println("Created table t_event"); } catch (DataAccessException e) { Throwable causeException = e.getCause(); if (causeException instanceof SQLException) { SQLException sqlException = (SQLException) causeException; if (sqlException.getSQLState().equals(SQL_ERROR_STATE_TABLE_EXISTS)) { System.out.println("Table already exists"); } else { throw e; } } else { throw e; } } }
Example 3
Source File: GlobalExceptionHandler.java From bootshiro with MIT License | 5 votes |
/** * description 拦截操作数据库异常 * * @param e 1 * @return com.usthe.bootshiro.domain.vo.Message */ @ExceptionHandler(DataAccessException.class) @ResponseStatus(HttpStatus.OK) public Message sqlException(DataAccessException e) { LOGGER.error("数据操作异常:",e); final Throwable cause = e.getCause(); // 之后判断cause类型进一步记录日志处理 if (cause instanceof MySQLIntegrityConstraintViolationException ) { return new Message().error(1111, "数据冲突操作失败"); } return new Message().error(1111, "服务器开小差"); }
Example 4
Source File: PostgreSqlExceptionTranslator.java From molgenis with GNU Lesser General Public License v3.0 | 5 votes |
private DataAccessException doTranslate( Throwable sourceThrowable, DataAccessException dataAccessException) { DataAccessException translatedException; Throwable cause = dataAccessException.getCause(); if (cause instanceof PSQLException) { translatedException = doTranslate(sourceThrowable, (PSQLException) cause); } else { translatedException = null; } return translatedException; }
Example 5
Source File: JdbcServiceImpl.java From herd with Apache License 2.0 | 4 votes |
/** * Executes a single statement using the given JDBC template. The given statement will be updated with the result and status. * * @param jdbcTemplate the JDBC template * @param jdbcStatement the JDBC statement to execute * @param variables the mapping of variables * @param jdbcStatementIndex the index of the statement */ private void executeStatement(JdbcTemplate jdbcTemplate, JdbcStatement jdbcStatement, Map<String, Object> variables, int jdbcStatementIndex) { // This is the exception to be set as the error message in the response Throwable exception = null; try { String sql = evaluate(jdbcStatement.getSql(), variables, "jdbc statement sql"); validateSqlStatement(sql, jdbcStatementIndex); // Process UPDATE type statements if (JdbcStatementType.UPDATE.equals(jdbcStatement.getType())) { int result = jdbcDao.update(jdbcTemplate, sql); jdbcStatement.setStatus(JdbcStatementStatus.SUCCESS); jdbcStatement.setResult(String.valueOf(result)); } // Process QUERY type statements else if (JdbcStatementType.QUERY.equals(jdbcStatement.getType())) { Integer maxResults = configurationHelper.getProperty(ConfigurationValue.JDBC_RESULT_MAX_ROWS, Integer.class); JdbcStatementResultSet jdbcStatementResultSet = jdbcDao.query(jdbcTemplate, sql, maxResults); jdbcStatement.setStatus(JdbcStatementStatus.SUCCESS); jdbcStatement.setResultSet(jdbcStatementResultSet); } // Any other statement types are unrecognized. This case should not be possible unless developer error. else { throw new IllegalStateException("Unsupported JDBC statement type '" + jdbcStatement.getType() + "'"); } } catch (CannotGetJdbcConnectionException cannotGetJdbcConnectionException) { /* * When the statement fails to execute due to connection errors. This usually indicates that the connection information which was specified is * wrong, or there is a network issue. Either way, it would indicate user error. * We get the wrapped exception and throw again as an IllegalArgumentException. */ Throwable causeThrowable = cannotGetJdbcConnectionException.getCause(); throw new IllegalArgumentException(String.valueOf(causeThrowable).trim(), cannotGetJdbcConnectionException); } catch (DataAccessException dataAccessException) { // DataAccessException's cause is a SQLException which is thrown by driver // We will use the SQLException message result exception = dataAccessException.getCause(); } // If there was an error if (exception != null) { // Set status to error and result as message jdbcStatement.setStatus(JdbcStatementStatus.ERROR); jdbcStatement.setErrorMessage(maskSensitiveInformation(exception, variables)); } }