ORA-01555错误详解(一)

2014-11-24 17:55:23 · 作者: · 浏览: 0

写了段java操作数据库的代码


Java代码 String getIPList="select t.dns_ip from t_dnscachetotal t where t.locid=0";
String getLocid="select t3.locid from (select max(t2.ipstart) ipstart,max(t2.ipend) ipend from t_GGMAP_IP t2 where t2.ipstart<=query_ip( )) t1,t_GGMAP_IP t3 where t1.ipend>=query_ip( ) and t1.ipstart=t3.ipstart";
String updateDnsCacheTotal="update t_dnscachetotal t set t.locid= where t.dns_ip= ";
stmt=con.prepareStatement(getIPList);
rs=pstmt.executeQuery();
String ip;
ResultSet rs2;
int countupdate=0;
while(rs.next()){
ip=rs.getString(1);
pstmt2 = con.prepareStatement(getLocid);
pstmt2.setString(1, ip);
pstmt2.setString(2, ip);
rs2=pstmt2.executeQuery();
int locid=-1;
while(rs2.next()){
locid=rs2.getInt(1); }
pstmt2.close();
rs2.close();
countupdate++;
pstmt2=con.prepareStatement(updateDnsCacheTotal);
pstmt2.setInt(1, locid);
pstmt2.setString(2, ip);
pstmt2.executeUpdate();
pstmt2.close();
}
pstmt.close();
rs.close();
String getIPList="select t.dns_ip from t_dnscachetotal t where t.locid=0"; String getLocid="select t3.locid from (select max(t2.ipstart) ipstart,max(t2.ipend) ipend from t_GGMAP_IP t2 where t2.ipstart<=query_ip( )) t1,t_GGMAP_IP t3 where t1.ipend>=query_ip( ) and t1.ipstart=t3.ipstart"; String updateDnsCacheTotal="update t_dnscachetotal t set t.locid= where t.dns_ip= "; pstmt=con.prepareStatement(getIPList); rs=pstmt.executeQuery(); String ip; ResultSet rs2; int countupdate=0; while(rs.next()){ ip=rs.getString(1); pstmt2 = con.prepareStatement(getLocid); pstmt2.setString(1, ip); pstmt2.setString(2, ip); rs2=pstmt2.executeQuery(); int locid=-1; while(rs2.next()){ locid=rs2.getInt(1); } pstmt2.close(); rs2.close(); countupdate++; pstmt2=con.prepareStatement(updateDnsCacheTotal); pstmt2.setInt(1, locid); pstmt2.setString(2, ip); pstmt2.executeUpdate(); pstmt2.close(); } pstmt.close(); rs.close();



大体就这样。。我删了一部分代码。


核心的问题就在于


while(rs.next()){

}


因为rs.next实际上是对oracle某表持续的查询,而在循环中又在不断地update这个表,从而导致了这个1555错误,


ora 1555别人的例子 写道首先了解Oracle在什么情况下会产生ORA-01555错误:


假设有一张6000万行数据的testdb表,预计testdb全表扫描1次需要2个小时,参考过程如下:
1、在1点钟,用户A发出了select * from testdb;此时不管将来testdb怎么变化,正确的结果应该是用户A会看到在1点钟这个时刻的内容。
2、在1点30分,用户B执行了update命令,更新了testdb表中的第4100万行的这条记录,这时,用户A的全表扫描还没有到达第4100万条。毫无疑问,这个时候,第4100万行的这条记录是被写入了回滚段,假设是回滚段UNDOTS1,如果用户A的全表扫描到达了第4100万行,是应该会正确的从回滚段UNDOTS1中读取出1点钟时刻的内容的。
3、这时,用户B将他刚才做的操作提交了,但是这时,系统仍然可以给用户A提供正确的数据,因为那第4100万行记录的内容仍然还在回滚段UNDOTS1里,系统可以根据SCN到回滚段里找到正确的数据,但要注意到,这时记录在UNDOTS1里的第4100万行记录已经发生了重大的改变:就是第4100万行在回滚段UNDOTS1里的数据有可能随时被覆盖掉,因为这条记录已经被提交了!
4、由于用户A的查询时间漫长,而业务在一直不断的进行,UNDOTS1回滚段在被多个不同的transaction使用着,这个回滚段里的extent循环到了第4100万行数据所在的extent,由于这条记录已经被标记提交了,所以这个extent是可以被其他transaction覆盖掉的!
5、到了1点45分,用户A的查询终于到了第4100万行,而这时已经出现了第4条说的情况,需要到回滚段UNDOTS1去找数据,但是已经被覆盖掉了,这时就出现了ORA-01555错误。


明显我是犯了同样的错误。而解决办法大致有三个


首先:


SQL> show parameter undo

NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
undo_management string AUTO
undo_retention integer 900
undo_tablespace string UNDOTBS1



可以看到我们是自动