use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class FileSystemResource method loadAsString.
/**
* Loads this resource as a string.
*
* @param encoding The encoding to use.
* @return The string contents of the resource.
*/
public String loadAsString(String encoding) {
try {
InputStream inputStream = new FileInputStream(location);
Reader reader = new InputStreamReader(inputStream, Charset.forName(encoding));
return FileCopyUtils.copyToString(reader);
} catch (IOException e) {
throw new FlywayException("Unable to load filesystem resource: " + location.getPath() + " (encoding: " + encoding + ")", e);
}
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class TransactionTemplate method execute.
/**
* Executes this callback within a transaction.
*
* @param transactionCallback The callback to execute.
* @return The result of the transaction code.
*/
public <T> T execute(Callable<T> transactionCallback) {
boolean oldAutocommit = true;
try {
oldAutocommit = connection.getAutoCommit();
connection.setAutoCommit(false);
T result = transactionCallback.call();
connection.commit();
return result;
} catch (SQLException e) {
throw new FlywaySqlException("Unable to commit transaction", e);
} catch (Exception e) {
Savepoint savepoint = null;
RuntimeException rethrow;
if (e instanceof RollbackWithSavepointException) {
savepoint = ((RollbackWithSavepointException) e).getSavepoint();
rethrow = (RuntimeException) e.getCause();
} else {
if (e instanceof RuntimeException) {
rethrow = (RuntimeException) e;
} else {
rethrow = new FlywayException(e);
}
}
if (rollbackOnException) {
try {
LOG.debug("Rolling back transaction...");
if (savepoint == null) {
connection.rollback();
} else {
connection.rollback(savepoint);
}
LOG.debug("Transaction rolled back");
} catch (SQLException se) {
LOG.error("Unable to rollback transaction", se);
}
} else {
try {
connection.commit();
} catch (SQLException se) {
LOG.error("Unable to commit transaction", se);
}
}
throw rethrow;
} finally {
try {
connection.setAutoCommit(oldAutocommit);
} catch (SQLException e) {
LOG.error("Unable to restore autocommit to original value for connection", e);
}
}
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class FlywayMediumTest method repairDescription.
@Test
public void repairDescription() throws Exception {
DriverDataSource dataSource = new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, "jdbc:h2:mem:flyway_db_repair_description;DB_CLOSE_DELAY=-1", "sa", "", null, "SET AUTOCOMMIT OFF");
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource);
flyway.setLocations("migration/quote");
assertEquals(1, flyway.migrate());
// Switch out V1 for a different migration with a new description and checksum
flyway.setLocations("migration/placeholder");
try {
flyway.validate();
fail();
} catch (FlywayException e) {
//Should happen
}
flyway.repair();
assertEquals(0, flyway.migrate());
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class FlywayMediumTest method failed.
@Test
public void failed() {
StringLogCreator logCreator = new StringLogCreator();
LogFactory.setLogCreator(logCreator);
try {
Flyway flyway = new Flyway();
flyway.setDataSource("jdbc:h2:mem:flyway_failed;DB_CLOSE_DELAY=-1", "sa", "");
flyway.setLocations("migration/failed");
flyway.migrate();
fail();
} catch (FlywayException e) {
System.out.println(logCreator.getOutput());
} finally {
LogFactory.setLogCreator(null);
}
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class H2TriggerExceptionSmallTest method triggerException.
@Test
public void triggerException() {
Flyway flyway = new Flyway();
flyway.setDataSource("jdbc:h2:mem:flyway_db_trigger;DB_CLOSE_DELAY=-1", "sa", "");
flyway.setLocations("org.flywaydb.core.internal.dbsupport.h2");
try {
flyway.migrate();
fail();
} catch (FlywayException e) {
assertEquals("Expected", ExceptionUtils.getRootCause(e).getMessage());
}
}
Aggregations