Search in sources :

Example 6 with JdbcTemplate

use of cn.taketoday.jdbc.core.JdbcTemplate in project today-infrastructure by TAKETODAY.

the class JdbcNamespaceIntegrationTests method createAndDestroyNestedWithH2.

@Test
void createAndDestroyNestedWithH2() throws Exception {
    try (ClassPathXmlApplicationContext context = context("jdbc-destroy-nested-config-h2.xml")) {
        DataSource dataSource = context.getBean(DataSource.class);
        JdbcTemplate template = new JdbcTemplate(dataSource);
        assertNumRowsInTestTable(template, 1);
        context.getBean(EmbeddedDatabaseFactoryBean.class).destroy();
        // Table has been dropped
        assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy(() -> assertNumRowsInTestTable(template, 1));
    }
}
Also used : BadSqlGrammarException(cn.taketoday.jdbc.BadSqlGrammarException) ClassPathXmlApplicationContext(cn.taketoday.context.support.ClassPathXmlApplicationContext) JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate) EmbeddedDatabaseFactoryBean(cn.taketoday.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean) AbstractDriverBasedDataSource(cn.taketoday.jdbc.datasource.AbstractDriverBasedDataSource) DataSource(javax.sql.DataSource) Test(org.junit.jupiter.api.Test)

Example 7 with JdbcTemplate

use of cn.taketoday.jdbc.core.JdbcTemplate in project today-infrastructure by TAKETODAY.

the class JdbcDataAccessObjectSupport method getExceptionTranslator.

/**
 * Return the SQLExceptionTranslator of this DAO's JdbcTemplate,
 * for translating SQLExceptions in custom JDBC access code.
 *
 * @see cn.taketoday.jdbc.core.JdbcTemplate#getExceptionTranslator()
 */
protected final SQLExceptionTranslator getExceptionTranslator() {
    JdbcTemplate jdbcTemplate = getJdbcTemplate();
    Assert.state(jdbcTemplate != null, "No JdbcTemplate set");
    return jdbcTemplate.getExceptionTranslator();
}
Also used : JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate)

Example 8 with JdbcTemplate

use of cn.taketoday.jdbc.core.JdbcTemplate in project today-infrastructure by TAKETODAY.

the class JdbcTemplateConfiguration method jdbcTemplate.

@Bean
@Primary
JdbcTemplate jdbcTemplate(DataSource dataSource, JdbcProperties properties) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    JdbcProperties.Template template = properties.getTemplate();
    jdbcTemplate.setFetchSize(template.getFetchSize());
    jdbcTemplate.setMaxRows(template.getMaxRows());
    if (template.getQueryTimeout() != null) {
        jdbcTemplate.setQueryTimeout((int) template.getQueryTimeout().getSeconds());
    }
    return jdbcTemplate;
}
Also used : JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate) NamedParameterJdbcTemplate(cn.taketoday.jdbc.core.namedparam.NamedParameterJdbcTemplate) Primary(cn.taketoday.context.annotation.Primary) Bean(cn.taketoday.context.annotation.Bean) ConditionalOnMissingBean(cn.taketoday.context.condition.ConditionalOnMissingBean)

Example 9 with JdbcTemplate

use of cn.taketoday.jdbc.core.JdbcTemplate in project today-infrastructure by TAKETODAY.

the class StoredProcedureTests method testStoredProcedureConfiguredViaJdbcTemplateWithCustomExceptionTranslator.

/**
 * Confirm no connection was used to get metadata. Does not use superclass replay
 * mechanism.
 */
@Test
public void testStoredProcedureConfiguredViaJdbcTemplateWithCustomExceptionTranslator() throws Exception {
    given(callableStatement.execute()).willReturn(false);
    given(callableStatement.getUpdateCount()).willReturn(-1);
    given(callableStatement.getObject(2)).willReturn(5);
    given(connection.prepareCall("{call " + StoredProcedureConfiguredViaJdbcTemplate.SQL + "(?, ?)}")).willReturn(callableStatement);
    class TestJdbcTemplate extends JdbcTemplate {

        int calls;

        @Override
        public Map<String, Object> call(CallableStatementCreator csc, List<SqlParameter> declaredParameters) throws DataAccessException {
            calls++;
            return super.call(csc, declaredParameters);
        }
    }
    TestJdbcTemplate t = new TestJdbcTemplate();
    t.setDataSource(dataSource);
    // Will fail without the following, because we're not able to get a connection
    // from the DataSource here if we need to create an ExceptionTranslator
    t.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
    StoredProcedureConfiguredViaJdbcTemplate sp = new StoredProcedureConfiguredViaJdbcTemplate(t);
    assertThat(sp.execute(11)).isEqualTo(5);
    assertThat(t.calls).isEqualTo(1);
    verify(callableStatement).setObject(1, 11, Types.INTEGER);
    verify(callableStatement).registerOutParameter(2, Types.INTEGER);
}
Also used : CallableStatementCreator(cn.taketoday.jdbc.core.CallableStatementCreator) List(java.util.List) SQLStateSQLExceptionTranslator(cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator) JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate) Test(org.junit.jupiter.api.Test)

Example 10 with JdbcTemplate

use of cn.taketoday.jdbc.core.JdbcTemplate in project today-infrastructure by TAKETODAY.

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();
    assertThat(res.size()).as("incorrect number of returns").isEqualTo(0);
}
Also used : JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate) Test(org.junit.jupiter.api.Test)

Aggregations

JdbcTemplate (cn.taketoday.jdbc.core.JdbcTemplate)52 Test (org.junit.jupiter.api.Test)34 DataSource (javax.sql.DataSource)18 AbstractDriverBasedDataSource (cn.taketoday.jdbc.datasource.AbstractDriverBasedDataSource)10 ClassPathXmlApplicationContext (cn.taketoday.context.support.ClassPathXmlApplicationContext)8 BadSqlGrammarException (cn.taketoday.jdbc.BadSqlGrammarException)6 ConfigurableApplicationContext (cn.taketoday.context.ConfigurableApplicationContext)4 EmbeddedDatabaseFactoryBean (cn.taketoday.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean)4 Transactional (cn.taketoday.transaction.annotation.Transactional)4 List (java.util.List)4 Autowired (cn.taketoday.beans.factory.annotation.Autowired)2 Bean (cn.taketoday.context.annotation.Bean)2 Primary (cn.taketoday.context.annotation.Primary)2 ConditionalOnMissingBean (cn.taketoday.context.condition.ConditionalOnMissingBean)2 ClassRelativeResourceLoader (cn.taketoday.core.io.ClassRelativeResourceLoader)2 CallableStatementCreator (cn.taketoday.jdbc.core.CallableStatementCreator)2 SqlReturnResultSet (cn.taketoday.jdbc.core.SqlReturnResultSet)2 NamedParameterJdbcTemplate (cn.taketoday.jdbc.core.namedparam.NamedParameterJdbcTemplate)2 DriverManagerDataSource (cn.taketoday.jdbc.datasource.DriverManagerDataSource)2 SimpleDriverDataSource (cn.taketoday.jdbc.datasource.SimpleDriverDataSource)2