关于J2EE Tranaction的几个基本概念

        Transaction不管在J2EE还是.NET领域中都是相当重要的一个组成部分。尽管很多与Transaction相关的概念在两个不同的平台中都是相通的,但是它们在Transaction的实现方面却有着很多的不同。想对.NET下的Transaction有更深入了解的朋友,可以参考idior兄写Transaction in ADO.net 2.0。在以下的篇幅里面,我就J2EE中与Transaction相关的几个概念做些讲述。
        1.什么是Transaction?所谓Transaction是指一系列不可分割的改动数据库的操作。在这个解释中,有三个关键词:一系列不可分割以及改动。仅仅是一个改动数据库的操作是没有Transaction可言,只有“一系列”操作(一组SQL语句)才可能组成Transaction;“不可分割”就意味着一致性和完整性,要么这一系列操作全部commit,要么就全部rollback;如果一系列的操作只包含enquiry操作,那么这些操作也不是Transaction。 
        2.在J2EE中,Transaction主要有几大类,具体有几种?在J2EE中,Transaction主要有Bean-Managed Transaction和Container-Managed Transaction两大类。其中在Bean-Managed Transaction中还会分为JDBC Transaction和JTA Transaction两种。
        3.什么是JDBC Transaction?它有怎样的特点?JDBC Transaction是指由Database本身去管理的事务。其最大的特点就是通过显示调用Connection接口的commit和rollback方法来完成事务的提交和回滚。事务结束的边界是commit或者rollback方法的调用,而开始的边界则不是那么明显了,它会开始于组成当前事务的所有statement中的第一个被执行的时候。具体代码如下:

ExpandedBlockStart.gifContractedBlock.gifclass CreditDAoImpl implements CreditDAO dot.gif {
InBlock.gif    Connection conn 
= getConnection();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void transfer(Currency amount, Account fromAccount, Account toAccount) throws CreditException dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif             conn.setAutoCommit(
false);
InBlock.gif             depositToAccount(conn, toAccount, amount);
InBlock.gif             withdrawFromAccount(conn, fromAccount, amount);     
InBlock.gif             conn.commit();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (Exception e) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
InBlock.gif                 conn.rollback();
InBlock.gif                 
throw new CreditException(e.getMessage());
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (SQLException e1) dot.gif{
InBlock.gif                 
throw new CreditException(e.getMessage());
ExpandedSubBlockEnd.gif            }
                       
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

        4.什么是JTA Transaction?它有怎样的特点呢?JTA Transaction是指由J2EE Transaction manager去管理的事务。其最大的特点是调用UserTransaction接口的begin,commit和rollback方法来完成事务范围的界定,事务的提交和回滚。JTA Transaction可以实现同一事务对应不同的数据库,但是它仍然无法实现事务的嵌套。具体的代码如下[1]

ExpandedBlockStart.gifContractedBlock.gifpublic void withdrawCash(double amount) dot.gif {
InBlock.gif   UserTransaction ut 
= context.getUserTransaction();
ExpandedSubBlockStart.gifContractedSubBlock.gif   
try dot.gif{
InBlock.gif      ut.begin();
InBlock.gif      updateChecking(amount);
InBlock.gif      machineBalance 
-= amount;
InBlock.gif      insertMachine(machineBalance);
InBlock.gif      ut.commit();
ExpandedSubBlockStart.gifContractedSubBlock.gif   }
 catch (Exception ex) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif       
try dot.gif{
InBlock.gif          ut.rollback();
ExpandedSubBlockStart.gifContractedSubBlock.gif       }
 catch (SystemException syex) dot.gif{
InBlock.gif           
throw new EJBException
InBlock.gif              (
"Rollback failed: " + syex.getMessage());
ExpandedSubBlockEnd.gif       }

InBlock.gif       
throw new EJBException 
InBlock.gif          (
"Transaction failed: " + ex.getMessage());
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

        5.什么是Container-Managed Transaction?它又有怎样的特点呢?Container-Managed Transaction,顾名思义,就是由Container负责管理的Transaction,当然这样Transaction是出现在EJB的范畴中。Container-Managed Transaction最大的特点是不需要显式界定事务的边界,也不需要显式的提交或者回滚事务,这一切都由Container来替我们完成。我们需要做的就是设定在一个Bean中,哪些方法是跟事务相关的,同时设定它们的Transaction Attribute既可。
        Transaction的Scope是相当重要的,特别是在一个Bean的方法中调用另外一个Bean的方法。为了便于说明问题,我们把这两个方法分别称为methodA和methodB。当methodA调用methodB的时候,methodB在事务的层面上对调用者methodA有怎样的限制(methodB中是否存在事务)以及methodA如何在事务的层面上实现对methodB的调用(是否需要重新创建一个新的事务来处理methodB的调用)都需要通过Transaction Attribute来设定的。具体的Transaction Attribute有以下六种:Required,RequiresNew,Mandatory,NotSupported,Supports和Never。有关Transaction Attribute的介绍,可以参阅J2EE Tutorial中关于Container-Managed Transaction的介绍

        [1] 代码来自SUN的J2EE Tutorial中关于Bean-Managed Transaction的介绍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值