KEYCLOAK-2529 More proper handling of DB errors during migration

This commit is contained in:
mposolda 2016-03-07 15:19:03 +01:00 committed by Bill Burke
parent 062c8a06ab
commit f8ec178fae
3 changed files with 18 additions and 4 deletions

View file

@ -184,13 +184,24 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide
emf = Persistence.createEntityManagerFactory(unitName, properties); emf = Persistence.createEntityManagerFactory(unitName, properties);
logger.trace("EntityManagerFactory created"); logger.trace("EntityManagerFactory created");
} catch (Exception e) {
// Safe rollback
if (connection != null) {
try {
connection.rollback();
} catch (SQLException e2) {
logger.warn("Can't rollback connection", e2);
}
}
throw e;
} finally { } finally {
// Close after creating EntityManagerFactory to prevent in-mem databases from closing // Close after creating EntityManagerFactory to prevent in-mem databases from closing
if (connection != null) { if (connection != null) {
try { try {
connection.close(); connection.close();
} catch (SQLException e) { } catch (SQLException e) {
logger.warn(e); logger.warn("Can't close connection", e);
} }
} }
} }

View file

@ -93,9 +93,6 @@ public class DefaultLiquibaseConnectionProvider implements LiquibaseConnectionPr
// Change command for creating lock and drop DELETE lock record from it // Change command for creating lock and drop DELETE lock record from it
SqlGeneratorFactory.getInstance().register(new CustomInsertLockRecordGenerator()); SqlGeneratorFactory.getInstance().register(new CustomInsertLockRecordGenerator());
// We wrap liquibase update in CustomLockService provided by DBLockProvider. No need to lock inside liquibase itself.
LockServiceFactory.getInstance().register(new DummyLockService());
} }
@ -127,6 +124,11 @@ public class DefaultLiquibaseConnectionProvider implements LiquibaseConnectionPr
String changelog = (database instanceof DB2Database) ? LiquibaseJpaUpdaterProvider.DB2_CHANGELOG : LiquibaseJpaUpdaterProvider.CHANGELOG; String changelog = (database instanceof DB2Database) ? LiquibaseJpaUpdaterProvider.DB2_CHANGELOG : LiquibaseJpaUpdaterProvider.CHANGELOG;
logger.debugf("Using changelog file: %s", changelog); logger.debugf("Using changelog file: %s", changelog);
// We wrap liquibase update in CustomLockService provided by DBLockProvider. No need to lock inside liquibase itself.
// NOTE: This can't be done in baseLiquibaseInitialization() as liquibase always restarts lock service
LockServiceFactory.getInstance().register(new DummyLockService());
return new Liquibase(changelog, new ClassLoaderResourceAccessor(getClass().getClassLoader()), database); return new Liquibase(changelog, new ClassLoaderResourceAccessor(getClass().getClassLoader()), database);
} }

View file

@ -166,6 +166,7 @@ public class KeycloakApplication extends Application {
} catch (Exception e) { } catch (Exception e) {
session.getTransaction().rollback(); session.getTransaction().rollback();
logger.migrationFailure(e); logger.migrationFailure(e);
throw e;
} finally { } finally {
session.close(); session.close();
} }