关于ibatis缓存的使用
配置文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings useStatementNamespaces="true"
enhancementEnabled="true"
lazyLoadingEnabled="true" />
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver"
value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
<property name="JDBC.ConnectionURL"
value="jdbc:sqlserver://localhost:1433;databaseName=test;SelectMethod=cursor" />
<property name="JDBC.Username" value="sa" />
<property name="JDBC.Password" value="sa" />
</dataSource>
</transactionManager>
<sqlMap resource="Users.xml"/>
</sqlMapConfig>
Users.xml如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
<cacheModel id="user_memory" type="FIFO" readOnly="true" serialize="true">
<flushInterval minutes="60" />
<flushOnExecute statement="update" />
<property name="size" value="100000" />
</cacheModel>
<typeAlias alias="user" type="net.test.Users" />
<resultMap id="result" class="user">
<result property="uid" column="uid" />
<result property="uname" column="uname" />
<result property="usex" column="usex" />
</resultMap>
<select id="select" parameterClass="user" resultMap="result"
cacheModel="user_memory">
select * from users
<dynamic prepend="WHERE">
<isNotNull prepend="AND" property="uname">
(uname like #uname#
<isNotNull prepend="AND" property="usex">
(usex like #usex#)
</isNotNull>
)
</isNotNull>
</dynamic>
</select>
<update id="update">
update users set uname='呵呵' where uname=1
</update>
</sqlMap>
测试类如下:
public final class Test {
@SuppressWarnings("unused")
private static SqlMapClient sqlMapper;
static {
try {
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
// Fail fast.
throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);
}
}
@SuppressWarnings("static-access")
public void select() {
try {
@SuppressWarnings("unused")
Users u = new Users();
System.out.println("开始执行!");
long startTime = System.currentTimeMillis();
for(int i=0;i<30;i++) { //到33次就是瓶颈了
this.sqlMapper.startTransaction();
List list = this.sqlMapper.queryForList("select",u);
Iterator it = list.iterator();
while(it.hasNext()) {
@SuppressWarnings("unused")
Users user = (Users)it.next();
}
this.sqlMapper.commitTransaction();
}
long elapseTime = System.currentTimeMillis() - startTime;
System.out.println("所用时间为: "+elapseTime+" 毫秒");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
this.sqlMapper.endTransaction();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Test t = new Test();
t.select();
}
}
一开始用memory方式,WEAK,SOFT,STRONG三种引用方式都试了,结果是循环到33次就变成瓶颈了,很慢,33次以内基本都是500毫秒多搞定的。我的CPU P41.8G,内存1G,DDR333。接着用试了LRU和FIFO两种类型,效果跟MEMORY基本一样。只是很奇怪,为什么到33次就变很慢呢,在用LRU和FIFO时,我把size从1万调整到十万,结果还是一样。不过感觉缓存还是挺有用的,不用缓存读取20次要花上20多秒。只是在一些细节方面不好把握,还有这个循环到33次的问题,是我机器的问题吗?