} catch (Exception e) {
/*try {
conn.rollback();
}
catch(SQLException ex) {}*/
throw new DotDataException(e.getMessage(), e);
}
finally {
try {
conn.close();
}
catch(SQLException ex) {
throw new DotDataException(ex.getMessage(), ex);
}
}
List<String> schemaList = new ArrayList<String>();
//Execute the SQL Script in accordance with the database type
if(DbConnectionFactory.isPostgres()){
schemaList = SQLUtil.tokenize(getPostgresScript());
}else if(DbConnectionFactory.isMySql()){
schemaList = SQLUtil.tokenize(getMySQLScript());
}else if(DbConnectionFactory.isOracle()){
schemaList = SQLUtil.tokenize(getOracleScript());
}else if(DbConnectionFactory.isMsSql()) {
schemaList = SQLUtil.tokenize(getMSSQLScript());
}else {
schemaList = SQLUtil.tokenize(getH2Script());
}
try {
conn = DbConnectionFactory.getDataSource().getConnection();
conn.setAutoCommit(false);
if(DbConnectionFactory.isMySql()){
dc.executeStatement("SET storage_engine=INNODB", conn);
}else if(DbConnectionFactory.isMsSql()){
dc.executeStatement("SET TRANSACTION ISOLATION LEVEL READ COMMITTED;", conn);
}
for (String query : schemaList) {
if(!runInSingleTransaction){
try {
HibernateUtil.startTransaction();
dc.executeStatement(query);
} catch (Exception e) {
Logger.error(this, "Unable to execute query : " + query);
HibernateUtil.rollbackTransaction();
continue;
}
HibernateUtil.commitTransaction();
} else {
try {
dc.executeStatement(query, conn);
} catch (SQLException e) {
Logger.fatal(this, "Unable to execute SQL upgrade", e);
throw new DotDataException(e.getMessage(), e);
}
}
}
conn.commit();
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
throw new DotDataException(e1.getMessage(), e1);
}
Logger.fatal(this, "Unable to execute SQL upgrade", e);
throw new DotDataException(e.getMessage(), e);
}
finally {
try {
conn.close();
} catch (SQLException e) {
throw new DotDataException(e.getMessage(), e);
}
}
try {
conn = DbConnectionFactory.getDataSource().getConnection();
conn.setAutoCommit(true);
if (foreignKeys!=null && rebuildForeignKeys) {
for (ForeignKey key:foreignKeys) {
try {
createConstraint(conn, key);
} catch (SQLException e) {
Logger.error(AbstractJDBCStartupTask.class,"SQLException: " +e.getMessage());
}
}
}
if (indexes!=null && rebuildIndices) {
idxfor: for (Index index:indexes) {
try {
for (PrimaryKey pk:primaryKeys) {
if(index.tableName.equalsIgnoreCase(pk.tableName) && index.indexName.equalsIgnoreCase(pk.keyName)) {
continue idxfor;
}
}
createIndex(conn, index);
} catch (SQLException e) {
Logger.warn(this, "can't create index on table "+index.tableName+" columns "+getColumnList(index.columnNames)+" message "+e.getMessage());
}
}
}
if (primaryKeys!=null && rebuildPrimaryKeys) {
for (PrimaryKey key:primaryKeys) {
try {
createPrimaryKey(conn, key);
} catch (SQLException e) {
Logger.warn(this, "can't create primary key on table "+key.tableName+" columns "+getColumnList(key.columnNames)+" message "+e.getMessage());
}
}
}
} catch (SQLException e) {
Logger.fatal(this, "Unable to execute SQL upgrade", e);
throw new DotDataException(e.getMessage(), e);
}
finally {
try {
conn.close();
} catch (SQLException e) {
throw new DotDataException(e.getMessage(), e);
}
}
}