我使用Spring JDBC + Oracle 10g出現了一個奇怪的問題。這裏是我的DataSource配置:設置defaultRowPrefetch對查詢沒有影響
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
<property name="username" value="admin" />
<property name="password" value="admin" />
<property name="validationQuery" value="SELECT 1 FROM DUAL"/>
<property name="testOnBorrow" value="true"/>
<property name="connectionProperties" value="defaultRowPrefetch=1000" />
</bean>
起初,我還以爲是被設定的ConnectionProperties中值,但正如我微調在SQL Developer中的查詢(成本從3670走到285和計劃講解從去:45至: 03),應用程序中的時間從原來的15秒起不會波動。刪除connectionProperties設置沒有效果。所以,我所做的就是這樣的:
DAO類
private List<Activity> getAllActivitiesJustJDBC() {
String query = "select * " + "from activity a, work_order w "
+ "where a.ac_customer = 'CSC' "
+ "and w.wo_customer = a.ac_customer "
+ "and a.ac_workorder = w.wo_workorder ";
long startTime = System.currentTimeMillis();
List<Activity> activities = new ArrayList<Activity>();
try {
Connection conn = jdbcTemplate.getDataSource().getConnection();
PreparedStatement st = conn.prepareStatement(query);
st.setFetchSize(1000);
ResultSet rs = st.executeQuery();
ActivityMapper mapper = new ActivityMapper();
while (rs.next()) {
Activity activity = mapper.mapRow(rs, 1);
activities.add(activity);
}
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("Time it took...."
+ (System.currentTimeMillis() - startTime));
System.out.println("Number of activities = " + activities.size());
return activities;
}
這一次,花了獲取11,115行的時間,平均需2秒。關鍵語句是setFetchSize(1000)。所以....我喜歡選項#2,但是我需要關閉連接還是Spring處理這個對我?在選項#1中,我將使用jdbcTemplate來調用查詢方法,使用我的數據對象傳遞參數化查詢和BeanPropertyRowMapper實例,然後返回List。