Search in sources :

Example 6 with JdbcOperations

use of org.springframework.jdbc.core.JdbcOperations in project spring-boot by spring-projects.

the class DataSourceInitializerTests method testDataSourceInitializedWithExplicitScript.

@Test
public void testDataSourceInitializedWithExplicitScript() throws Exception {
    this.context.register(DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.initialize:true", "spring.datasource.schema:" + ClassUtils.addResourcePathToPackagePath(getClass(), "schema.sql"), "spring.datasource.data:" + ClassUtils.addResourcePathToPackagePath(getClass(), "data.sql"));
    this.context.refresh();
    DataSource dataSource = this.context.getBean(DataSource.class);
    assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue();
    assertThat(dataSource).isNotNull();
    JdbcOperations template = new JdbcTemplate(dataSource);
    assertThat(template.queryForObject("SELECT COUNT(*) from FOO", Integer.class)).isEqualTo(1);
}
Also used : JdbcOperations(org.springframework.jdbc.core.JdbcOperations) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 7 with JdbcOperations

use of org.springframework.jdbc.core.JdbcOperations 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)

Aggregations

JdbcOperations (org.springframework.jdbc.core.JdbcOperations)7 DataSource (javax.sql.DataSource)6 Test (org.junit.Test)6 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)6 SQLException (java.sql.SQLException)2 Connection (java.sql.Connection)1 HibernateConfiguration (org.apereo.portal.hibernate.DelegatingHibernateIntegrator.HibernateConfiguration)1 Dialect (org.hibernate.dialect.Dialect)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 DefaultResourceLoader (org.springframework.core.io.DefaultResourceLoader)1 DataAccessException (org.springframework.dao.DataAccessException)1 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)1