- 浏览: 483212 次
- 性别:
- 来自: 大连
文章分类
最新评论
-
龘龘龘:
TrueBrian 写道有个问题,Sample 1中,为了控制 ...
What's New on Java 7 Phaser -
龘龘龘:
楼主总结的不错。
What's New on Java 7 Phaser -
TrueBrian:
有个问题,Sample 1中,为了控制线程的启动时机,博主实际 ...
What's New on Java 7 Phaser -
liguanqun811:
不知道楼主是否对zookeeper实现的分布式锁进行过性能测试 ...
Distributed Lock -
hobitton:
mysql的get lock有版本限制,否则get lock可 ...
Distributed Lock
本文部分内容节选自Enterprise JavaBeans 3.0 by Bill Burke & Richard Monson-Haefel
4 Persistence
EJB支持声明性事务,事务传播行为可以由TransactionAttribute annotation或者XML配置文件指定。事务属性有下列可选值:NotSupported、Supports、Required、RequiresNew、Mandatory和Never(Srping多支持一个NESTED)。默认的事务属性是Required。
伴随着事务的传播,persistence context也有特定的传播规则:
- 如果一个EJB调用了具有不同事务范围的另一个EJB,那么persistence context不会被传播。
- 如果在事务范围之外调用transaction scoped entity manager,会创建一个与调用时间等长的persistence context。
- 如果在事务范围内调用transaction scoped entity manager,并且当前没有persistence context与事务关联,那么会创建一个persistence context,当事务被提交或者回滚后,persistence context也就自动结束,被EntityManager管理的所有entity都变成detached状态。
- 如果在事务范围内调用extended entity manager,那么不论是否在事务内,entity manager在其整个生命周期内维护同一个persistence context。
- Stateful session bean是唯一允许借助@PersistenceContext注入extended persistence context的EJB组件。当stateful session bean被移除时,extended persistence context也会被关闭。
- 如果使用transaction scoped persistence context的EJB调用了使用extended persistence context的stateful session bean,那么会导致异常。
- 如果使用extended persistence context的stateful session bean调用了另一个注入transaction scoped persistence context的EJB,那么extended persistence context会被传播。
- 如果使用extended persistence context的stateful session bean调用了另一个注入extended persistence context的stateful session bean,那么extended persistence context会被传播。
- Persistence context只是在注入本地接口时有效,如果被注入的是远程接口,则嵌套方和被嵌套方将不会共享persistence context。
以下是个简单的例子:
import javax.ejb.Local; @Local public interface StatelessLocal { void doStatelessLocal(Movie m); }
import javax.ejb.Remote; @Remote public interface StatelessRemote { void doStatelessRemote(Movie m); }
import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContextType; import com.versus.ejb.injection.entitymanager.Movie; @Stateless public class StatelessImpl implements StatelessLocal, StatelessRemote { // @PersistenceContext(unitName = "ejb", type = PersistenceContextType.TRANSACTION) private EntityManager entityManager; public void doStatelessLocal(Movie movie) { // System.out.println("in StatelessImpl.doStatelessLocal()"); @SuppressWarnings("unused") Movie m2 = entityManager.find(Movie.class, new Integer(2)); // System.out.println(entityManager.contains(movie)); } public void doStatelessRemote(Movie movie) { // System.out.println("in StatelessImpl.doStatelessRemote()"); @SuppressWarnings("unused") Movie m2 = entityManager.find(Movie.class, new Integer(2)); // System.out.println(entityManager.contains(movie)); } }
import javax.ejb.Local; @Local public interface Stateful1Local { void doStatefulLocal(); }
import javax.ejb.Remote; @Remote public interface Stateful1Remote { void doStatefulRemote(); }
import javax.ejb.EJB; import javax.ejb.Stateful; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContextType; @Stateful public class Stateful1Impl implements Stateful1Local, Stateful1Remote { // @PersistenceContext(unitName = "ejb", type = PersistenceContextType.EXTENDED) private EntityManager entityManager; // @EJB private StatelessLocal statelessLocal; @EJB private StatelessRemote statelessRemote; @EJB private Stateful2Local stateful2Local; @EJB private Stateful2Remote stateful2Remote; public void doStatefulLocal() { // System.out.println("in Stateful1Impl.doStatefulLocal()"); Movie m1 = entityManager.find(Movie.class, new Integer(1)); System.out.println(entityManager.contains(m1)); // statelessLocal.doStatelessLocal(m1); statelessRemote.doStatelessRemote(m1); // stateful2Local.doStatefulLocal(m1); stateful2Remote.doStatefulRemote(m1); } public void doStatefulRemote() { // System.out.println("in Stateful1Impl.doStatefulRemote()"); Movie m1 = entityManager.find(Movie.class, new Integer(1)); System.out.println(entityManager.contains(m1)); // statelessLocal.doStatelessLocal(m1); statelessRemote.doStatelessRemote(m1); // stateful2Local.doStatefulLocal(m1); stateful2Remote.doStatefulRemote(m1); } }
import javax.ejb.Local; @Local public interface Stateful2Local { void doStatefulLocal(Movie m); }
import javax.ejb.Remote; @Remote public interface Stateful2Remote { void doStatefulRemote(Movie m); }
import javax.ejb.Stateful; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContextType; @Stateful public class Stateful2Impl implements Stateful2Local, Stateful2Remote { // @PersistenceContext(unitName = "ejb", type = PersistenceContextType.EXTENDED) private EntityManager entityManager; public void doStatefulLocal(Movie movie) { // System.out.println("in Stateful2Impl.doStatefulLocal()"); @SuppressWarnings("unused") Movie m3 = entityManager.find(Movie.class, new Integer(3)); // System.out.println(entityManager.contains(movie)); } public void doStatefulRemote(Movie movie) { // System.out.println("in Stateful2Impl.doStatefulRemote()"); @SuppressWarnings("unused") Movie m3 = entityManager.find(Movie.class, new Integer(3)); // System.out.println(entityManager.contains(movie)); } }
import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; public class PersistenceTest { public static void main(String args[]) throws Exception { // Properties properties = new Properties(); properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory"); InitialContext ctx = new InitialContext(properties); // if(true) { Object ref1 = ctx.lookup("Stateful1ImplLocal"); Stateful1Local simpleStatefulLocal = (Stateful1Local)PortableRemoteObject.narrow(ref1, Stateful1Local.class); simpleStatefulLocal.doStatefulLocal(); Object ref2 = ctx.lookup("Stateful1ImplRemote"); Stateful1Remote simpleStatefulRemote = (Stateful1Remote)PortableRemoteObject.narrow(ref2, Stateful1Remote.class); simpleStatefulRemote.doStatefulRemote(); } } }
以上代码的输出是:
in Stateful1Impl.doStatefulLocal()
true
in StatelessImpl.doStatelessLocal()
true
in StatelessImpl.doStatelessRemote()
false
in Stateful2Impl.doStatefulLocal()
true
in Stateful2Impl.doStatefulRemote()
false
in Stateful1Impl.doStatefulRemote()
true
in StatelessImpl.doStatelessLocal()
true
in StatelessImpl.doStatelessRemote()
false
in Stateful2Impl.doStatefulLocal()
true
in Stateful2Impl.doStatefulRemote()
false
发表评论
-
Terracotta in Action (3)
2009-03-25 09:20 51453 Inside Terracotta 3.1 Core T ... -
Terracotta in Action (2)
2009-03-21 21:09 47022. Terracotta Eclipse Plugin ... -
Terracotta in Action (1)
2009-03-19 21:52 62861. Introduction Terraco ... -
OpenEJB (4)
2008-05-11 09:05 3186本文部分内容节选自Enterprise JavaBeans 3 ... -
OpenEJB (2)
2008-05-11 09:03 3321本文部分内容节选自Enterprise JavaBeans 3 ... -
OpenEJB (1)
2008-05-10 22:39 5111本文部分内容节选自Enterprise JavaBeans 3 ... -
OpenJPA (7)
2008-03-25 21:56 357510 Miscellaneous Features 10 ... -
OpenJPA (6)
2008-03-23 21:33 63438 Object Locking 8.1 Configu ... -
OpenJPA (5)
2008-03-18 22:38 50527 Inheritance 对象使用引用以便关联到其 ... -
OpenJPA (4)
2008-03-11 23:27 71116 Query 6.1 JPQL Queries 6.1. ... -
OpenJPA (3)
2008-03-09 23:09 52554 EntityManagerFactory 4.1 Ove ... -
OpenJPA (2)
2008-03-05 23:59 72993 Metadata 通过javax.persist ... -
OpenJPA (1)
2008-03-04 23:11 68821 Overview Apache OpenJPA是 ... -
ActiveMQ in Action (7)
2008-02-27 14:33 126792.6.7 Wildcards Wil ... -
ActiveMQ in Action (6)
2008-02-26 15:22 137992.6 Features ActiveMQ包含了很多 ... -
ActiveMQ in Action (5)
2008-02-26 00:35 136022.5 Clustering ActiveMQ从多种 ... -
ActiveMQ in Action (4)
2008-02-26 00:21 112782.4 Security ActiveMQ ... -
ActiveMQ in Action (3)
2008-02-26 00:16 106382.3 Persistence 2.3.1 AMQ Mess ... -
ActiveMQ in Action (2)
2008-02-25 23:58 132782.2 Transport ActiveMQ目前支持 ... -
ActiveMQ in Action (1)
2008-02-25 23:18 238811 JMS 在介绍ActiveMQ ...
相关推荐
3. **部署与生命周期管理** OpenEJB 3.0 提供了自动部署功能,只需将包含 EJB 的 JAR 文件放入指定目录,OpenEJB 就能识别并启动它们。示例中展示了如何定义bean的生命周期方法,如 `@PostConstruct` 和 `@...
3. **配置EJB项目**: 创建一个EJB项目,包含所需的bean类和接口,以及ejb-jar.xml配置文件。在ejb-jar.xml中定义bean的接口、实现类和部署属性。 4. **打包EJB项目**: 使用jar命令或者Maven、Gradle等构建工具...
标题中的"openejb-embedded-maven-plugin-4.6.0.1.zip"是一个开源项目的Maven插件包,特别地,它是OpenEJB的嵌入式版本。OpenEJB是一个开源的企业JavaBeans(EJB)容器和服务器,它实现了Java EE(现在称为Jakarta ...
目前Tomcat并不直接支持EJB,但可借助openejb插件来间接能够支持EJB
3. **S/MIME**:S/MIME是一种用于保护电子邮件的安全协议,但也可应用于HTTP通信。它通过数字签名保证消息的完整性和来源认证,通过加密确保数据的私密性。在RESTful服务中,S/MIME可以用来安全地传输敏感数据。 4....
一个简单的工具,可简化企业Java bean源代码和openEJB的配置的生成
标签:arquillian-openejb-transaction-provider-1.7.0.jar,arquillian,openejb,transaction,provider,1.7.0,jar包下载,依赖包
标签:arquillian-openejb-embedded-4-4.6.0.jar,arquillian,openejb,embedded,4,4.6.0,jar包下载,依赖包
标签:arquillian-openejb-embedded-4-4.5.1.jar,arquillian,openejb,embedded,4,4.5.1,jar包下载,依赖包
标签:arquillian-openejb-embedded-4-4.7.0.jar,arquillian,openejb,embedded,4,4.7.0,jar包下载,依赖包
标签:arquillian-openejb-transaction-provider-1.7.1.jar,arquillian,openejb,transaction,provider,1.7.1,jar包下载,依赖包
标签:arquillian-openejb-embedded-4-4.7.1.jar,arquillian,openejb,embedded,4,4.7.1,jar包下载,依赖包
标签:arquillian-openejb-transaction-provider-1.5.1.jar,arquillian,openejb,transaction,provider,1.5.1,jar包下载,依赖包
标签:arquillian-openejb-transaction-provider-1.6.0.jar,arquillian,openejb,transaction,provider,1.6.0,jar包下载,依赖包
标签:arquillian-openejb-embedded-4-4.5.2.jar,arquillian,openejb,embedded,4,4.5.2,jar包下载,依赖包
标签:arquillian-openejb-embedded-4-4.5.0.jar,arquillian,openejb,embedded,4,4.5.0,jar包下载,依赖包
标签:arquillian-openejb-transaction-provider-1.5.0.jar,arquillian,openejb,transaction,provider,1.5.0,jar包下载,依赖包
标签:arquillian-openejb-transaction-provider-1.5.2.jar,arquillian,openejb,transaction,provider,1.5.2,jar包下载,依赖包
标签:arquillian-openejb-embedded-4-4.5.1-sources.jar,arquillian,openejb,embedded,4,4.5.1,sources,jar包下载,依赖包
标签:arquillian-openejb-embedded-4-4.6.0.2.jar,arquillian,openejb,embedded,4,4.6.0.2,jar包下载,依赖包