Java Code Examples for org.apache.ibatis.jdbc.RuntimeSqlException

The following examples show how to use org.apache.ibatis.jdbc.RuntimeSqlException. These examples are extracted from open source projects. 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 Project: taskana   Source File: SampleDataGenerator.java    License: Apache License 2.0 6 votes vote down vote up
private void runScripts(Consumer<ScriptRunner> consumer) {
  try (Connection connection = dataSource.getConnection()) {
    if (LOGGER.isTraceEnabled()) {
      LOGGER.trace(connection.getMetaData().toString());
    }

    StringWriter outWriter = new StringWriter();
    StringWriter errorWriter = new StringWriter();

    ScriptRunner runner = getScriptRunner(connection, outWriter, errorWriter);
    consumer.accept(runner);

    if (LOGGER.isTraceEnabled()) {
      LOGGER.trace(outWriter.toString());
      String trimmedErrorString = errorWriter.toString().trim();
      if (!trimmedErrorString.isEmpty()) {
        LOGGER.error(trimmedErrorString);
      }
    }
  } catch (SQLException e) {
    throw new RuntimeSqlException("Failed to execute script.", e);
  }
}
 
Example 2
Source Project: taskana   Source File: DbSchemaCreator.java    License: Apache License 2.0 6 votes vote down vote up
private boolean isSchemaPreexisting(Connection connection) {
  ScriptRunner runner = getScriptRunnerInstance(connection);
  StringWriter errorWriter = new StringWriter();
  runner.setErrorLogWriter(new PrintWriter(errorWriter));
  try {
    String scriptPath =
        selectDbSchemaDetectionScript(connection.getMetaData().getDatabaseProductName());
    InputStream resourceAsStream = DbSchemaCreator.class.getResourceAsStream(scriptPath);
    BufferedReader reader =
        new BufferedReader(new InputStreamReader(resourceAsStream, StandardCharsets.UTF_8));
    runner.runScript(getSqlSchemaNameParsed(reader));
  } catch (RuntimeSqlException | SQLException e) {
    LOGGER.debug("Schema does not exist.");
    if (!errorWriter.toString().trim().isEmpty()) {
      LOGGER.debug(errorWriter.toString());
    }
    return false;
  }
  LOGGER.debug("Schema does exist.");
  return true;
}
 
Example 3
private void createDatabaseTables(Connection conn, Statement statement) throws SQLException, IOException {
    String createDbScriptPath = getCreateDBScriptPath();
    // Database does not exist
    logger.info("Database tables do not exist.");
    logger.info("Creating database tables from script " + createDbScriptPath);
    ScriptRunner sr = new ScriptRunner(conn);

    sr.setDelimiter(delimiter);
    sr.setStopOnError(true);
    sr.setLogWriter(null);
    InputStream is = getClass().getClassLoader().getResourceAsStream(createDbScriptPath);
    String scriptContent = IOUtils.toString(is);
    Reader reader = new StringReader(
            scriptContent.replaceAll(CRAFTER_SCHEMA_NAME, studioConfiguration.getProperty(DB_SCHEMA)));
    try {
        sr.runScript(reader);

        if (isRandomAdminPasswordEnabled()) {
            setRandomAdminPassword(conn, statement);
        }

        integrityValidator.store(conn);
    } catch (RuntimeSqlException e) {
        logger.error("Error while running create DB script", e);
    }
}
 
Example 4
private void createSchema(Connection conn) throws IOException {
    String createSchemaScriptPath = getCreateSchemaScriptPath();
    // Database does not exist
    logger.info("Database schema does not exists.");
    logger.info("Creating database schema from script " + createSchemaScriptPath);
    ScriptRunner sr = new ScriptRunner(conn);

    sr.setDelimiter(delimiter);
    sr.setStopOnError(true);
    sr.setLogWriter(null);
    InputStream is = getClass().getClassLoader().getResourceAsStream(createSchemaScriptPath);
    String scriptContent = IOUtils.toString(is);
    Reader reader = new StringReader(
            scriptContent.replaceAll(CRAFTER_SCHEMA_NAME, studioConfiguration.getProperty(DB_SCHEMA)));
    try {
        sr.runScript(reader);
    } catch (RuntimeSqlException e) {
        logger.error("Error while running create DB script", e);
    }
}
 
Example 5
Source Project: taskana   Source File: SampleDataGenerator.java    License: Apache License 2.0 5 votes vote down vote up
boolean tableExists(String table) {
  try (Connection connection = dataSource.getConnection()) {
    connection.setSchema(schema);
    SqlRunner runner = new SqlRunner(connection);
    String tableSafe = SqlReplacer.getSanitizedTableName(table);
    String query = "SELECT 1 FROM " + tableSafe + " LIMIT 1;";
    runner.run(query);
    return true;
  } catch (RuntimeSqlException | SQLException e) {
    return false;
  }
}
 
Example 6
Source Project: taskana   Source File: SampleDataGenerator.java    License: Apache License 2.0 5 votes vote down vote up
private List<String> parseScripts(Stream<String> scripts) {
  try (Connection connection = dataSource.getConnection()) {
    String dbProductName = connection.getMetaData().getDatabaseProductName();
    return scripts
        .map(script -> SqlReplacer.getScriptAsSql(dbProductName, now, script))
        .collect(Collectors.toList());
  } catch (SQLException e) {
    throw new RuntimeSqlException("Connection to database failed.", e);
  }
}
 
Example 7
Source Project: taskana   Source File: DbSchemaCreator.java    License: Apache License 2.0 5 votes vote down vote up
public boolean isValidSchemaVersion(String expectedVersion) {
  try (Connection connection = dataSource.getConnection()) {
    connection.setSchema(this.schemaName);
    SqlRunner runner = new SqlRunner(connection);
    LOGGER.debug(connection.getMetaData().toString());

    String query =
        "select VERSION from TASKANA_SCHEMA_VERSION where "
            + "VERSION = (select max(VERSION) from TASKANA_SCHEMA_VERSION) "
            + "AND VERSION = ?";

    Map<String, Object> queryResult = runner.selectOne(query, expectedVersion);
    if (queryResult == null || queryResult.isEmpty()) {
      LOGGER.error(
          "Schema version not valid. The VERSION property in table TASKANA_SCHEMA_VERSION "
              + "has not the expected value {}",
          expectedVersion);
      return false;
    } else {
      LOGGER.debug("Schema version is valid.");
      return true;
    }

  } catch (RuntimeSqlException | SQLException e) {
    LOGGER.error(
        "Schema version not valid. The VERSION property in table TASKANA_SCHEMA_VERSION "
            + "has not the expected value {}",
        expectedVersion);
    return false;
  }
}
 
Example 8
Source Project: gossip   Source File: GossipDataSourceProvider.java    License: MIT License 5 votes vote down vote up
private void executeSQL(Connection connection, Reader reader) throws SQLException {
    ScriptRunner runner = new ScriptRunner(connection);
    runner.setAutoCommit(true);
    runner.setStopOnError(true);

    try {
        runner.runScript(reader);
    } catch (RuntimeSqlException e) {
        throw new SQLException(e); // Force to handle the exception by myself
    }
}
 
Example 9
Source Project: camunda-bpm-platform   Source File: HistoryByteArrayTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testHistoricExternalTaskJobLogStacktraceBinary() {
  // given
  testRule.deploy("org/camunda/bpm/engine/test/api/externaltask/oneExternalTaskProcess.bpmn20.xml");
  runtimeService.startProcessInstanceByKey("oneExternalTaskProcess");

  List<LockedExternalTask> tasks = externalTaskService.fetchAndLock(5, WORKER_ID)
      .topic(TOPIC_NAME, LOCK_TIME)
      .execute();

  LockedExternalTask task = tasks.get(0);

  // submitting a failure (after a simulated processing time of three seconds)
  ClockUtil.setCurrentTime(nowPlus(3000L));

  String errorMessage;
  String exceptionStackTrace;
  try {
    throw new RuntimeSqlException("test cause");
  } catch (RuntimeException e) {
    exceptionStackTrace = ExceptionUtils.getStackTrace(e);
    errorMessage = e.getMessage();
  }
  assertNotNull(exceptionStackTrace);

  externalTaskService.handleFailure(task.getId(), WORKER_ID, errorMessage, exceptionStackTrace, 5, 3000L);

  HistoricExternalTaskLogEntity entity = (HistoricExternalTaskLogEntity) historyService.createHistoricExternalTaskLogQuery().errorMessage(errorMessage).singleResult();
  assertNotNull(entity);

  ByteArrayEntity byteArrayEntity = configuration.getCommandExecutorTxRequired().execute(new GetByteArrayCommand(entity.getErrorDetailsByteArrayId()));

  // then
  checkBinary(byteArrayEntity);
}
 
Example 10
Source Project: camunda-bpm-platform   Source File: RuntimeByteArrayTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testExternalTaskStacktraceBinary() {
  // given
  testRule.deploy("org/camunda/bpm/engine/test/api/externaltask/oneExternalTaskProcess.bpmn20.xml");
  runtimeService.startProcessInstanceByKey("oneExternalTaskProcess");

  List<LockedExternalTask> tasks = externalTaskService.fetchAndLock(5, WORKER_ID)
      .topic(TOPIC_NAME, LOCK_TIME)
      .execute();

  LockedExternalTask task = tasks.get(0);

  // submitting a failure (after a simulated processing time of three seconds)
  ClockUtil.setCurrentTime(nowPlus(3000L));

  String errorMessage;
  String exceptionStackTrace;
  try {
    throw new RuntimeSqlException("test cause");
  } catch (RuntimeException e) {
    exceptionStackTrace = ExceptionUtils.getStackTrace(e);
    errorMessage = e.getMessage();
  }
  assertNotNull(exceptionStackTrace);

  externalTaskService.handleFailure(task.getId(), WORKER_ID, errorMessage, exceptionStackTrace, 5, 3000L);

  ExternalTaskEntity externalTask = (ExternalTaskEntity) externalTaskService.createExternalTaskQuery().singleResult();

  ByteArrayEntity byteArrayEntity = configuration.getCommandExecutorTxRequired().execute(new GetByteArrayCommand(externalTask.getErrorDetailsByteArrayId()));

  // then
  checkBinary(byteArrayEntity);
}