Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > xjl:Spring 事务管理原理探究
【标  题】:xjl:Spring 事务管理原理探究
【关键字】:xjl,Spring
【来  源】:http://www.blogjava.net/huanzhugege/archive/2007/01/02/91466.html

xjl:Spring 事务管理原理探究

Your Ad Here

Spring 事务管理创造性的解决了很多以前要用重量级的应用服务器才能解决的事务问题,那么其实现原理一定很深奥吧?可是如果读者仔细研究了Spring事务管理的代码以后就会发现,事务管理其实也是如此简单的事情。这也印证了在本书开头的一句话“重剑无锋、大巧不工”,Spring并没有使用什么特殊的API,它运行的原理就是事务的原理。下面是DataSourceTransactionManager的启动事务用的代码(经简化):
protected void doBegin(Object transaction, TransactionDefinition definition)
{
?DataSourceTransactionObject txObject =
(DataSourceTransactionObject) transaction;
?Connection con = null;
?try
?{
??if (txObject.getConnectionHolder() == null)
??{
???Connection newCon = this.dataSource.getConnection();
???txObject.setConnectionHolder(
new ConnectionHolder(newCon), true);
??}
??txObject.getConnectionHolder()
.setSynchronizedWithTransaction(true);
??con = txObject.getConnectionHolder().getConnection();

??Integer previousIsolationLevel = DataSourceUtils
?????.prepareConnectionForTransaction(con, definition);
??txObject.setPreviousIsolationLevel(previousIsolationLevel);
??if (con.getAutoCommit())
??{
???txObject.setMustRestoreAutoCommit(true);
???con.setAutoCommit(false);
??}
??txObject.getConnectionHolder().setTransactionActive(true);
??// Bind the session holder to the thread.
??if (txObject.isNewConnectionHolder())
??{
???TransactionSynchronizationManager.bindResource(
getDataSource(),txObject.getConnectionHolder());
??}
?}
?catch (SQLException ex)
?{
??DataSourceUtils.releaseConnection(con, this.dataSource);
??throw new CannotCreateTransactionException(
?????"Could not open JDBC Connection for transaction", ex);
?}
}
本文出自:http://www.cownew.com
在调用一个需要事务的组件的时候,管理器首先判断当前调用(即当前线程)有没有一个事务,如果没有事务则启动一个事务,并把事务与当前线程绑定。Spring使用TransactionSynchronizationManager的bindResource方法将当前线程与一个事务绑定,采用的方式就是ThreadLocal,这可以从TransactionSynchronizationManager类的代码看出。
public abstract class TransactionSynchronizationManager
{
?……
?private static final ThreadLocal currentTransactionName = new ThreadLocal();
?private static final ThreadLocal currentTransactionReadOnly = new ThreadLocal();
?private static final ThreadLocal actualTransactionActive = new ThreadLocal();?……
}
从doBegin的代码中可以看到在启动事务的时候,如果Connection是的自动提交的(也就是getAutoCommit()方法返回true)则事务管理就会失效,所以首先要调用setAutoCommit(false)方法将其改为非自动提交的。setAutoCommit(false)这个动作在有的JDBC驱动中会非常耗时,所以最好在配置数据源的时候就将“autoCommit”属性配置为true。

web开发中的权限设计拙见一二(2) ----数据库设计:【上一篇】
商业智能与实验室智能:【下一篇】
【相关文章】
  • Eclipse RCP与Spring的整合
  • 经验主义害死人啊(spring与osgi结合当中遇到的问题)
  • SpringSide开发实战(三):漫谈CSS和页面布局
  • 图解MyEclipse配置struts+hibernate+spring+FreeMarker
  • 反思spring:由Ruby on Rails想到的
  • 用代码学习Spring:IoC、AOP
  • 整合JSF与Spring
  • 任务调度的再探索-在spring中使用TimerTask
  • SpringSide开发实战(二):修改数据库、字符编码和快速部署应用程序
  • FastSpring.NetV1.1 应用示例下载(OASystemWeb)
  • 【随机文章】
  • 突然对数据同步有兴趣了
  • ASP.NET中上传文件到数据库
  • DB2 V8 数据仓库环境中存在的问题2
  • VMware虚拟机下的拨号连接实验
  • 普通2层交换机中的广播风暴
  • 委托与事件的故事(For c++ developer)
  • HTML之调色原理
  • Kaspersky Flaw(5) -- 修改没有输出和不是系统服务的内核函数
  • 第3章 简单的 port -- 3.4 测试 port
  • Rhel AS4 server sendmail配置,带SMTP认证
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.