HA

测试包含truncate table的方法(oracle数据库)

测试方法

  1. @BeforeEach 备份表
  2. 执行方法
  3. @AfterEach 从备份表恢复数据并删除备份表

示例代码:

{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @BeforeEach
    public void before() {
        // 备份表
        String today = LocalDate.now().format(DateTimeFormatter.ofPattern("uuMMdd"));
        template.execute(String.format("create table test_table_%s as select * from test_table", today));
    }

    @AfterEach
    public void after() {
        String today = LocalDate.now().format(DateTimeFormatter.ofPattern("uuMMdd"));
        template.execute(String.format("insert into test_table select * from test_table_%s", today));
        template.execute(String.format("drop table test_table_%s", today));
    }

    @Test
	@Transactional(readOnly = true)
	void test() {
		// 测试包含truncate的方法
        log.info("1. " + template.queryForList("select * from test_table") + "");
        template.execute("truncate table test_table");
        log.info("2. " + template.queryForList("select * from test_table") + "");
	}
}

只适用于oracle数据库, postgresql数据库的话会报错 “不能在只读事务中执行create table操作”

reference