Search in sources :

Example 96 with JdbcTemplate

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

the class AbstractDataSourcePoolMetadataTests method getPoolSizeOneConnection.

@Test
public void getPoolSizeOneConnection() {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource());
    jdbcTemplate.execute(new ConnectionCallback<Void>() {

        @Override
        public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
            assertThat(getDataSourceMetadata().getActive()).isEqualTo(Integer.valueOf(1));
            assertThat(getDataSourceMetadata().getUsage()).isEqualTo(Float.valueOf(0.5F));
            return null;
        }
    });
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 97 with JdbcTemplate

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

the class DataSourceInitializerTests method testDataSourceInitialized.

@Test
public void testDataSourceInitialized() throws Exception {
    EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.initialize:true");
    this.context.register(DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    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 BAR", 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 98 with JdbcTemplate

use of org.springframework.jdbc.core.JdbcTemplate 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 99 with JdbcTemplate

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

the class JdbcDaoSupportTests method testJdbcDaoSupportWithJdbcTemplate.

@Test
public void testJdbcDaoSupportWithJdbcTemplate() throws Exception {
    JdbcTemplate template = new JdbcTemplate();
    final List<String> test = new ArrayList<>();
    JdbcDaoSupport dao = new JdbcDaoSupport() {

        @Override
        protected void initDao() {
            test.add("test");
        }
    };
    dao.setJdbcTemplate(template);
    dao.afterPropertiesSet();
    assertEquals("Correct JdbcTemplate", dao.getJdbcTemplate(), template);
    assertEquals("initDao called", test.size(), 1);
}
Also used : ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Example 100 with JdbcTemplate

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

the class StoredProcedureTests method testStoredProcedureSkippingResultsProcessing.

@Test
public void testStoredProcedureSkippingResultsProcessing() throws Exception {
    given(callableStatement.execute()).willReturn(true);
    given(callableStatement.getUpdateCount()).willReturn(-1);
    given(connection.prepareCall("{call " + StoredProcedureWithResultSetMapped.SQL + "()}")).willReturn(callableStatement);
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    jdbcTemplate.setSkipResultsProcessing(true);
    StoredProcedureWithResultSetMapped sproc = new StoredProcedureWithResultSetMapped(jdbcTemplate);
    Map<String, Object> res = sproc.execute();
    assertEquals("incorrect number of returns", 0, res.size());
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

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