Search in sources :

Example 91 with JdbcTemplate

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

the class QuartzSupportTests method schedulerWithHsqlDataSource.

/**
	 * SPR-6038: detect HSQL and stop illegal locks being taken.
	 * TODO: Against Quartz 2.2, this test's job doesn't actually execute anymore...
	 */
@Test
public void schedulerWithHsqlDataSource() throws Exception {
    // Assume.group(TestGroup.PERFORMANCE);
    DummyJob.param = 0;
    DummyJob.count = 0;
    ClassPathXmlApplicationContext ctx = context("databasePersistence.xml");
    JdbcTemplate jdbcTemplate = new JdbcTemplate(ctx.getBean(DataSource.class));
    assertFalse("No triggers were persisted", jdbcTemplate.queryForList("SELECT * FROM qrtz_triggers").isEmpty());
/*
		Thread.sleep(3000);
		try {
			assertTrue("DummyJob should have been executed at least once.", DummyJob.count > 0);
		}
		finally {
			ctx.close();
		}
		*/
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 92 with JdbcTemplate

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

the class InferredDataSourceSqlScriptsTests method database2.

@Test
@Sql(scripts = "data-add-catbert.sql", config = @SqlConfig(transactionManager = "txMgr2"))
public void database2() {
    assertInTransaction(false);
    assertUsers(new JdbcTemplate(dataSource2), "Dilbert", "Catbert");
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Example 93 with JdbcTemplate

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

the class InferredDataSourceTransactionalSqlScriptsTests method database1.

@Test
@Transactional("txMgr1")
@Sql(scripts = "data-add-dogbert.sql", config = @SqlConfig(transactionManager = "txMgr1"))
public void database1() {
    assertInTransaction(true);
    assertUsers(new JdbcTemplate(dataSource1), "Dilbert", "Dogbert");
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 94 with JdbcTemplate

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

the class NamedParameterJdbcTemplateTests method testBatchUpdateWithSqlParameterSourcePlusTypeInfo.

@Test
public void testBatchUpdateWithSqlParameterSourcePlusTypeInfo() throws Exception {
    SqlParameterSource[] ids = new SqlParameterSource[2];
    ids[0] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC);
    ids[1] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC);
    final int[] rowsAffected = new int[] { 1, 2 };
    given(preparedStatement.executeBatch()).willReturn(rowsAffected);
    given(connection.getMetaData()).willReturn(databaseMetaData);
    JdbcTemplate template = new JdbcTemplate(dataSource, false);
    namedParameterTemplate = new NamedParameterJdbcTemplate(template);
    int[] actualRowsAffected = namedParameterTemplate.batchUpdate("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
    assertTrue("executed 2 updates", actualRowsAffected.length == 2);
    assertEquals(rowsAffected[0], actualRowsAffected[0]);
    assertEquals(rowsAffected[1], actualRowsAffected[1]);
    verify(connection).prepareStatement("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?");
    verify(preparedStatement).setObject(1, 100, Types.NUMERIC);
    verify(preparedStatement).setObject(1, 200, Types.NUMERIC);
    verify(preparedStatement, times(2)).addBatch();
    verify(preparedStatement, atLeastOnce()).close();
    verify(connection, atLeastOnce()).close();
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Example 95 with JdbcTemplate

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

the class NamedParameterJdbcTemplateTests method testBatchUpdateWithSqlParameterSource.

@Test
public void testBatchUpdateWithSqlParameterSource() throws Exception {
    SqlParameterSource[] ids = new SqlParameterSource[2];
    ids[0] = new MapSqlParameterSource("id", 100);
    ids[1] = new MapSqlParameterSource("id", 200);
    final int[] rowsAffected = new int[] { 1, 2 };
    given(preparedStatement.executeBatch()).willReturn(rowsAffected);
    given(connection.getMetaData()).willReturn(databaseMetaData);
    JdbcTemplate template = new JdbcTemplate(dataSource, false);
    namedParameterTemplate = new NamedParameterJdbcTemplate(template);
    int[] actualRowsAffected = namedParameterTemplate.batchUpdate("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
    assertTrue("executed 2 updates", actualRowsAffected.length == 2);
    assertEquals(rowsAffected[0], actualRowsAffected[0]);
    assertEquals(rowsAffected[1], actualRowsAffected[1]);
    verify(connection).prepareStatement("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?");
    verify(preparedStatement).setObject(1, 100);
    verify(preparedStatement).setObject(1, 200);
    verify(preparedStatement, times(2)).addBatch();
    verify(preparedStatement, atLeastOnce()).close();
    verify(connection, atLeastOnce()).close();
}
Also used : JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Aggregations

JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)124 Test (org.junit.Test)46 DataSource (javax.sql.DataSource)37 Before (org.junit.Before)19 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