org.springframework.jdbc.core.JdbcTemplate.setExceptionTranslator()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(140)

本文整理了Java中org.springframework.jdbc.core.JdbcTemplate.setExceptionTranslator()方法的一些代码示例,展示了JdbcTemplate.setExceptionTranslator()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JdbcTemplate.setExceptionTranslator()方法的具体详情如下:
包路径:org.springframework.jdbc.core.JdbcTemplate
类名称:JdbcTemplate
方法名:setExceptionTranslator

JdbcTemplate.setExceptionTranslator介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

public StoredProcedureExceptionTranslator(DataSource ds) {
  setDataSource(ds);
  setSql(SQL);
  getJdbcTemplate().setExceptionTranslator(new SQLExceptionTranslator() {
    @Override
    public DataAccessException translate(String task, @Nullable String sql, SQLException ex) {
      return new CustomDataException(sql, ex);
    }
  });
  compile();
}

代码示例来源:origin: spring-projects/spring-framework

@Before
public void setUp() throws SQLException {
  given(connection.createStatement()).willReturn(statement);
  given(connection.prepareStatement(anyString())).willReturn(preparedStatement);
  given(statement.executeQuery(anyString())).willReturn(resultSet);
  given(preparedStatement.executeQuery()).willReturn(resultSet);
  given(resultSet.next()).willReturn(true, true, false);
  given(resultSet.getString(1)).willReturn("tb1", "tb2");
  given(resultSet.getInt(2)).willReturn(1, 2);
  template.setDataSource(new SingleConnectionDataSource(connection, false));
  template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
  template.afterPropertiesSet();
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Test that we see an SQLException translated using Error Code.
 * If we provide the SQLExceptionTranslator, we shouldn't use a connection
 * to get the metadata
 */
@Test
public void testUseCustomSQLErrorCodeTranslator() throws Exception {
  // Bad SQL state
  final SQLException sqlException = new SQLException("I have a known problem", "07000", 1054);
  final String sql = "SELECT ID FROM CUSTOMER";
  given(this.resultSet.next()).willReturn(true);
  given(this.connection.createStatement()).willReturn(this.preparedStatement);
  JdbcTemplate template = new JdbcTemplate();
  template.setDataSource(this.dataSource);
  // Set custom exception translator
  template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
  template.afterPropertiesSet();
  this.thrown.expect(BadSqlGrammarException.class);
  this.thrown.expect(exceptionCause(sameInstance(sqlException)));
  try {
    template.query(sql, (RowCallbackHandler) rs -> {
      throw sqlException;
    });
  }
  finally {
    verify(this.resultSet).close();
    verify(this.preparedStatement).close();
    verify(this.connection).close();
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * If beanProperty is true, initialize via exception translator bean property;
 * if false, use afterPropertiesSet().
 */
private void doTestCouldNotGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty)
    throws SQLException {
  SQLException sqlException = new SQLException("foo", "07xxx");
  this.dataSource = mock(DataSource.class);
  given(this.dataSource.getConnection()).willThrow(sqlException);
  this.template = new JdbcTemplate();
  this.template.setDataSource(this.dataSource);
  this.template.setLazyInit(false);
  if (beanProperty) {
    // This will get a connection.
    this.template.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(this.dataSource));
  }
  else {
    // This will cause creation of default SQL translator.
    this.template.afterPropertiesSet();
  }
  RowCountCallbackHandler rcch = new RowCountCallbackHandler();
  this.thrown.expect(CannotGetJdbcConnectionException.class);
  this.thrown.expect(exceptionCause(sameInstance(sqlException)));
  this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
}

代码示例来源:origin: spring-projects/spring-framework

@SuppressWarnings("unchecked")
public Mock(MockType type) throws Exception {
  connection = mock(Connection.class);
  statement = mock(Statement.class);
  resultSet = mock(ResultSet.class);
  resultSetMetaData = mock(ResultSetMetaData.class);
  given(connection.createStatement()).willReturn(statement);
  given(statement.executeQuery(anyString())).willReturn(resultSet);
  given(resultSet.getMetaData()).willReturn(resultSetMetaData);
  given(resultSet.next()).willReturn(true, false);
  given(resultSet.getString(1)).willReturn("Bubba");
  given(resultSet.getLong(2)).willReturn(22L);
  given(resultSet.getTimestamp(3)).willReturn(new Timestamp(1221222L));
  given(resultSet.getObject(anyInt(), any(Class.class))).willThrow(new SQLFeatureNotSupportedException());
  given(resultSet.getDate(3)).willReturn(new java.sql.Date(1221222L));
  given(resultSet.getBigDecimal(4)).willReturn(new BigDecimal("1234.56"));
  given(resultSet.wasNull()).willReturn(type == MockType.TWO);
  given(resultSetMetaData.getColumnCount()).willReturn(4);
  given(resultSetMetaData.getColumnLabel(1)).willReturn(
      type == MockType.THREE ? "Last Name" : "name");
  given(resultSetMetaData.getColumnLabel(2)).willReturn("age");
  given(resultSetMetaData.getColumnLabel(3)).willReturn("birth_date");
  given(resultSetMetaData.getColumnLabel(4)).willReturn("balance");
  jdbcTemplate = new JdbcTemplate();
  jdbcTemplate.setDataSource(new SingleConnectionDataSource(connection, false));
  jdbcTemplate.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
  jdbcTemplate.afterPropertiesSet();
}

代码示例来源:origin: io.bufferslayer/bufferslayer-spring-jdbc

public void setExceptionTranslator(
  SQLExceptionTranslator exceptionTranslator) {
 delegate.setExceptionTranslator(exceptionTranslator);
}

代码示例来源:origin: io.bufferslayer/buffer-spring-jdbc

public void setExceptionTranslator(
  SQLExceptionTranslator exceptionTranslator) {
 delegate.setExceptionTranslator(exceptionTranslator);
}

代码示例来源:origin: org.molgenis/molgenis-data-postgresql

@Bean
public JdbcTemplate jdbcTemplate() {
 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
 jdbcTemplate.setExceptionTranslator(postgreSqlExceptionTranslator);
 return jdbcTemplate;
}

代码示例来源:origin: oVirt/ovirt-engine

@Produces
@Singleton
public JdbcTemplate produceJdbcTemplate(
    DataSource dataSource,
    DbEngineDialect dbEngineDialect,
    SQLExceptionTranslator sqlExceptionTranslator) {
  final JdbcTemplate jdbcTemplate = dbEngineDialect.createJdbcTemplate(dataSource);
  jdbcTemplate.setExceptionTranslator(sqlExceptionTranslator);
  return jdbcTemplate;
}

代码示例来源:origin: org.slinkyframework.environment/slinky-environment-builder-liquibase

private void createJdbcTemplate() {
  jdbcTemplate = new JdbcTemplate(dataSource);
  jdbcTemplate.setExceptionTranslator(new OracleSQLExceptionTranslator());
}

代码示例来源:origin: dCache/dcache

/**
 *  this is a utility class which is issues SQL queries on database
 *
 */
protected FsSqlDriver(DataSource dataSource) throws ChimeraFsException
{
  _jdbc = new JdbcTemplate(dataSource);
  _jdbc.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(dataSource) {
    @Override
    protected DataAccessException customTranslate(String task, String sql, SQLException sqlEx)
    {
      if (isForeignKeyError(sqlEx)) {
        return new ForeignKeyViolationException(buildMessage(task, sql, sqlEx), sqlEx);
      }
      return super.customTranslate(task, sql, sqlEx);
    }
  });
  Long root = getInumber("000000000000000000000000000000000000");
  if (root == null) {
    throw new FileNotFoundHimeraFsException("Root inode does not exist.");
  }
  _root = root;
}

代码示例来源:origin: spring-cloud/spring-cloud-dataflow

/**
   * Execute list of {@code SqlCommand} by suppressing errors if those are given
   * with a command.
   *
   * @param connection the connection
   * @param commands the sql commands
   */
  public void execute(Connection connection, List<SqlCommand> commands) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(new SingleConnectionDataSource(connection, true));
    SQLExceptionTranslator origExceptionTranslator = jdbcTemplate.getExceptionTranslator();

    for (SqlCommand command : commands) {
      if(!ObjectUtils.isEmpty(command.getSuppressedErrorCodes())) {
        jdbcTemplate.setExceptionTranslator(new SuppressSQLErrorCodesTranslator(command.getSuppressedErrorCodes()));
      }
      try {
        logger.debug("Executing command {}", command.getCommand());
        jdbcTemplate.execute(command.getCommand());
      } catch (SuppressDataAccessException e) {
        logger.debug("Suppressing error {}", e);
      }
      // restore original translator in case next command
      // doesn't define suppressing codes.
      jdbcTemplate.setExceptionTranslator(origExceptionTranslator);
    }
  }
}

代码示例来源:origin: spring-cloud/spring-cloud-skipper

/**
   * Execute list of {@code SqlCommand} by suppressing errors if those are given
   * with a command.
   *
   * @param connection the connection
   * @param commands the sql commands
   */
  public void execute(Connection connection, List<SqlCommand> commands) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(new SingleConnectionDataSource(connection, true));
    SQLExceptionTranslator origExceptionTranslator = jdbcTemplate.getExceptionTranslator();

    for (SqlCommand command : commands) {
      if (!ObjectUtils.isEmpty(command.getSuppressedErrorCodes())) {
        jdbcTemplate.setExceptionTranslator(new SuppressSQLErrorCodesTranslator(command.getSuppressedErrorCodes()));
      }
      try {
        logger.debug("executing command {}", command.getCommand());
        jdbcTemplate.execute(command.getCommand());
      }
      catch (SuppressDataAccessException e) {
        logger.debug("Suppressing error {}", e);
      }
      // restore original translator in case next command
      // doesn't define suppressing codes.
      jdbcTemplate.setExceptionTranslator(origExceptionTranslator);
    }
  }
}

相关文章