ibatis事务和高速缓存

news/2024/5/18 21:49:13 标签: iBATIS, Cache, XML, 配置管理, JDBC

事务处理

事务(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

 

 

 


http://www.niftyadmin.cn/n/872945.html

相关文章

istio数据面的网络端口和健康检查

本篇文章整理了&#xff0c;istio&#xff08;版本是1.11.2&#xff09;数据面所使用的端口&#xff0c;以及他的健康检查&#xff0c;算是读书笔记&#xff0c;方便后续查找翻看。1. istio数据面的组件介绍pilot-agent&#xff1a;主要用来生成envoy启动使用的配置文件&#x…

android打包错误,Android Studio编译打包Apk出现错误

打包Apk的时候出现“Build APK: Errors while building APK. You can find the errors in the Messages view.”错误我的Gradle&#xff1a;apply plugin: com.android.applicationandroid {compileSdkVersion 25buildToolsVersion "25.0.3"defaultConfig {applicati…

android开发 博客园,博客园(com.android.cnblogs) - 2.5.3 - 应用 - 酷安

权限信息 完全的网络访问权限 修改或删除您的USB存储设备中的内容 查看网络连接 连接WLAN网络和断开连接 查看WLAN连接 允许接收WLAN多播 更改您的音频设置 查阅敏感日志数据 直接拨打电话号码 精确位置(基于GPS和网络) 大致位置(基于网络) 检索正在运行的应用 启用应用调试 在…

有了container为什么还需要pod

问题: 容器明明对标的是单个进程的概念&#xff0c;物理机里面进程不就是调度的最小单位了吗&#xff1f;为什么k8s还特意搞了一个pod出来&#xff1f;传统玩法:我们先来看下物理机里面调度进程都需要那些资源&#xff0c;cpu、内存、网络&#xff0c;整个调度都是基于同一个操…

std::function与std::bind

一、背景介绍&#xff1a;函数指针始终不太灵活&#xff0c;它只能指向全局或静态函数&#xff0c;对于类成员函数、lambda表达式或其他可调用对象就无能为力了&#xff0c;因此&#xff0c;C11推出了std::function与std::bind这两件大杀器&#xff0c;他们配合起来能够很好的替…

android 代码生成fragment,android Fragments详解二:创建Fragment

创建Fragment要创建fragment&#xff0c;必须从Fragment或Fragment的派生类派生出一个类。Fragment的代码写起来有些像activity。它具有跟activity一样的回调方法&#xff0c;比如 onCreate(),onStart(),onPause()和onStop()。实际上&#xff0c;如果你想把老的程序改为使用fra…

K8S网络介绍

一、背景介绍&#xff1a;对于K8S里面容器之间的通讯基本上面可以分为三种类型&#xff1a;1. POD里面不同容器之间的通讯&#xff1a; 因为同一个Pod里面的不同容器之间是共享同一个POD里面的网络资源&#xff0c;所以POD里容器之间的通讯基本上就是IPC之间的通讯方式&#x…

cuda8.0.27linux.run,ubuntu16.04+cuda8.0+caffe

http://blog.csdn.net/xuzhongxiong/article/details/52717285http://blog.csdn.net/xierhacker/article/details/53035989(1)ubuntu16.04安装(2)换阿里源cd /etc/apt/备份sources.listsudo cp sources.list sources.list.bak替换更新源&#xff1a;sudo apt-get update更新软件…