我打算使用LiquiBase来管理数据库,我希望每次将Web应用程序部署到Glassfish 3.1时都要运行它。为了做到这一点,我写了下面的课:
@Singleton
@Startup
public class LiquibaseWrapper {
@Resource(name = "jdbc/mydatabase")
private DataSource dataSource;
@PostConstruct
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void doLiquibaseUpdate() {
ResourceAccessor fileOpener = new ClassLoaderResourceAccessor();
DatabaseFactory databaseFactory = DatabaseFactory.getInstance();
Database database = null;
try {
database = databaseFactory
.findCorrectDatabaseImplementation(new Jdbc Connection(
dataSource.getConnection()));
Liquibase liquibase = new Liquibase("ChangeLog.xml",
fileOpener, database);
liquibase.update("");
} catch (DatabaseException e) {
// TODO: handle error
} catch (SQLException e) {
// TODO: handle error
} catch (LiquibaseException e) {
// TODO: handle error
}
}
}
问题在于,当我通过Eclipse将应用程序部署到服务器上时,不会执行doLiquiBaseUpdate()。当我从中构建WAR文件并手动部署它时,将执行该方法。
我有点困惑为什么在通过eclipse进行部署时不会运行LiquiBaseUpdate()。
其他可能有用的信息: - 正在使用Maven 2构建WAR - 使用JSF 2.1 - 数据库是MySQL - 使用LiquiBase 2.0.3 - Eclipse Indigo with Glassfish服务器工具1.7.3