谨慎使用ibatis中的缓存 |

Ibatis的xml映射文件中有一个叫cacheModel的标签, 可以用它来定义一种缓存的模式, 但是必须要谨慎使用缓存,使用不当,可能会造成一些麻烦, 例如在最近做的项目中, 在ibatis的xml映射文件里, 每个sql查询语句我都基本使用了一个cacheModel, 比如,一个新建的用户可以访问两个引擎(a和b), 此时用这个新建的用户登陆系统, 系统能正常显示该用户有权限访问的引擎;当我用另一个更高级别的用户登陆系统来修改之前刚刚新建的用户可以访问的引擎后(例如改为可以访问a、b和c),再次用此新建的用户登陆系统, 系统此时显示该用户可以访问的引擎跟原来的一样, 没有改变, 这是cacheModel造成的, 系统查询了一次, 使用缓存把第一次查询的结果保存起来, 下次再次查询时先从缓存中查找, 如果存在则直接从缓存中获得,否则才从库中查询出来。  

ibatis 使用oscache缓存

10-27

jdk1.5 ibatis 2.3.4 oscache2.4.1rn配置oscache的缓存形式,结果报错,不知道什么愿意,忘高手指点 配置如下rnoscache.properties:rn[code=Java]rn#是否使用内存作为缓存空间 rncache.memory=true rn#缓存管理事件监听器,通过这个监听器可以获知当前Cache的运行情况 rncache.event.listeners=com.opensymphony.oscache.plugins.clustersupport.JMSBroadcastingListener rncache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCachern#内存缓存的最大容量 rncache.capacity=1000rn[/code]rnSqlMapConfig.xmlrn[code=XML]rnrnrnrn rn rn rn rn rn rn rn rn rn rnrn[/code]rnAccount.xmlrn[code=XML]rnrnrnrn rn rn rn rn rn rn rnrn rn rn rn rn select * from accountrn rnrn[/code]rn每次执行的时候总是报JMSBroadcastingListener 监听出错,不知道是什么原因 错误如下rn[code=Java]rn2008-10-27 14:28:56 com.opensymphony.oscache.plugins.clustersupport.JMSBroadcastingListener initializern信息: Starting JMS clustering (node name=null, topic=null, topic factory=null)rn2008-10-27 14:28:56 com.opensymphony.oscache.base.AbstractCacheAdministrator configureStandardListenersrn严重: Could not initialize listener 'com.opensymphony.oscache.plugins.clustersupport.JMSBroadcastingListener'. Listener ignored.rncom.opensymphony.oscache.base.InitializationException: Initialization of the JMSBroadcastingListener failed: java.lang.NullPointerExceptionrn at com.opensymphony.oscache.plugins.clustersupport.JMSBroadcastingListener.initialize(JMSBroadcastingListener.java:144)rn at com.opensymphony.oscache.base.AbstractCacheAdministrator.configureStandardListeners(AbstractCacheAdministrator.java:328)rn at com.opensymphony.oscache.general.GeneralCacheAdministrator.createCache(GeneralCacheAdministrator.java:305)rn at com.opensymphony.oscache.general.GeneralCacheAdministrator.(GeneralCacheAdministrator.java:99)rn at com.opensymphony.oscache.general.GeneralCacheAdministrator.(GeneralCacheAdministrator.java:90)rn at com.ibatis.sqlmap.engine.cache.oscache.OSCacheController.(OSCacheController.java:30)rn at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)rn at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)rn at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)rn at java.lang.reflect.Constructor.newInstance(Constructor.java:494)rn at com.ibatis.common.beans.ClassInfo.instantiateClass(ClassInfo.java:337)rn at com.ibatis.common.resources.Resources.instantiate(Resources.java:296)rn at com.ibatis.sqlmap.engine.builder.xml.SqlMapParser$4.process(SqlMapParser.java:102)rn at com.ibatis.common.xml.NodeletParser.processNodelet(NodeletParser.java:121)rn at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:84)rn at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:102)rn at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:72)rn at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:51)rn at com.ibatis.sqlmap.engine.builder.xml.SqlMapParser.parse(SqlMapParser.java:40)rn at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser$10.process(SqlMapConfigParser.java:270)rn at com.ibatis.common.xml.NodeletParser.processNodelet(NodeletParser.java:121)rn at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:84)rn at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:102)rn at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:72)rn at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:51)rn at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:46)rn at com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(SqlMapClientBuilder.java:63)rn at com.mydomain.data.SimpleExample.(SimpleExample.java:33)rn at com.mydomain.Ibatis.doGet(Ibatis.java:51)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)rn at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)rn at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)rn at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)rn at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)rn at java.lang.Thread.run(Thread.java:595)rnrn[/code]rnrn

关于ibatis缓存使用

02-01

配置文件如下:rnrnrnrnrnrnrn rn rn rn rn rn rn rn rn rnrn rn rnrnrnrnUsers.xml如下:rnrnrnrnrnrn rn rn rn rn rn rnrn rn rn rn rn rn rn rn rn rn select * from usersrn rn rn (uname like #uname#rn rn (usex like #usex#)rn rn )rn rnrn rn rn rn update users set uname='呵呵' where uname=1rn rnrnrnrnrnrnrnrn测试类如下:rnpublic final class Test rnrn @SuppressWarnings("unused")rn private static SqlMapClient sqlMapper;rn static rn try rn Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");rn sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);rn reader.close(); rn catch (IOException e) rn // Fail fast.rn throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);rn rn rn rn rn @SuppressWarnings("static-access")rn public void select() rn try rn @SuppressWarnings("unused")rn Users u = new Users();rn System.out.println("开始执行!");rn long startTime = System.currentTimeMillis();rn rn for(int i=0;i<30;i++) //到33次就是瓶颈了rn this.sqlMapper.startTransaction();rn List list = this.sqlMapper.queryForList("select",u);rn Iterator it = list.iterator();rn while(it.hasNext()) rn @SuppressWarnings("unused")rn Users user = (Users)it.next();rn rn this.sqlMapper.commitTransaction(); rn rn rn long elapseTime = System.currentTimeMillis() - startTime;rn System.out.println("所用时间为: "+elapseTime+" 毫秒");rn rn catch (SQLException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn finally rn try rn this.sqlMapper.endTransaction();rn catch (Exception e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn rn rn rn public static void main(String[] args) rn Test t = new Test();rn t.select();rn rnrnrnrnrnrn一开始用memory方式,WEAK,SOFT,STRONG三种引用方式都试了,结果是循环到33次就变成瓶颈了,很慢,33次以内基本都是500毫秒多搞定的。我的CPU P41.8G,内存1G,DDR333。接着用试了LRU和FIFO两种类型,效果跟MEMORY基本一样。只是很奇怪,为什么到33次就变很慢呢,在用LRU和FIFO时,我把size从1万调整到十万,结果还是一样。不过感觉缓存还是挺有用的,不用缓存读取20次要花上20多秒。只是在一些细节方面不好把握,还有这个循环到33次的问题,是我机器的问题吗?rnrn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭