- 浏览: 1047706 次
- 性别:
- 来自: 郑州
文章分类
- 全部博客 (605)
- 数据挖掘 (22)
- spring (40)
- 工具使用 (39)
- java (137)
- JavaScript (40)
- webwork (12)
- web (120)
- 资源 (7)
- SSH (5)
- oracle (20)
- J2ME (1)
- 环境配置 (37)
- 项目管理 (29)
- mysql (14)
- struts (4)
- 项目总结 (27)
- ibatis学习 (33)
- 学习计划 (2)
- 缓存 (7)
- 重构 (3)
- Android (1)
- jquery (12)
- UML (3)
- 用户体验 (4)
- 习惯 (7)
- sakai (1)
- urlrewrite (4)
- rss (5)
- C plus plus (5)
- 算法 (5)
- 海量数据处理 (7)
- office(word、excel) (1)
- 面试题 (3)
- solr (8)
- 大数据 (2)
最新评论
-
hujin19861102:
截图看不见,最后一个webwrok的配置看不见
Ext+Webwork+Json 实现分页表格查询效果 -
蜗牛笔:
弱弱的问一句,要是分出来的词在词典中没有,那么两部分的pos- ...
ICTCLAS 中科院分词系统 -
weipeng1986:
授人予鱼不如授人予鱼,我想问你的是你是怎么总结的。比如第四种情 ...
JAVA中字符串连接效率的测试 -
xiaoqiang2008:
执行两次的原因是什么,好像楼主没弄清楚啊!是不是在web.xm ...
关于Spring中用quartz定时器在定时到达时同时执行两次的问题 -
Kent_Mu:
...
ibatis-dynamic的用法
事务处理
事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit)。
事务应该具有4个属性:原子性、一致性、隔离性、持续性。这四个属性通常称为ACID特性。
原子性(atomicity)。一个事务是一个不可分割的工作单位,事务中包括的诸操作要么都做,要么都不做。
一致性(consistency)。事务必须是使数据库从一个一致性状态变到另一个一致性状态。一致性与原子性是密切相关的。
隔离性(isolation)。一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。
持久性(durability)。持续性也称永久性(permanence),指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。
一般的事务是由很多个操作组成的,所以事务一般放在业务逻辑层,低耦合,高内聚。
ibatis里事务一般分为主动事务和被动事务,在全局事务概念下
主动参与事务:iBATIS会查看现有事务的状态,并且在必要的时候主动开始一个事务,当事务失败时,会通知所有相关事务资源,执行rollback。
被动事务:iBATIS忽略当前应用程序中所有关于开始事务,提交事务,结束事务的指定代码,不是主动rollback,而是抛出异常。
SqlMapClient接口里的事务相关的三个方法:
- public void myPointcut();
- public void startTransaction () throws SQLException ;
- public void commitTransaction () throws SQLException ;
- public void endTransaction () throws SQLException ;
- 例子:
- public updateItemDescription (String itemId, String newDescription) throws SQLException
- {
- try { sqlMap.startTransaction ();
- Item item = (Item) sqlMap.queryForObject ("getItem", itemId);
- item.setDescription (newDescription);
- sqlMap.update ("updateItem", item);
- sqlMap.commitTransaction ();
- } finally
- {
- sqlMap.endTransaction ();
- }
- }
public void myPointcut(); public void startTransaction () throws SQLException ; public void commitTransaction () throws SQLException ; public void endTransaction () throws SQLException ; 例子: public updateItemDescription (String itemId, String newDescription) throws SQLException { try { sqlMap.startTransaction (); Item item = (Item) sqlMap.queryForObject ("getItem", itemId); item.setDescription (newDescription); sqlMap.update ("updateItem", item); sqlMap.commitTransaction (); } finally { sqlMap.endTransaction (); } }
jdbc事务:Connection对象默认是自动提交事务模式,如果屏蔽(setAutoCommit(true)),则需要显式调用。iBATIS同样支持自己管理事务
例如:
- public void runStatementsUsingSetUserConnection() {
- SqlMapClient sqlMapClient =
- SqlMapClientConfig.getSqlMapClient();
- Connection conn = null;
- try {
- conn = dataSource.getConnection();
- conn.setAutoCommit(false);
- sqlMapClient.setUserConnection(conn);
- Person p =
- (Person)sqlMapClient.queryForObject
- ("getPerson", new Integer(9));
- p.setLastName("Smith");
- sqlMapClient.update("updatePerson", p);
- Department d =
- (Department)sqlMapClient.queryForObject
- ("getDept", new Integer(3));
- p.setDepartment(d);
- sqlMapClient.update("updatePersonDept", p);
- conn.commit();
- } finally {
- sqlMapClient.setUserConnection(null);
- if (conn != null) conn.close();
- }
- }
- 也可以使用openSession
- public void runStatementsUsingSetUserConnection() {
- SqlMapClient sqlMapClient =
- SqlMapClientConfig.getSqlMapClient();
- Connection conn = null;
- SqlMapSession session = null;
- try {
- conn = dataSource.getConnection();
- conn.setAutoCommit(false);
- session = sqlMapClient.openSession(conn);
- Person p =
- (Person)session.queryForObject("getPerson",
- new Integer(9));
- p.setLastName("Smith");
- session.update("updatePerson", p);
- Department d =
- (Department)session.queryForObject
- ("getDept", new Integer(3));
- p.setDepartment(d);
- session.update("updatePersonDept", p);
- conn.commit();
- } finally {
- if (session != null) session.close();
- if (conn != null) conn.close();
- }
- }
public void runStatementsUsingSetUserConnection() { SqlMapClient sqlMapClient = SqlMapClientConfig.getSqlMapClient(); Connection conn = null; try { conn = dataSource.getConnection(); conn.setAutoCommit(false); sqlMapClient.setUserConnection(conn); Person p = (Person)sqlMapClient.queryForObject ("getPerson", new Integer(9)); p.setLastName("Smith"); sqlMapClient.update("updatePerson", p); Department d = (Department)sqlMapClient.queryForObject ("getDept", new Integer(3)); p.setDepartment(d); sqlMapClient.update("updatePersonDept", p); conn.commit(); } finally { sqlMapClient.setUserConnection(null); if (conn != null) conn.close(); } } 也可以使用openSession public void runStatementsUsingSetUserConnection() { SqlMapClient sqlMapClient = SqlMapClientConfig.getSqlMapClient(); Connection conn = null; SqlMapSession session = null; try { conn = dataSource.getConnection(); conn.setAutoCommit(false); session = sqlMapClient.openSession(conn); Person p = (Person)session.queryForObject("getPerson", new Integer(9)); p.setLastName("Smith"); session.update("updatePerson", p); Department d = (Department)session.queryForObject ("getDept", new Integer(3)); p.setDepartment(d); session.update("updatePersonDept", p); conn.commit(); } finally { if (session != null) session.close(); if (conn != null) conn.close(); } }
2 高速缓存
iBATIS里主要是在xml文件里进行一些配置
<cacheModel id=”productCache” type=”LRU”> <flushInterval hours=”24”/> <property name=”size” value=”1000” /> </cacheModel>
cacheModel有四个属性,id,type,serializable,readonly,后两个属性可以设置为true或false
type为缓存的模式,有四种MEMORY,LRU,FIFO,OSCACHE
- <cacheModel id="product-cache" type="MEMORY">
- <flushInterval hours="24"/> //每隔多长时间更新,hours,minutes,seconds等
- <flushOnExecute statement="insertProduct"/> //定义的映射id,当执行insertProduct时,执行高速缓存
- <flushOnExecute statement="updateProduct"/>
- <flushOnExecute statement="deleteProduct"/>
- <property name=”reference-type” value=”WEAK” /> //MEMORY cache实现只认识一个<property>元素。这个名为“reference-type”属性的值必须是STRONG,SOFT和WEAK三者其一。
- 默认是weak,让垃圾处理器去处理
- </cacheModel>
- <cacheModel id="product-cache" type="LRU">
- <flushInterval hours="24"/>
- <flushOnExecute statement="insertProduct"/>
- <flushOnExecute statement="updateProduct"/>
- <flushOnExecute statement="deleteProduct"/>
- <property name=”size” value=”1000” /> //缓冲区大小
- </cacheModel>
- <cacheModel id="product-cache" type="FIFO">
- <flushInterval hours="24"/>
- <flushOnExecute statement="insertProduct"/>
- <flushOnExecute statement="updateProduct"/>
- <flushOnExecute statement="deleteProduct"/>
- <property name=”size” value=”1000” />
- </cacheModel>
- <cacheModel id="product-cache" type="OSCACHE">
- <flushInterval hours="24"/>
- <flushOnExecute statement="insertProduct"/>
- <flushOnExecute statement="updateProduct"/>
- <flushOnExecute statement="deleteProduct"/>
- </cacheModel>
<cacheModel id="product-cache" type="MEMORY"> <flushInterval hours="24"/> //每隔多长时间更新,hours,minutes,seconds等 <flushOnExecute statement="insertProduct"/> //定义的映射id,当执行insertProduct时,执行高速缓存 <flushOnExecute statement="updateProduct"/> <flushOnExecute statement="deleteProduct"/> <property name=”reference-type” value=”WEAK” /> //MEMORY cache实现只认识一个<property>元素。这个名为“reference-type”属性的值必须是STRONG,SOFT和WEAK三者其一。 默认是weak,让垃圾处理器去处理 </cacheModel> <cacheModel id="product-cache" type="LRU"> <flushInterval hours="24"/> <flushOnExecute statement="insertProduct"/> <flushOnExecute statement="updateProduct"/> <flushOnExecute statement="deleteProduct"/> <property name=”size” value=”1000” /> //缓冲区大小 </cacheModel> <cacheModel id="product-cache" type="FIFO"> <flushInterval hours="24"/> <flushOnExecute statement="insertProduct"/> <flushOnExecute statement="updateProduct"/> <flushOnExecute statement="deleteProduct"/> <property name=”size” value=”1000” /> </cacheModel> <cacheModel id="product-cache" type="OSCACHE"> <flushInterval hours="24"/> <flushOnExecute statement="insertProduct"/> <flushOnExecute statement="updateProduct"/> <flushOnExecute statement="deleteProduct"/> </cacheModel>
实际用法
- <select id=”getProductList” cacheModel=”productCache”>
- select * from PRODUCT where PRD_CAT_ID = #value#
- </select>
<select id=”getProductList” cacheModel=”productCache”> select * from PRODUCT where PRD_CAT_ID = #value# </select>
转载自:http://crazycat03.iteye.com/blog/550491
发表评论
-
fork/join框架
2017-03-09 11:03 489http://blog.csdn.net/ye1992/art ... -
protobuffer
2017-03-09 10:54 668http://blog.csdn.net/antgan/art ... -
整理上传代码容易忽略的注意事项
2013-08-31 11:28 1134每个公司都有自己的代码代码上传流程:如果打包发布的话,是不存 ... -
mysql版本升级引发的问题
2013-05-16 15:25 1645问题描述: 1. ibatis 中 mysql语 ... -
网站的记住密码功能设计
2013-02-27 19:09 1302http://greenyouyou.blog.163 ... -
大型网站架构演变之路
2013-02-27 19:08 1143http://www.cnblogs.com/ivanjack ... -
使用Filter统计Java(J2EE)的web程序http请求响应时间
2013-02-27 19:07 1386http://www.cnblogs.com/ivan ... -
java bean与xml相互转换
2013-02-06 10:27 2042最近在做的项目,在各个平台之间需要大量传输数据,且结构比较 ... -
java 反射
2013-01-14 18:21 825http://www.cnblogs.com/rollenh ... -
字符串替换
2012-12-17 11:17 1151高效的字符串模式替换实现效果如下 String str ... -
httpURLConnection获取网络数据:XML格式返回与Json格式返回
2012-12-15 16:57 149141.服务器端代码样例: public class ... -
利用Freemarker实现表到功能界面的一键生成
2012-11-22 18:03 1013利用Freemarker实现表到功能界面的一键生成 h ... -
ibatis in ##的问题
2012-11-02 13:25 838http://www.hake.cc/a/biancheng/ ... -
ibatis缓存强制刷新与命中率
2012-10-19 19:06 1390缓存强制刷新: // spring注入 private Sq ... -
eclipse 的一些设置
2012-08-16 22:06 17581. 控制台信息太多,程序跑完后只能保留最后的一部分之前的 ... -
jdk1.5 System.arraycopy与jdk1.6 中Arrays.copyOf()&Arrays.copyOfRange()
2012-08-16 21:56 3277在JDK1.5的类System类中有方法 ... -
Eclipse报Java heap space错误的解决方案
2012-08-16 16:43 12064当运行大数量的数据时: (1) 如果是java 应用程 ... -
求两个字符串的最大公共字符串
2012-08-10 20:41 1059http://blog.csdn.net/wangcj625/ ... -
ffmpeg 截取视频 播放中的图片
2012-06-19 09:14 1309http://www.cnblogs.com/live365w ... -
memcached 项目中的使用
2012-06-13 09:29 1062http://ljh2008.iteye.com/blog/6 ...
相关推荐
书中既详实地介绍了iBATIS的设计理念和基础知识,也讨论了动态SQL、高速缓存、DAD框架等高级主题,还讲解了iBATIS在实际开发中的应用。书的最后给出了一个设计优雅、层次清晰的示例程序JGameStore,该示例涵盖全书的...
高速缓存控制器用于管理查询结果缓存,提高数据访问性能。iBATIS提供了以下几种内置的缓存控制器别名: 1. **FIFO**:`com.ibatis.sqlmap.engine.cache.fifo.FifoCacheController` - FIFO(First In First Out)...
8. **第9章 使用高速缓存提高性能**:讨论了iBATIS的缓存机制,包括本地缓存和二级缓存,以及如何通过缓存来提升应用程序的响应速度和效率。 9. **第8章 使用动态SQL**:这部分可能讲解了iBATIS的动态SQL功能,如何...
- **cacheModelsEnabled**: 是否开启数据高速缓存,默认为`true`。 - **enhancementEnabled**: 是否启用cglib中已优化的类来提高延迟加载的性能,默认为`true`。 - **lazyLoadingEnabled**: 是否开启延迟加载,默认...
整合Ibatis和Memcached的主要目的是利用Memcached的高速缓存来减少对数据库的直接访问,从而提高性能。以下是一些整合的关键步骤: 1. **安装Memcached**:首先在服务器上安装并运行Memcached服务。 2. **引入依赖*...
4. **高速缓存**:iBATIS提供了缓存机制,可以缓存查询结果,提高数据访问速度,减少对数据库的压力。缓存分为一级缓存和二级缓存,分别对应SqlSession级别的缓存和SqlMapClient级别的缓存。 5. **事务管理...
- `cacheModelsEnabled` 属性:控制是否启用数据高速缓存,默认为 true(开启)。 - `enhancementEnabled` 属性:控制是否启用 cglib 中已优化类来提高延迟加载性能,默认为 true(开启)。 - `...
Redis被选作缓存框架,因其高速的key-value操作和复杂数据结构支持,有效缓解了高并发下数据库的压力。 前端开发部分,针对PC端,由于浏览器兼容性的需求,主要使用JQuery 1.11.3来处理JavaScript逻辑。后台则可以...
在实际应用中,DDLM中间件需要处理多种挑战,比如保证数据一致性和事务性、优化SQL语句以匹配不同数据库的特点、以及如何高效地管理数据分片和负载均衡。DDLM的设计不仅要关注于技术实现,还要考虑到系统的可扩展性...
- 支持数据的高速读取和写入。 **iBATIS与Hibernate的区别:** - **iBATIS**: - 更接近传统的SQL编程。 - 需要手动编写SQL语句。 - 适用于复杂的查询场景。 - **Hibernate**: - 自动管理对象生命周期。 - ...