Search in sources :

Example 16 with JdbcTemplate

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

the class BatchAutoConfigurationTests method testDefaultContext.

@Test
public void testDefaultContext() throws Exception {
    this.context = new AnnotationConfigApplicationContext();
    this.context.register(TestConfiguration.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, TransactionAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
    assertThat(this.context.getBean(JobExplorer.class)).isNotNull();
    assertThat(this.context.getBean(BatchProperties.class).getInitializer().isEnabled()).isTrue();
    assertThat(new JdbcTemplate(this.context.getBean(DataSource.class)).queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Example 17 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate in project cachecloud by sohutv.

the class CleanUpStatisticsJob method action.

@Override
public void action(JobExecutionContext context) {
    if (!ConstUtils.WHETHER_SCHEDULE_CLEAN_DATA) {
        return;
    }
    try {
        SchedulerContext schedulerContext = context.getScheduler().getContext();
        ApplicationContext applicationContext = (ApplicationContext) schedulerContext.get(APPLICATION_CONTEXT_KEY);
        InstanceStatsCenter InstanceStatsCenter = applicationContext.getBean("instanceStatsCenter", InstanceStatsCenter.class);
        try {
            InstanceStatsCenter.cleanUpStandardStats(5);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -31);
        Date time = calendar.getTime();
        int cleanCount = jdbcTemplate.update(CLEAN_APP_HOUR_COMMAND_STATISTICS, time);
        logger.warn("clean_app_hour_command_statistics count={}", cleanCount);
        cleanCount = jdbcTemplate.update(CLEAN_APP_MINUTE_COMMAND_STATISTICS, time);
        logger.warn("clean_app_minute_command_statistics count={}", cleanCount);
        cleanCount = jdbcTemplate.update(CLEAN_APP_HOUR_STATISTICS, time);
        logger.warn("clean_app_hour_statistics count={}", cleanCount);
        cleanCount = jdbcTemplate.update(CLEAN_APP_MINUTE_STATISTICS, time);
        logger.warn("clean_app_minute_statistics count={}", cleanCount);
        //清除客户端耗时数据(保存2天)
        ClientReportCostDistriService clientReportCostDistriService = applicationContext.getBean("clientReportCostDistriService", ClientReportCostDistriService.class);
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -2);
        long timeFormat = NumberUtils.toLong(new SimpleDateFormat("yyyyMMddHHmm00").format(calendar.getTime()));
        cleanCount = clientReportCostDistriService.deleteBeforeCollectTime(timeFormat);
        logger.warn("clean_app_client_costtime_minute_stat count={}", cleanCount);
        //清除客户端耗时汇总数据(保存14天)
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -14);
        timeFormat = NumberUtils.toLong(new SimpleDateFormat("yyyyMMddHHmm00").format(calendar.getTime()));
        cleanCount = jdbcTemplate.update(CLEAN_APP_CLIENT_MINUTE_COST_TOTAL, timeFormat);
        logger.warn("clean_app_client_costtime_minute_stat_total count={}", cleanCount);
        //清除客户端值数据(保存2天)
        ClientReportValueDistriService clientReportValueDistriService = applicationContext.getBean("clientReportValueDistriService", ClientReportValueDistriService.class);
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -2);
        timeFormat = NumberUtils.toLong(new SimpleDateFormat("yyyyMMddHHmm00").format(calendar.getTime()));
        cleanCount = clientReportValueDistriService.deleteBeforeCollectTime(timeFormat);
        logger.warn("clean_app_client_value_minute_stats count={}", cleanCount);
        //清除服务器统计数据
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        String date = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        cleanCount = jdbcTemplate.update(CLEAN_SERVER_STAT_STATISTICS, date);
        logger.warn("clean_server_stat_total count={}", cleanCount);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : Calendar(java.util.Calendar) ClientReportCostDistriService(com.sohu.cache.client.service.ClientReportCostDistriService) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Date(java.util.Date) ApplicationContext(org.springframework.context.ApplicationContext) SchedulerContext(org.quartz.SchedulerContext) InstanceStatsCenter(com.sohu.cache.stats.instance.InstanceStatsCenter) ClientReportValueDistriService(com.sohu.cache.client.service.ClientReportValueDistriService) SimpleDateFormat(java.text.SimpleDateFormat)

Example 18 with JdbcTemplate

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

the class DataSourceHealthIndicatorTests method customQuery.

@Test
public void customQuery() {
    this.indicator.setDataSource(this.dataSource);
    new JdbcTemplate(this.dataSource).execute("CREATE TABLE FOO (id INTEGER IDENTITY PRIMARY KEY)");
    this.indicator.setQuery("SELECT COUNT(*) from FOO");
    Health health = this.indicator.health();
    System.err.println(health);
    assertThat(health.getDetails().get("database")).isNotNull();
    assertThat(health.getStatus()).isEqualTo(Status.UP);
    assertThat(health.getDetails().get("hello")).isNotNull();
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Example 19 with JdbcTemplate

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

the class HibernateJpaAutoConfigurationTests method testDataScript.

// This can't succeed because the data SQL is executed immediately after the schema
// and Hibernate hasn't initialized yet at that point
@Test(expected = BeanCreationException.class)
public void testDataScript() throws Exception {
    EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.data:classpath:/city.sql");
    setupTestConfiguration();
    this.context.refresh();
    assertThat(new JdbcTemplate(this.context.getBean(DataSource.class)).queryForObject("SELECT COUNT(*) from CITY", Integer.class)).isEqualTo(1);
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Example 20 with JdbcTemplate

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

the class DataSourceHealthIndicator method setDataSource.

/**
	 * Set the {@link DataSource} to use.
	 * @param dataSource the data source
	 */
public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
    this.jdbcTemplate = new JdbcTemplate(dataSource);
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Aggregations

JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)121 Test (org.junit.Test)45 DataSource (javax.sql.DataSource)36 Before (org.junit.Before)18 SQLException (java.sql.SQLException)11 EmbeddedDatabaseBuilder (org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder)11 BaseDbTest (com.alibaba.otter.node.etl.BaseDbTest)6 DbDialect (com.alibaba.otter.node.etl.common.db.dialect.DbDialect)6 DbMediaSource (com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource)6 Connection (java.sql.Connection)6 JdbcOperations (org.springframework.jdbc.core.JdbcOperations)6 TransactionStatus (org.springframework.transaction.TransactionStatus)6 Test (org.testng.annotations.Test)6 SqlTemplate (com.alibaba.otter.node.etl.common.db.dialect.SqlTemplate)5 PreparedStatement (java.sql.PreparedStatement)5 Table (org.apache.ddlutils.model.Table)5 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)5 DataAccessException (org.springframework.dao.DataAccessException)5 AbstractDriverBasedDataSource (org.springframework.jdbc.datasource.AbstractDriverBasedDataSource)5 TransactionCallback (org.springframework.transaction.support.TransactionCallback)5