`

ibatis事务和高速缓存

    博客分类:
  • java
阅读更多

事务处理

事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit)。

事务应该具有4个属性:原子性、一致性、隔离性、持续性。这四个属性通常称为ACID特性。

原子性(atomicity)。一个事务是一个不可分割的工作单位,事务中包括的诸操作要么都做,要么都不做。

一致性(consistency)。事务必须是使数据库从一个一致性状态变到另一个一致性状态。一致性与原子性是密切相关的。

隔离性(isolation)。一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。

持久性(durability)。持续性也称永久性(permanence),指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。

一般的事务是由很多个操作组成的,所以事务一般放在业务逻辑层,低耦合,高内聚。
ibatis里事务一般分为主动事务和被动事务,在全局事务概念下
主动参与事务:iBATIS会查看现有事务的状态,并且在必要的时候主动开始一个事务,当事务失败时,会通知所有相关事务资源,执行rollback。
被动事务:iBATIS忽略当前应用程序中所有关于开始事务,提交事务,结束事务的指定代码,不是主动rollback,而是抛出异常。
SqlMapClient接口里的事务相关的三个方法: 

Java代码 复制代码
  1. public void myPointcut();   
  2.     public void startTransaction () throws SQLException ;   
  3.     public void commitTransaction () throws SQLException ;   
  4.     public void endTransaction () throws SQLException ;   
  5.     例子:    
  6.     public updateItemDescription (String itemId, String newDescription) throws SQLException    
  7.     {    
  8.         try { sqlMap.startTransaction ();    
  9.             Item item = (Item) sqlMap.queryForObject ("getItem", itemId);   
  10.             item.setDescription (newDescription);    
  11.             sqlMap.update ("updateItem", item);    
  12.             sqlMap.commitTransaction ();    
  13.         } finally    
  14.         {    
  15.             sqlMap.endTransaction ();   
  16.         }   
  17.     }   
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同样支持自己管理事务
例如:

Java代码 复制代码
  1. public void runStatementsUsingSetUserConnection() {   
  2. SqlMapClient sqlMapClient =   
  3. SqlMapClientConfig.getSqlMapClient();   
  4. Connection conn = null;   
  5. try {   
  6. conn = dataSource.getConnection();   
  7. conn.setAutoCommit(false);   
  8. sqlMapClient.setUserConnection(conn);   
  9. Person p =   
  10. (Person)sqlMapClient.queryForObject   
  11. ("getPerson"new Integer(9));   
  12. p.setLastName("Smith");   
  13. sqlMapClient.update("updatePerson", p);   
  14. Department d =   
  15. (Department)sqlMapClient.queryForObject   
  16. ("getDept"new Integer(3));   
  17. p.setDepartment(d);   
  18. sqlMapClient.update("updatePersonDept", p);   
  19. conn.commit();   
  20. finally {   
  21. sqlMapClient.setUserConnection(null);   
  22. if (conn != null) conn.close();   
  23. }   
  24. }   
  25.   
  26. 也可以使用openSession   
  27.   
  28. public void runStatementsUsingSetUserConnection() {   
  29. SqlMapClient sqlMapClient =   
  30. SqlMapClientConfig.getSqlMapClient();   
  31. Connection conn = null;   
  32. SqlMapSession session = null;   
  33. try {   
  34. conn = dataSource.getConnection();   
  35. conn.setAutoCommit(false);   
  36. session = sqlMapClient.openSession(conn);   
  37. Person p =   
  38. (Person)session.queryForObject("getPerson",   
  39. new Integer(9));   
  40. p.setLastName("Smith");   
  41. session.update("updatePerson", p);   
  42. Department d =   
  43. (Department)session.queryForObject   
  44. ("getDept"new Integer(3));   
  45. p.setDepartment(d);   
  46. session.update("updatePersonDept", p);   
  47. conn.commit();   
  48. finally {   
  49. if (session != null) session.close();   
  50. if (conn != null) conn.close();   
  51. }   
  52. }  
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

  

Xml代码 复制代码
  1. <cacheModel id="product-cache" type="MEMORY">    
  2. <flushInterval hours="24"/> //每隔多长时间更新,hours,minutes,seconds等   
  3. <flushOnExecute statement="insertProduct"/> //定义的映射id,当执行insertProduct时,执行高速缓存   
  4. <flushOnExecute statement="updateProduct"/>    
  5. <flushOnExecute statement="deleteProduct"/>    
  6. <property name=”reference-type” value=”WEAK” /> //MEMORY cache实现只认识一个<property>元素。这个名为“reference-type”属性的值必须是STRONG,SOFT和WEAK三者其一。   
  7. 默认是weak,让垃圾处理器去处理   
  8. </cacheModel>    
  9. <cacheModel id="product-cache" type="LRU">    
  10. <flushInterval hours="24"/>    
  11. <flushOnExecute statement="insertProduct"/>    
  12. <flushOnExecute statement="updateProduct"/>    
  13. <flushOnExecute statement="deleteProduct"/>    
  14. <property name=”size” value=”1000” /> //缓冲区大小   
  15. </cacheModel>    
  16. <cacheModel id="product-cache" type="FIFO">    
  17. <flushInterval hours="24"/>    
  18. <flushOnExecute statement="insertProduct"/>    
  19. <flushOnExecute statement="updateProduct"/>    
  20. <flushOnExecute statement="deleteProduct"/>    
  21. <property name=”size” value=”1000” />    
  22. </cacheModel>    
  23. <cacheModel id="product-cache" type="OSCACHE">    
  24. <flushInterval hours="24"/>    
  25. <flushOnExecute statement="insertProduct"/>    
  26. <flushOnExecute statement="updateProduct"/>    
  27. <flushOnExecute statement="deleteProduct"/>    
  28. </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> 

 

实际用法

Xml代码 复制代码
  1. <select id=”getProductList” cacheModel=”productCache”>    
  2. select * from PRODUCT where PRD_CAT_ID = #value#    
  3. </select>   
<select id=”getProductList” cacheModel=”productCache”> 
select * from PRODUCT where PRD_CAT_ID = #value# 
</select> 


 转载自:http://crazycat03.iteye.com/blog/550491


 

 

分享到:
评论

相关推荐

    iBATIS实战

    书中既详实地介绍了iBATIS的设计理念和基础知识,也讨论了动态SQL、高速缓存、DAD框架等高级主题,还讲解了iBATIS在实际开发中的应用。书的最后给出了一个设计优雅、层次清晰的示例程序JGameStore,该示例涵盖全书的...

    iBATIS内置别名列表

    高速缓存控制器用于管理查询结果缓存,提高数据访问性能。iBATIS提供了以下几种内置的缓存控制器别名: 1. **FIFO**:`com.ibatis.sqlmap.engine.cache.fifo.FifoCacheController` - FIFO(First In First Out)...

    ibatis hello world code

    8. **第9章 使用高速缓存提高性能**:讨论了iBATIS的缓存机制,包括本地缓存和二级缓存,以及如何通过缓存来提升应用程序的响应速度和效率。 9. **第8章 使用动态SQL**:这部分可能讲解了iBATIS的动态SQL功能,如何...

    操作数据库 iBATIS查询

    - **cacheModelsEnabled**: 是否开启数据高速缓存,默认为`true`。 - **enhancementEnabled**: 是否启用cglib中已优化的类来提高延迟加载的性能,默认为`true`。 - **lazyLoadingEnabled**: 是否开启延迟加载,默认...

    ibatis memcached 整合 源码 文档

    整合Ibatis和Memcached的主要目的是利用Memcached的高速缓存来减少对数据库的直接访问,从而提高性能。以下是一些整合的关键步骤: 1. **安装Memcached**:首先在服务器上安装并运行Memcached服务。 2. **引入依赖*...

    iBATIS实战.pdf第二部分,就两部分。

    4. **高速缓存**:iBATIS提供了缓存机制,可以缓存查询结果,提高数据访问速度,减少对数据库的压力。缓存分为一级缓存和二级缓存,分别对应SqlSession级别的缓存和SqlMapClient级别的缓存。 5. **事务管理...

    ibatis教程学习笔记

    - `cacheModelsEnabled` 属性:控制是否启用数据高速缓存,默认为 true(开启)。 - `enhancementEnabled` 属性:控制是否启用 cglib 中已优化类来提高延迟加载性能,默认为 true(开启)。 - `...

    金融信息服务平台项目技术与方案.pdf

    Redis被选作缓存框架,因其高速的key-value操作和复杂数据结构支持,有效缓解了高并发下数据库的压力。 前端开发部分,针对PC端,由于浏览器兼容性的需求,主要使用JQuery 1.11.3来处理JavaScript逻辑。后台则可以...

    分布式应用层中间件的设计.pdf

    在实际应用中,DDLM中间件需要处理多种挑战,比如保证数据一致性和事务性、优化SQL语句以匹配不同数据库的特点、以及如何高效地管理数据分片和负载均衡。DDLM的设计不仅要关注于技术实现,还要考虑到系统的可扩展性...

    Java 亚信上海 面试题

    - 支持数据的高速读取和写入。 **iBATIS与Hibernate的区别:** - **iBATIS**: - 更接近传统的SQL编程。 - 需要手动编写SQL语句。 - 适用于复杂的查询场景。 - **Hibernate**: - 自动管理对象生命周期。 - ...

Global site tag (gtag.js) - Google Analytics