我正在使用Hibernate。我需要获取大约1000000条记录,这将导致超时异常。所以我正在使用 setfetchsize
对于6000条记录,这样它将在多个事务中分配每条6000条记录的操作。
获取所有内容大约需要21个小时。
但同时检索记录,如果有人删除了要获取的记录之一,那么我得到 ORA-08103: object no longer exists
。
现在我想跳过检索时删除的那个对象。我怎样才能做到这一点?
我正在使用Hibernate。我需要获取大约1000000条记录,这将导致超时异常。所以我正在使用 setfetchsize
对于6000条记录,这样它将在多个事务中分配每条6000条记录的操作。
获取所有内容大约需要21个小时。
但同时检索记录,如果有人删除了要获取的记录之一,那么我得到 ORA-08103: object no longer exists
。
现在我想跳过检索时删除的那个对象。我怎样才能做到这一点?
很可能是基于使用创建的全局临时表(GTT)打开游标 ON COMMIT DELETE ROWS
选项。和事业 ORA-08103: object no longer exists
错误是 commit
紧随其后的声明 delete
声明。这是一个简单的例子:
SQL> declare
2 type t_recs is table of number;
3 l_cur sys_refcursor; -- our cursor
4 l_rec t_recs;
5
6 begin
7
8 -- populating a global temporary table GTT1 with sample data
9 insert into GTT1(col)
10 select level
11 from dual
12 connect by level <= 1000;
13
14 open l_cur -- open a cursor based on data from GTT1
15 for select col
16 from GTT1;
17
18 -- here goes delete statement
19 -- and
20 commit; <-- cause of the error. After committing all data from GTT1 will be
21 -- deleted and when we try to fetch from the cursor
22 loop -- we'll face the ORA-08103 error
23 fetch l_cur -- attempt to fetch data which are long gone.
24 bulk collect into l_rec;
25 exit when l_cur%notfound;
26 end loop;
27
28 end;
29 /
ORA-08103: object no longer exists
ORA-06512: at line 24
使用全局临时表重新创建 on commit preserve rows
子句将允许从基于该表的游标安全地获取数据,而不用担心面对 ORA-08103:
错误。
经过一周的努力,我终于解决了问题:
解决方案:很可能是基于全局临时表(GTT)打开游标,该表是使用ON COMMIT DELETE ROWS选项创建的。并且ORA-08103:对象不再存在的原因错误是紧跟在delete语句之后的commit语句。 DBA团队不同意在提交保留行上更改GTT,所以最后我在Java服务层添加了代码库[实现Spring - Programmatic Transaction]
package com.test;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
public class StudentJDBCTemplate implements StudentDAO {
private DataSource dataSource;
private JdbcTemplate jdbcTemplateObject;
private PlatformTransactionManager transactionManager;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplateObject = new JdbcTemplate(dataSource);
}
public void setTransactionManager(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public void create(String name, Integer age, Integer marks, Integer year){
TransactionDefinition def = new DefaultTransactionDefinition();
TransactionStatus status = transactionManager.getTransaction(def);
try {
String SQL1 = "insert into Student (name, age) values (?, ?)";
jdbcTemplateObject.update( SQL1, name, age);
// Get the latest student id to be used in Marks table
String SQL2 = "select max(id) from Student";
int sid = jdbcTemplateObject.queryForInt( SQL2 );
String SQL3 = "insert into Marks(sid, marks, year) " + "values (?, ?, ?)";
jdbcTemplateObject.update( SQL3, sid, marks, year);
System.out.println("Created Name = " + name + ", Age = " + age);
transactionManager.commit(status);
}
catch (DataAccessException e) {
System.out.println("Error in creating record, rolling back");
transactionManager.rollback(status);
throw e;
}
return;
}
public List<StudentMarks> listStudents() {
String SQL = "select * from Student, Marks where Student.id=Marks.sid";
List <StudentMarks> studentMarks = jdbcTemplateObject.query(SQL,
new StudentMarksMapper());
return studentMarks;
}
}