Public interface javax.ejb.EJBContext { public Identity getCallerIdentity(); public boolean isCallerInRole(Identity other); public EJBHome getEJBHome(); public Properties getEnvironment(); public UserTransaction getUserTransaction() throwsIllegalStateException; public boolean getRollbackOnly(); public void set RollbackOnly(); }
Leaving a tranaction active across method calls is stateful,and is not allowed for stateless session beans.Fro similar reasons,entity beans are also not allowed to maintain an open transaction state across method calls when the bean has declared the TX_BEAN_MANAGED transaction control.
会话同步接口 有状态和无状态的会话bean都可以访问数据库,并且参与一个事务。为了让bean在事务中执行它的任务,bean开发者可以实现在bean中实现 javax.ejb.SessionSynchronization接口。容器能自动检测这个接口,容器会使用这个接口中的方法以使bean得到事务的状态信息。实体bean不支持这个接口。因为实体bean are implicitly transaction aware,所以容器使用不同的方法控制一个事务中的实体 bean.
SessionSynchronization接口定义如下:
public interface javax.ejb.SessionSynchronization { public void afterBegin() throws RemoteException; public void beforeCompletion() throws RemoteException; public void afterCompletion(boolean yn) throws RemoteException; }
Current current = new Current(); Current.setServiceProvider(txMgrURL); Current.create(); Current.begin(); Current.doSomeWork(); RemRef1.doSomeWork(); RemRef2.doMoreWork(); Current.commit();