};
try {
runOnce = TaskLocatorUtil.getStartupRunOnceTaskClasses();
runAlways = TaskLocatorUtil.getStartupRunAlwaysTaskClasses();
} catch (Exception e) {
throw new DotRuntimeException(e.getMessage(), e);
}
Collections.sort(runOnce, comparator);
Collections.sort(runAlways, comparator);
Logger.debug(this.getClass(), "Locking db_version table");
setupSQL();
//Integer currentVersion = null;
PreparedStatement update = null;
Statement s = null;
ResultSet rs = null;
Connection conn = null;
try {
conn = DbConnectionFactory.getDataSource().getConnection();
conn.setAutoCommit(false);
s = conn.createStatement();
update = conn
.prepareStatement("INSERT INTO db_version (db_version,date_update) VALUES (?,?)");
s.execute(lock);
rs = s
.executeQuery(select);
rs.next();
Config.DB_VERSION = rs.getInt("db_version");
} catch (SQLException e) {
// Maybe the table doesn't exist?
Logger.debug(this.getClass(), "Trying to create db_version table");
try {
conn.rollback();
if(DbConnectionFactory.isMySql()){
s.execute("SET storage_engine=INNODB");
}
s.execute(create);
if(update==null) {
// looks like H2 do an early table name check
update = conn.prepareStatement("INSERT INTO db_version (db_version,date_update) VALUES (?,?)");
}
update.setInt(1, 0);
Date date = new Date(Calendar.getInstance().getTimeInMillis());
update.setDate(2, date);
update.execute();
conn.commit();
Logger
.debug(this.getClass(),
"Table db_version created. Trying to lock db_table again.");
s.execute(lock);
rs = s
.executeQuery(select);
rs.next();
Config.DB_VERSION = rs.getInt("db_version");
} catch (SQLException e2) {
Logger.fatal(this.getClass(),
"Locking of db_version table failed: "
+ e2.getMessage());
throw new DotRuntimeException(
"Locking of db_version table failed: "
+ e2.getMessage(), e2);
}
}
Logger.debug(this.getClass(), "Locking db_version succeeded");
boolean firstTimeStart = false;
String name = null;
try {
Logger.info(this, "Starting startup tasks.");
HibernateUtil.startTransaction();
for (Class<?> c : runAlways) {
name = c.getCanonicalName();
name = name.substring(name.lastIndexOf(".") + 1);
if (StartupTask.class.isAssignableFrom(c)) {
StartupTask task;
try {
task = (StartupTask) c.newInstance();
} catch (Exception e) {
throw new DotRuntimeException(e.getMessage(), e);
}
HibernateUtil.startTransaction();
if (task.forceRun()) {
HibernateUtil.commitTransaction();
HibernateUtil.startTransaction();
Logger.info(this, "Running: " + name);
task.executeUpgrade();
if(name.equals("Task00001LoadSchema")){
firstTimeStart = true;
}
} else {
Logger.info(this, "Not running: " + name);
}
HibernateUtil.commitTransaction();
}
}
Logger.info(this, "Finishing startup tasks.");
} catch (Exception e) {
HibernateUtil.rollbackTransaction();
Logger.fatal(this, "Unable to execute the upgrade task : " + name, e);
} finally {
// This will commit the changes and close the connection
HibernateUtil.closeSession();
try {
update.close();
// conn.close();
} catch(Exception exx) { /* Ignore this */ }
}
Logger.info(this, "Starting upgrade tasks.");
Logger.info(this, "Database version: " + Config.DB_VERSION);
name = null;
//HibernateUtil.startTransaction();
try {
if(runOnce.size() > 0)
ReindexThread.stopThread();
for (Class<?> c : runOnce) {
name = c.getCanonicalName();
name = name.substring(name.lastIndexOf(".") + 1);
String id = name.substring(4, 9);
try {
int taskId = Integer.parseInt(id);
if (StartupTask.class.isAssignableFrom(c)
&& taskId > Config.DB_VERSION) {
StartupTask task;
try {
task = (StartupTask) c.newInstance();
} catch (Exception e) {
throw new DotRuntimeException(e.getMessage(), e);
}
//HibernateUtil.startTransaction();
if (!firstTimeStart && task.forceRun()) {
HibernateUtil.commitTransaction();