`

EJB transction, BMT, CMT,setRollbackOnly

ejb 
阅读更多
So, you have two choices: BMT and CMT.
(unless you're an entity, in which case you're CMT)
With BMT, your job is to demarcate the start and end of the transaction.
You use your context (EJBContext) to get yourself a UserTransaction, and off you go...
UserTransaction ut = ctx.getUserTransaction();
Then you start it...
ut.begin();
Then you end it in either a commit or a rollback:
ut.commit();
OR
ut.rollback();
But there are TWO different setRollbackOnly() methods, and ONE getRollbackOnly() method.
Both UserTransaction and EJBContext have a setRollbackOnly().
So, if you are BMT, just remember that the ONLY transaction-related thing that you do with your EJBContext is simply to GET the UserTransaction. That's it. After that, EVERYTHING a BMT bean does with respect to transactions is in UserTransaction interface. So that means:
BMT == UserTransaction.setRollbackOnly()
And why would you use it? Because you might know the transaction is going badly *before* you reach the place where the transaction actually ends. You might not want to end the tx at the moment you discover it won't work, for many reasons. Perhaps you want to keep only one place in your code where the transaction ends. Or... you might need the rest of the transactional code to run for other side-effects, who knows.
(I have a hard time coming up with good reasons)
CMT == EJBContext.setRollbackOnly()
Why use THAT? Because a CMT bean has no API for controlling the transaction, so calling setRollbackOnly() is the CMT bean's way of saying to the container "Do NOT commit this transaction!"
Then we come to getRollbackOnly(). That method lives in only ONE interface, EJBContext.
Why use it?
If a bean is doing some transactional work, the bean might want to find out if that work is pointless, since the transaction has already been given a death sentence. So a CMT bean calls EJBContext.setRollbackOnly() to make sure that some other transaction participant has not previously called setRollbackOnly.
(keep in mind that a CMT bean might be participating in a tx started by a BMT bean; but who/what started the tx is transparent to the CMT bean)
(and keep in mind that a BMT bean can participate ONLY in transactions that the BMT bean himself started. No transaction will propogate in to a method call on a BMT bean.)
So if there's no getRollbackOnly() in UserTransaction, how does a BMT bean find out if someone called setRollbackOnly() on the tx? By calling getStatus() on the UserTransaction interface (which gives you a status code -- there are bunch -- you do NOT need to know what these are on the exam.)
Summary of what is legal:
BMT:
EJBContext.getUserTransaction()
UserTransaction.begin
UserTransaction.commit
UserTransaction.rollback
UserTransaction.setRollbackOnly()
UserTransaction.getStatus()
CMT:
EJBContext.setRollbackOnly()
EJBContext.getRollbackOnly()
That's it!! A CMT cannot call ANYTHING in UserTransaction. In fact, a CMT isn't even allowed to *think* about UserTransactions. (and the container can tell...)
A BMT bean cannot call ANYTHING that is transaction-related in EJBContext, *except* getUserTransaction. After that, it is UserTransaction all the way down for a BMT bean.
Whew!
cheers,
分享到:
评论

相关推荐

    EJB方面 ejb pdf

    容器管理的事务(Container-Managed Transaction, CMT)由EJB容器自动控制,开发者无需显式编写事务代码,适用于大部分场景。Bean管理的事务(Bean-Managed Transaction, BMT)则需要开发者在Bean中显式控制事务边界...

    实战EJB 实战EJB 实战EJB

    ### 实战EJB知识点解析 #### 一、企业JavaBeans (EJB) 技术概览 **什么是企业JavaBeans技术?** 企业JavaBeans (EJB) 是Java平台上的服务器端组件模型,专为构建可扩展、可靠且跨平台的企业级应用程序而设计。...

    EJB3.0规范中文版

    5. **事务管理(Transaction Management)**:EJB 3.0提供了容器管理的事务(CMT)和 bean 管理的事务(BMT)。CMT由容器自动处理事务边界,而BMT允许开发者在代码中手动管理事务。 6. **接口优先(Interface-First)**...

    ejb3.0 分布式事务

    EJB3.0中,事务管理分为容器管理的事务(CMT, Container-Managed Transactions)和 bean 管理的事务(BMT, Bean-Managed Transactions)。CMT是默认模式,由容器自动管理事务的开始、提交、回滚,开发者只需关注...

    javax.ejb.jar下载

    Files contained in javax.ejb.jar: META-INF/MANIFEST.MF javax.ejb.AccessLocalException.class javax.ejb.AccessTimeout.class javax.ejb.ActivationConfigProperty.class javax.ejb.AfterBegin.class javax....

    EJB 和WEBLOGIC 结合例子

    5. **事务管理**:WebLogic Server支持多种事务管理模型,如Bean管理的事务(BMT)和容器管理的事务(CMT),可以按照业务需求选择合适的事务策略。 6. **安全性**:EJB和WebLogic提供了强大的安全框架,可以控制对...

    java的ejb.jar包

    javax.ejb.AccessLocalException.class javax.ejb.CreateException.class javax.ejb.DuplicateKeyException.class javax.ejb.EJBContext.class javax.ejb.EJBException.class javax.ejb.EJBHome.class javax.ejb....

    EJB3.0规范-EJB3.0 SPECIFICATION

    6. **容器管理的事务(Container-managed Transactions, CMT)**:EJB容器负责事务的开始、提交、回滚,开发者只需关注业务逻辑。EJB3.0中,事务可以通过方法级别的注解如`@TransactionAttribute`来控制。 7. **...

    EJB中ejb-jar——xml文件说明.pdf

    EJB中ejb-jar文件配置详解 EJB(Enterprise JavaBean)是一种Java技术,用于开发企业级应用程序。EJB容器提供了许多功能,如事务处理、安全认证、资源管理等,以便开发者更方便地开发企业级应用程序。在EJB中,ejb-...

    实战角度比较EJB2和EJB3的架构异同

    【实战角度比较EJB2和EJB3的架构异同】 EJB,即Enterprise JavaBeans,是Java EE(企业版Java)平台的核心组件之一,用于构建可复用、分布式的服务器端应用程序。EJB2和EJB3是EJB技术的两个主要版本,它们在架构上...

    EJB3.0 PPT

    EJB 3.0提供了两种事务管理模型:容器管理的事务(CMT)和bean管理的事务(BMT)。CMT由容器自动管理事务,而BMT则允许开发者自定义事务的边界。 ### 4. EJB 3.0 的安全性 EJB 3.0继承了Java EE的安全模型,支持...

    javax.ejb.rar

    META-INF / maven / org.glassfish.main.ejb / javax.ejb / pom.properties META-INF / maven / org.glassfish.main.ejb / javax.ejb / pom.xml javax.ejb.AccessLocalException.class javax.ejb.AccessTimeout....

    Idea搭建EJB架构Demo项目源代码

    【标题】"Idea搭建EJB架构Demo项目源代码"涉及的是使用IntelliJ IDEA(简称Idea)这个强大的Java集成开发环境来构建一个基于EJB(Enterprise JavaBeans)架构的示例项目。EJB是Java EE(企业版)平台的核心部分,...

    ejb2.0与ejb3.0的区别

    ### EJB2.0与EJB3.0的主要区别 #### 一、简介 企业Java Beans(EJB)是Java平台为企业级应用提供的一种组件模型。随着技术的发展,EJB经历了多个版本的迭代,其中EJB 2.0和EJB 3.0是两个重要的里程碑版本。本文将...

    ejb 学习笔记6

    常用的方法包括`lookup(String name)`用于查找相关资源以及`setRollbackOnly()`用于控制事务回滚。 示例代码如下: ```java @Resource private SessionContext sctx; private TaxRemote tr; @EJB(name="ejb/...

    EJB白皮书EJB白皮书

    **企业级JavaBeans (EJB) 技术详解** 企业级JavaBeans(EJB)是Java平台上用于构建可部署在企业级服务器上的分布式组件模型的关键技术。EJB规范定义了一套标准,允许开发者创建可复用、安全且事务管理的业务逻辑...

    第一个EJB简单例子

    - 容器管理的事务(CMT):容器负责开始、提交或回滚事务。 - bean管理的事务(BMT):由Bean自己控制事务边界。 5. **EJB安全性**: - 角色和权限:定义不同用户群体(角色)以及他们对Bean操作的权限。 - ...

    EJB3基础教程

    EJB3中的容器管理的事务(CMT)允许开发者无需关心事务的开始、提交或回滚,这些都由EJB容器自动处理。开发者只需在方法上添加`@TransactionAttribute`注解来指定事务的属性。 ### 6. 容器服务 EJB3提供了一系列的...

Global site tag (gtag.js) - Google Analytics