我有以下域对象:
class Cat
{
String name;
int age;
}
以及以下语句来批量插入猫:
void insertBulkCats(Collection<Cat> cats)
{
Connection conn = getConnection();
PreparedStatement statement = new PreparedStatement();
for(Cat cat : cats)
{
statement.setString(1, cat.getName());
statement.setInt(2, cat.getAge());
statement.addBatch();
}
statement.executeBatch();
PreparedStatement mergeStatement = conn.prepareStatement(MERGE_CATS);
mergeStatement.execute();
PreparedStatement dropStatement = conn.prepareStatement(CLEAR_CATS);
dropStatement.execute();
conn.commit();
}
这是一个Oracle数据库。由于我要执行的步骤是插入所有猫,对我存档的猫进行合并,然后从插入的原始猫中删除所有记录。我担心的是,上面的这种方法并不能保证回滚或单独操作的发生。我的问题是如何保证这一切都作为一个原子操作执行?此外,我如何保证没有其他函数触及未读取的数据库(就对 Cat 表进行更新而言)?