EJB3(中文版) 第六集 扩展的持久化上下文

EJB3中文版 第六集 扩展的持久化上下文

通常,在JBoss EJB 3.0中,一个EntityManger的生存和死亡在一个JTA事务中.一旦事务结束,所有与EntityManager绑定的持久化对象不再受管理,任何本地缓存的EntityManager实例将会丢失.JBoss EJB3.0允许你定义超出JTA事务范围之外的长久生存的EntityManager.被叫做扩展的持久化上下文.

当你指定注入一个EntityManager作为扩展的持久化上下文,所有的对象实例仍受管理.扩展的持久化上下文仅用于有状态会话Bean.让我们看一下ShoppingCartBean:
@Stateful
@Remote(ShoppingCart.class)
public class ShoppingCartBean implements ShoppingCart
{
@PersistenceContext(type=PersistenceContextType.EXTENDED) EntityManager em;

@EJB StatelessLocal stateless;

private Customer customer;

public long createCustomer()
{
customer = new Customer();
customer.setName("William");
em.persist(customer);
return customer.getId();
}

public void update()
{
customer.setName("Bill");
}
...
注入一个扩展的持久化上下文,使用type()属性和设置它为EXTENDED.如果你看到createCustomer()方法,你将会注意到它持久化一个Customer对象并且存储对它的引用到一个有状态会话Bean的成员变量.当update()方法被调用,你会看到客户的状态被修改.因为实体管理使用的是EXTENDED,客户的成员变量仍被实体管理器管理并且状态的修改将被同步到数据库.

会话
一个有趣的情况是,当你组合扩展的持久化上下文同一个non-transactional方法.如果你交互同一个扩展的持久化上下文在事务外,插入,更新,和删除将会被执行直到访问持久化上下文在一个事务内.这意味着你可以调用任意persist(),merge(),或remove()方法,但不会返回实际的结果在JDBC执行,因此,一个数据库的更新直到你手动调用EntityManager.flush()方法.

为什么它很有用?考虑到在一个网站上使用安装向导的情况.向导有七个步骤,由七个网页进行填充.跨跃多个http请求的一个JTA事务是不可取的,你不能够提交任何数据到数据库直到所有的步骤完成.你的代码可以同EntityManager进行交互,同样你不必担心混合的更新或写操作在你的应用程序中,所有的实体管理工作将会在向导最终的完成步骤之后.高效的执行.因为管理的持久化对象仍与持久化上下文绑定.所有的优化也可以在应用程序的事务中进行维护.
下面是一个例子:
@Stateful
@Remote(ShoppingCart.class)
public class ShoppingCartBean implements ShoppingCart
{
@PersistenceContext(type=PersistenceContextType.EXTENDED) EntityManager em;

@EJB StatelessLocal stateless;

private Customer customer;

public long createCustomer()
{
customer = new Customer();
customer.setName("William");
em.persist(customer);
return customer.getId();
}

@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void never()
{
customer.setName("Bob");
}
@Remove
public void checkout()
{
}
}

调用never()方法将会更新处理Customer对象的引用,但是不会保存到数据库中直到checkout()方法被调用.规范中要任何时间你调用一个有状态会话Bean的一个事务方法,EntityManager将会参加到一个事务中.因此,never()方法的更新将会被提交在checkout()方法结束后.

GenerationType
中的详细解释见书

易发生的异常如下:
注意看动画
sionBean ejbName: StatelessSessionBean
18:22:06,984 WARN [ServiceController] Problem starting service jboss.j2ee:jar=E
xtendContext.jar,name=ShoppingCartBean,service=EJB3
java.lang.RuntimeException: It is illegal to inject an EXTENDED PC into somethin
g other than a SFSB
at org.jboss.injection.PcEncInjector.inject(PcEncInjector.java:80)
at org.jboss.ejb3.EJBContainer.start(EJBContainer.java:466)
at org.jboss.ejb3.SessionContainer.start(SessionContainer.java:121)
at org.jboss.ejb3.stateless.StatelessContainer.start(StatelessContainer.
java:96)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.jboss.ejb3.ServiceDelegateWrapper.startService(ServiceDelegateWra
pper.java:102)
at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanS
upport.java:289)
at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMB
eanSupport.java:245)
at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatch
er.java:155)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.
java:264)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
at org.jboss.system.ServiceController$ServiceProxy.invoke(ServiceControl
ler.java:978)
at $Proxy0.start(Unknown Source)
at org.jboss.system.ServiceController.start(ServiceController.java:417)
at org.jboss.system.ServiceController.start(ServiceController.java:435)
at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatch
er.java:155)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.
java:264)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
at $Proxy68.start(Unknown Source)
at org.jboss.ejb3.JmxKernelAbstraction.install(JmxKernelAbstraction.java
:96)
at org.jboss.ejb3.Ejb3Deployment.registerEJBContainer(Ejb3Deployment.jav
a:281)
at org.jboss.ejb3.Ejb3Deployment.start(Ejb3Deployment.java:328)
at org.jboss.ejb3.Ejb3Module.startService(Ejb3Module.java:91)
at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanS
upport.java:289)
at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMB
eanSupport.java:245)
at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatch
er.java:155)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.
java:264)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
at org.jboss.system.ServiceController$ServiceProxy.invoke(ServiceControl
ler.java:978)
at $Proxy0.start(Unknown Source)
at org.jboss.system.ServiceController.start(ServiceController.java:417)
at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatch
er.java:155)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.
java:264)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
at $Proxy29.start(Unknown Source)
at org.jboss.ejb3.EJB3Deployer.start(EJB3Deployer.java:449)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatch
er.java:155)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractIntercept
or.java:133)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelM
BeanOperationInterceptor.java:142)
at org.jboss.mx.interceptor.DynamicInterceptor.invoke(DynamicInterceptor
.java:97)
at org.jboss.system.InterceptorServiceMBeanSupport.invokeNext(Intercepto
rServiceMBeanSupport.java:238)
at org.jboss.ws.server.WebServiceDeployer.start(WebServiceDeployer.java:
117)
at org.jboss.deployment.SubDeployerInterceptorSupport$XMBeanInterceptor.
start(SubDeployerInterceptorSupport.java:188)
at org.jboss.deployment.SubDeployerInterceptor.invoke(SubDeployerInterce
ptor.java:95)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.
java:264)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
at $Proxy30.start(Unknown Source)
at org.jboss.deployment.MainDeployer.start(MainDeployer.java:1007)
at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:808)
at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:771)
at sun.reflect.GeneratedMethodAccessor50.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatch
er.java:155)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractIntercept
or.java:133)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelM
BeanOperationInterceptor.java:142)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.
java:264)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
at $Proxy8.deploy(Unknown Source)
at org.jboss.deployment.scanner.URLDeploymentScanner.deploy(URLDeploymen
tScanner.java:421)
at org.jboss.deployment.scanner.URLDeploymentScanner.scan(URLDeploymentS
canner.java:634)
at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.
doScan(AbstractDeploymentScanner.java:263)
at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.
loop(AbstractDeploymentScanner.java:274)
at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.
run(AbstractDeploymentScanner.java:225)

Exception in thread "main" java.lang.NoClassDefFoundError: Lorg/jboss/invocation/Invoker;
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Unknown Source)
at java.lang.Class.getDeclaredField(Unknown Source)
at java.io.ObjectStreamClass.getDeclaredSUID(Unknown Source)
at java.io.ObjectStreamClass.access$700(Unknown Source)
at java.io.ObjectStreamClass$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.io.ObjectStreamClass.<init>(Unknown Source)
at java.io.ObjectStreamClass.lookup(Unknown Source)
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at org.jboss.aop.joinpoint.InvocationResponse.readExternal(InvocationResponse.java:107)
at java.io.ObjectInputStream.readExternalData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at org.jboss.remoting.serialization.impl.java.JavaSerializationManager.receiveObject(JavaSerializationManager.java:128)
at org.jboss.remoting.marshal.serializable.SerializableUnMarshaller.read(SerializableUnMarshaller.java:66)
at org.jboss.remoting.transport.socket.SocketClientInvoker.transport(SocketClientInvoker.java:279)
at org.jboss.remoting.RemoteClientInvoker.invoke(RemoteClientInvoker.java:143)
at org.jboss.remoting.Client.invoke(Client.java:525)
at org.jboss.remoting.Client.invoke(Client.java:488)
at org.jboss.aspects.remoting.InvokeRemoteInterceptor.invoke(InvokeRemoteInterceptor.java:41)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
at org.jboss.aspects.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:34)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
at org.jboss.aspects.remoting.PojiProxy.invoke(PojiProxy.java:46)
at $Proxy0.createProxy(Unknown Source)
at org.jboss.ejb3.JndiProxyFactory.getObjectInstance(JndiProxyFactory.java:52)
at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)
at org.jnp.interfaces.NamingContext.getObjectInstance(NamingContext.java:1125)
at org.jnp.interfaces.NamingContext.getObjectInstanceWrapFailure(NamingContext.java:1142)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:705)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.lyh.ebj3.Client.Client.testWithFlushMode(Client.java:48)
at com.lyh.ebj3.Client.Client.main(Client.java:20)
不好意思忘记加@Id了
异常如下:
org.hibernate.AnnotationException: No identifier specified for entity: com.lyh.e
jb3.domain.Customer
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:62
6)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(Anno
tationConfiguration.java:452)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(Annotatio
nConfiguration.java:268)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1039
)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.j
ava:1207)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerCo

由于电脑速度慢所以多运行了一次.
下面简单的看一下Log4J的配置.
我的联系方式:
QQ:495585885
博客:http://hi.baidu.com/vsandjava 敬请关注!!!
邮箱:[email protected]

关于扩展的持久化上下文和jboss-EJB-3.0_Embeddable_ALPHA_9包的使用 我的博客是http://hi.baidu.com/vsandjava 与大家共同学习交流!

主页公布的邮箱提供下载.

你可能感兴趣的:(java,Hibernate,jboss,ejb,sun)