用程序释放数据库变更锁



我有一个使用java的springboot应用程序,我希望有一个释放数据库更改日志的命令行命令。

我正在考虑执行查询,或者只是使用java来调用mvnliqibase:releaseLock命令。

这些可能性可行吗?

这就是我运行迁移的方式

ConfigurableApplicationContext ctx = SpringApplication.run(MigrationsApplication.class, args);
int exitCode = SpringApplication.exit(ctx, () -> 0);
logger.info("All migrations were finished with success");
System.exit(exitCode);

您可以使用Liquibase的Java API。

java.sql.Connection connection = openConnection(); //your openConnection logic here
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
Liquibase liquibase = new liquibase.Liquibase("path/to/changelog.xml", new ClassLoaderResourceAccessor(), database);
liquibase.update(new Contexts(), new LabelExpression());

来源:https://www.liquibase.org/blog/3-ways-to-run-liquibase?_ga=2.169260328.887208231.1623768288-1838810704.1623768288

这是JavaDochttps://www.liquibase.org/javadoc/?_ga=2.169260328.887208231.1623768288-1838810704.1623768288

正如Simon在回答中提到的,您可以随时使用Liquibase Java API来实现这一点。

此外,我认为没有必要明确地释放锁。Liquibase将在更新成功后自动发布它们。

仍然只是为了以防万一,你可以参考下面的代码块:

//your openConnection logic here
java.sql.Connection connection = openConnection(); 
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
//This will access your changelog file and create liquibase object
Liquibase liquibase = new liquibase.Liquibase("path/to/changelog.xml", new ClassLoaderResourceAccessor(), database); 
// You can set contexts here to execute required changesets
final Contexts contexts = new Contexts("yourcontext");
// This will execute update on your Database with executing changesets
liquibase.update(contexts, new LabelExpression()); 
// To release locks
liquibase.forceReleaseLocks();

只是一个示例实现(可能会有所帮助(:

public void updateDb(String url, String login, String password, String diffFilePath, String driverClassName) throws Exception{
Connection c = //create connection from url/login/password/driverClassName;
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
Liquibase liquibase = new liquibase.Liquibase("path/to/changelog.xml", new ClassLoaderResourceAccessor(), database); 
try {
liquibase.update(new Contexts("contexts"));
} catch (SQLException e) {
throw new DatabaseException(e);
} finally {
if (liquibase != null) {
liquibase.forceReleaseLocks();
}
if (c != null) {
try {
c.rollback();
c.close();
} catch (SQLException e) {
//nothing to do
}
}
}
}

相关内容

  • 没有找到相关文章

最新更新