批量操作主键冲突
我的服务器环境是AIX,java独立程序,数据库是oracle11
代码是把数据从一个大表A中,把前一天的数据,导到另外一个表B中,两个A、B表主键一样。代码进行先删(不管库表中有没有这个主键的数据)后插库。但是报错了,自知不应该setAutoCommit(true),但是不确定是不是这个问题,服务器不可以乱测试,也不可能有多次给人尝试的机会,所以想一次定位问题,解决掉,谢谢各位大侠了。后代码如下 :
public void doHscSnap() throws Exception {
		String hscTable = reconVO.getTableName();
		String hscSnapTable = reconVO.getHscSnapTable();
		Connection con = null;
		PreparedStatement stmt = null;
		PreparedStatement stmt2 = null;
		ResultSet rs = null;
		int count = 0;
		Snapdb snapdb = new Snapdb();
		String[] pks = StringUtils.splitPreserveAllTokens(reconVO.getPrimaryKey(), "|");  
		String[] cols = StringUtils.splitPreserveAllTokens(reconVO.getTableColumn(), "|");
		String colSpit = reconVO.getTableColumn().replaceAll("\\|", ",");
		try {
			con = snapdb.getConnection();
			con.setAutoCommit(true);
			delDate(snapdb);//删除N天前数据,包括N天			
			rs = snapdb.getSnapDateFromHsc(reconVO.getTableColumn(), hscTable, reconVO.getPrimaryKey());
			//getSnapDateFromHsc(this.getHscTable(),this.linkPk,getVoSnapHsc());
			StringBuffer insertSb = new StringBuffer();
			StringBuffer deleteSb = new StringBuffer();
			insertSb.append("INSERT INTO ");
			insertSb.append(hscSnapTable);
			insertSb.append("(");
			insertSb.append(colSpit);
			insertSb.append(",UPDATETIME)");
			insertSb.append(" VALUES(");
			for(count=0;count<cols.length;count++){
				insertSb.append("?,");
			}
			insertSb.append("?)");			
			deleteSb.append("DELETE FROM ");
			deleteSb.append(hscSnapTable);
			deleteSb.append(" WHERE 1=1 ");
			for(String pk : pks ){
				deleteSb.append(" AND ");
				deleteSb.append(pk);
				deleteSb.append("=? ");
			}			
			stmt = con.prepareStatement(insertSb.toString());
			stmt2 = con.prepareStatement(deleteSb.toString());
			int index =0;
			int pkIndex = 0;
			long psCount = 0;
			while(rs.next()){
				index =0;  
				pkIndex = 0;
				for(String col:cols){
					index++;
					setValtoSql(col, rs, stmt, index);
					for(String pk : pks ){
						if(col.equalsIgnoreCase(pk)){
							pkIndex++;
							setValtoSql(col, rs, stmt2, pkIndex);
						}
					}
				}
				stmt2.addBatch();
				index++;
				stmt.setTimestamp(index, rs.getTimestamp("CHANGEDATE"));
				stmt.addBatch();
				psCount++;
				if(psCount %500 ==0){//批量删除,批量保存
					stmt2.executeBatch();
					stmt2.clearBatch();					
					stmt.executeBatch();
					stmt.clearBatch();
				}
			}
			if(psCount %500 >0){
				stmt2.executeBatch();
				stmt2.clearBatch();
				stmt.executeBatch();
				stmt.clearBatch();
			}
			log.info(threadName+">>>>>hsc data total ="+psCount);
		} catch (Exception e) {
			log.error(threadName+">>>>>snap hsc data error :"+e.getMessage());
			e.printStackTrace();
		}finally{
			if(rs!=null)
				rs.close();
			//hscdb.closePrepared();
			if(stmt!=null)
				stmt.close();
			if(stmt2!=null)
				stmt2.close();
			if(con != null)
				con.close();
			snapdb.closePrepared();
		}
	}
报错如下 :2012-03-07 00:00:17,412 ERROR [ReconBO] - CaBillmail>>>>>snap hsc data error :ORA-00001: unique constraint (ZHHSC.PK_HSC_CA_BILLMAIL_SNAP) violated
补充一下:这段代码在服务器上跑了一个多月都没问题,现在突然天天出问题。求救呀。。
------解决方案--------------------可能存在这样一个问题,你没删除掉,数据又插进来了
建议在删除和插入之间清一下 session
------解决方案-------