use of org.springframework.dao.DataAccessException in project uPortal by Jasig.
the class HibernateDbLoader method process.
@Override
public void process(DbLoaderConfig configuration) throws ParserConfigurationException, SAXException, IOException {
final String scriptFile = configuration.getScriptFile();
final List<String> script;
if (scriptFile == null) {
script = null;
} else {
script = new LinkedList<String>();
}
final ITableDataProvider tableData = this.loadTables(configuration, dialect);
//Handle table drop/create
if (configuration.isDropTables() || configuration.isCreateTables()) {
//Load Table object model
final Map<String, Table> tables = tableData.getTables();
final Mapping mapping = this.configuration.buildMapping();
final String defaultCatalog = this.configuration.getProperty(Environment.DEFAULT_CATALOG);
final String defaultSchema = this.configuration.getProperty(Environment.DEFAULT_SCHEMA);
final Map<String, DataAccessException> failedSql = new LinkedHashMap<String, DataAccessException>();
//Generate and execute drop table scripts
if (configuration.isDropTables()) {
final List<String> dropScript = this.dropScript(tables.values(), dialect, defaultCatalog, defaultSchema);
if (script == null) {
this.logger.info("Dropping existing tables");
for (final String sql : dropScript) {
this.logger.info(sql);
try {
jdbcOperations.update(sql);
} catch (NonTransientDataAccessResourceException dae) {
throw dae;
} catch (DataAccessException dae) {
failedSql.put(sql, dae);
}
}
} else {
script.addAll(dropScript);
}
}
//Log any drop/create statements that failed
for (final Map.Entry<String, DataAccessException> failedSqlEntry : failedSql.entrySet()) {
this.logger.warn("'" + failedSqlEntry.getKey() + "' failed to execute due to " + failedSqlEntry.getValue());
}
//Generate and execute create table scripts
if (configuration.isCreateTables()) {
final List<String> createScript = this.createScript(tables.values(), dialect, mapping, defaultCatalog, defaultSchema);
if (script == null) {
this.logger.info("Creating tables");
for (final String sql : createScript) {
this.logger.info(sql);
jdbcOperations.update(sql);
}
} else {
script.addAll(createScript);
}
}
}
//Perform database population
if (script == null && configuration.isPopulateTables()) {
this.logger.info("Populating database");
final Map<String, Map<String, Integer>> tableColumnTypes = tableData.getTableColumnTypes();
this.populateTables(configuration, tableColumnTypes);
}
//Write out the script file
if (script != null) {
for (final ListIterator<String> iterator = script.listIterator(); iterator.hasNext(); ) {
final String sql = iterator.next();
iterator.set(sql + ";");
}
final File outputFile = new File(scriptFile);
FileUtils.writeLines(outputFile, script);
this.logger.info("Saved DDL to: " + outputFile.getAbsolutePath());
}
}
use of org.springframework.dao.DataAccessException in project uPortal by Jasig.
the class DbTestImpl method printDbInfo.
@Override
public void printDbInfo() {
boolean fail = false;
logger.info("JDBC DataSources");
for (final Entry<String, JdbcOperations> jdbcEntry : this.jdbcOperations.entrySet()) {
final String jdbcName = jdbcEntry.getKey();
try {
logger.info("\t" + jdbcName);
final JdbcOperations jdbcOps = jdbcEntry.getValue();
jdbcOps.execute(new ConnectionCallback<Object>() {
@Override
public Object doInConnection(Connection con) throws SQLException, DataAccessException {
printInfo(con);
return null;
}
});
} catch (Exception e) {
logger.error("\t" + jdbcName + ": parse info", e);
fail = true;
}
logger.info("");
}
logger.info("Hibernate Dialects");
for (final Entry<String, HibernateConfiguration> configEntry : this.hibernateConfigurations.entrySet()) {
final String persistenceUnit = configEntry.getKey();
try {
final HibernateConfiguration hibernateConfiguration = configEntry.getValue();
final SessionFactoryImplementor sessionFactory = hibernateConfiguration.getSessionFactory();
final Dialect dialect = sessionFactory.getDialect();
logger.info("\t" + persistenceUnit + ": " + dialect);
} catch (Exception e) {
logger.error("\t" + persistenceUnit + ": Failed to resolve Dialect", e);
fail = true;
}
logger.info("");
}
if (fail) {
throw new RuntimeException("One or more of the portal data sources is not configured correctly or the target database is not available.");
}
}
use of org.springframework.dao.DataAccessException in project uPortal by Jasig.
the class JdbcAuthDao method createToken.
protected void createToken(final String serviceName) {
try {
this.jdbcOperations.execute(new ConnectionCallback<Object>() {
@Override
public Object doInConnection(Connection con) throws SQLException, DataAccessException {
//This is horribly hacky but we can't rely on the main uPortal TM directly or we get
//into a circular dependency loop from JPA to Ehcache to jGroups and back to JPA
final DataSource ds = new SingleConnectionDataSource(con, true);
final PlatformTransactionManager ptm = new DataSourceTransactionManager(ds);
final TransactionOperations to = new TransactionTemplate(ptm);
to.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
logger.info("Creating jGroups auth token");
final String authToken = RandomTokenGenerator.INSTANCE.generateRandomToken(authTokenLength);
final ImmutableMap<String, String> params = ImmutableMap.of(PRM_SERVICE_NAME, serviceName, PRM_RANDOM_TOKEN, authToken);
namedParameterJdbcOperations.update(INSERT_SQL, params);
}
});
return null;
}
});
} catch (ConstraintViolationException e) {
//Ignore, just means a concurrent token creation
} catch (DataIntegrityViolationException e) {
//Ignore, just means a concurrent token creation
}
}
Aggregations