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));
}
}
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();
}
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;
}
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);
}
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);
}
Aggregations