【转载】JDBC的连接参数的设置导致rowid自动添加到sql

原博文地址:http://blog.itpub.net/8202193/viewspace-1029914/

1,问题描述:
在数据库后台经常捕获到如下的sql:
select rowid, * From PrpDcompany Where ComCode =:1
而实际上我们系统只触发了如下的sql:
select * From PrpDcompany Where ComCode =:1

从weblogic到oracle的整个过程中,有一个步骤自动添加了rowid到sql中;

2,问题原因解释:
经多方了解,JDBC的连接属性的设置可能导致rowid的自动添加;
我们现有程序中存在如下的JDBC属性设置:
preparedStatement = con.prepareStatement(strSql,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

属性ResultSet.TYPE_SCROLL_INSENSITIVE表示数据从数据库读取出来后放到client端的resultset后,不再关心数据库中的数据是否变化了,即使变化了
他也不会去同步;

ResultSet.CONCUR_UPDATABLE--该属性表示取出来的数据可能需要被更新回数据库;由于可能需要被更新回去,
所以执行select的时候会把rowid自动添加进来,这个rowid用来唯一标识一条记录;

属性ResultSet.TYPE_SCROLL_INSENSITIVE和ResultSet.CONCUR_UPDATABLE共同使用本来就是矛盾的,一个不再关心数据库里的数据了,
一个还是需要往回更新数据;

3,问题重现
根据以上的理论,设计一个java,并在后台跟踪,结果是:
属性ResultSet.TYPE_SCROLL_INSENSITIVE和ResultSet.CONCUR_UPDATABLE共同使用的场合,会出现:
PARSE ERROR #7:len=51 dep=0 uid=90 oct=3 lid=90 tim=8485473208992 err=936
select rowid, * From PrpDcompany Where ComCode =:1


java 文件如下:

package com.sinosoft.accfee.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;

public class Test1 {
public static void main(String[] args) throws Exception {
//定义驱动器地址
String url="jdbc:oracle:thin:@10.0.13.40:1521:perfdb";
//下载驱动器
Class.forName("oracle.jdbc.driver.OracleDriver");
//与驱动器建立连接
Connection connection=DriverManager.getConnection(url,"ccic","ccic$1211");
Thread.sleep(120*1000);
try {
for(int i=0;i<1000000;i++){
Statement stmt=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);


ResultSet resultset = (ResultSet)stmt.executeQuery("Select comtype From prpdcompany Where comcode = '11010001'");

while(resultset.next()){
System.out.println(resultset.getString(1));
//..........
}
//关闭打开的连接、包容器、记录集、
resultset.close();

ResultSet resultset1 = (ResultSet)stmt.executeQuery("Select * From prpdcompany Where comcode = '11010001'");

while(resultset1.next()){
System.out.println(resultset1.getString(1));
//..........
}
//关闭打开的连接、包容器、记录集、
resultset1.close();

stmt.close();
Thread.sleep(1000);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
connection.close();
}
}
}

trace 如下:

*** 2009-12-22 16:20:23.013
*** SERVICE NAME:(SYS$USERS) 2009-12-22 16:20:23.007
*** SESSION ID:(2153.8127) 2009-12-22 16:20:23.007
=====================
PARSING IN CURSOR #1 len=65 dep=0 uid=90 oct=3 lid=90 tim=8563923159082 hv=1060576053 ad='f41bc160'
select rowid, comtype From prpdcompany Where comcode = :"SYS_B_0"
END OF STMT
PARSE #1:c=0,e=644,p=0,cr=0,cu=0,mis=1,r=0,dep=0,og=1,tim=8563923159078
EXEC #1:c=0,e=1897,p=0,cr=0,cu=0,mis=1,r=0,dep=0,og=1,tim=8563923166667
FETCH #1:c=0,e=80,p=0,cr=3,cu=0,mis=0,r=1,dep=0,og=1,tim=8563923166842
STAT #1 id=1 cnt=1 pid=0 pos=1 obj=50329 op='TABLE ACCESS BY INDEX ROWID PRPDCOMPANY (cr=3 pr=0 pw=0 time=67 us)'
STAT #1 id=2 cnt=1 pid=1 pos=1 obj=50330 op='INDEX UNIQUE SCAN PK_PRPDCOMPANY (cr=2 pr=0 pw=0 time=33 us)'
=====================
PARSE ERROR #1:len=60 dep=0 uid=90 oct=3 lid=90 tim=8563923230414 err=936
select rowid, * From prpdcompany Where comcode = :"SYS_B_0"
=====================
PARSING IN CURSOR #1 len=52 dep=0 uid=90 oct=3 lid=90 tim=8563923244727 hv=1150625011 ad='af22b5c0'
Select * From prpdcompany Where comcode = :"SYS_B_0"
END OF STMT
PARSE #1:c=0,e=85,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=8563923244725
EXEC #1:c=0,e=35,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=8563923244811
FETCH #1:c=0,e=56,p=0,cr=3,cu=0,mis=0,r=1,dep=0,og=1,tim=8563923245068
STAT #1 id=1 cnt=1 pid=0 pos=1 obj=50329 op='TABLE ACCESS BY INDEX ROWID PRPDCOMPANY (cr=3 pr=0 pw=0 time=30 us)'
STAT #1 id=2 cnt=1 pid=1 pos=1 obj=50330 op='INDEX UNIQUE SCAN PK_PRPDCOMPANY (cr=2 pr=0 pw=0 time=13 us)'

[@more@]

posted @ 2021-03-12 14:12  JarvisLau  阅读(89)  评论(0编辑  收藏  举报