关于ibatis缓存的使用

michael9527 2008-02-01 12:24:29
配置文件如下:

<?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次的问题,是我机器的问题吗?

...全文
1235 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
js4laotan 2012-03-19
  • 打赏
  • 举报
回复
我也测了,比你的还差,我用的公司电脑,不行啊
michael9527 2008-02-03
  • 打赏
  • 举报
回复
自己顶一下 ^_^
Leen_fang 2008-02-01
  • 打赏
  • 举报
回复
学习

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧