Search in sources :

Example 71 with DataAccessException

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());
    }
}
Also used : Table(org.hibernate.mapping.Table) Mapping(org.hibernate.engine.spi.Mapping) LinkedHashMap(java.util.LinkedHashMap) NonTransientDataAccessResourceException(org.springframework.dao.NonTransientDataAccessResourceException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) File(java.io.File) DataAccessException(org.springframework.dao.DataAccessException)

Example 72 with DataAccessException

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.");
    }
}
Also used : SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Connection(java.sql.Connection) JdbcOperations(org.springframework.jdbc.core.JdbcOperations) DataAccessException(org.springframework.dao.DataAccessException) SQLException(java.sql.SQLException) Dialect(org.hibernate.dialect.Dialect) HibernateConfiguration(org.apereo.portal.hibernate.DelegatingHibernateIntegrator.HibernateConfiguration) DataAccessException(org.springframework.dao.DataAccessException)

Example 73 with DataAccessException

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
    }
}
Also used : SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) TransactionOperations(org.springframework.transaction.support.TransactionOperations) SQLException(java.sql.SQLException) Connection(java.sql.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) DataSource(javax.sql.DataSource) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataAccessException(org.springframework.dao.DataAccessException) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Aggregations

DataAccessException (org.springframework.dao.DataAccessException)73 SQLException (java.sql.SQLException)40 Test (org.junit.Test)20 Connection (java.sql.Connection)17 ResultSet (java.sql.ResultSet)16 PreparedStatement (java.sql.PreparedStatement)14 TransactionStatus (org.springframework.transaction.TransactionStatus)7 HashMap (java.util.HashMap)6 DeadlockLoserDataAccessException (org.springframework.dao.DeadlockLoserDataAccessException)5 IncorrectResultSizeDataAccessException (org.springframework.dao.IncorrectResultSizeDataAccessException)5 IOException (java.io.IOException)4 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)4 ConnectionCallback (org.springframework.jdbc.core.ConnectionCallback)4 SpringSqlParams (com.opengamma.elsql.SpringSqlParams)3 DatabaseMetaData (java.sql.DatabaseMetaData)3 Statement (java.sql.Statement)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 IJoinQueryString (org.apereo.portal.jdbc.IJoinQueryString)3 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)3