简体   繁体   English

ejb3中两个会话Bean之间的事务

[英]Transaction between two session beans in ejb3

I have three tables server_detail, server_group, server_group_mappping and the entity classes as below. 我有三个表server_detail,server_group,server_group_mappping和实体类,如下所示。 (have not given the full code details) (尚未提供完整的代码详细信息)

@Entity
@Table(name = "server_detail")
public class ServerBean implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "server_id")
private Integer ServerId;

@Column(name = "name")
private String serverName;
....
}

@Entity
@Table(name = "server_group")
public class ServerGroupBean implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "group_id")
private Integer groupId;

@Column(name = "name")
private String groupName;
....
}

@Entity
@IdClass(GroupMapPK.class)
@Table(name = "server_group_mapping")
public class ServerGroupMapBean implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "group_id")
private Integer groupId;
@Id
@Column(name = "server_id")
private Integer serverId;
....
}

Each Entity Bean class having one wrapper class, to manage operations on entity bean as below 每个实体Bean类都有一个包装器类,用于管理实体Bean上的操作,如下所示

@Stateless
@Local
public class ServerClient implements ServerLocal {
@PersistenceContext
private EntityManager em;

public ServerClient() {
}
public ServerBean create(java.lang.String name) {
    ServerBean bean = new ServerBean(name);
    em.persist(bean);
    return bean;
}
public ServerBean update(ServerBean bean){
    return (em.contains(bean) ? bean : em.merge(bean));
}
public void remove(ServerBean bean) {
    em.remove(em.contains(bean) ? bean : em.merge(bean));
}


@Stateless
@Local
public class ServerGroupClient implements ServerGroupLocal {
@PersistenceContext
private EntityManager em;

public ServerGroupClient() {
}
public ServerGroupBean create(java.lang.Integer name) {
    ServerGroupBean bean = new ServerGroupBean(name);
    em.persist(bean);
    return bean;
}
public ServerGroupBean update(ServerGroupBean bean){
    return (em.contains(bean) ? bean : em.merge(bean));
}
public void remove(ServerGroupBean bean) {
    em.remove(em.contains(bean) ? bean : em.merge(bean));
}


@Stateless
@Local
public class ServerGroupMapClient implements ServerGroupMapLocal {
@PersistenceContext
private EntityManager em;

public ServerGroupMapClient() {
}
public ServerGroupMapBean create(java.lang.Integer serverId,java.lang.Integer groupId ) {
    ServerGroupMapBean bean = new ServerBean(serverId, groupId);
    em.persist(bean);
    return bean;
}
public ServerBean update(ServerBean bean){
    return (em.contains(bean) ? bean : em.merge(bean));
}
public void remove(ServerBean bean) {
    em.remove(em.contains(bean) ? bean : em.merge(bean));
}

I am using MYSQL (innoDB Engine) for the tables and the their relation mapping amound the tables. 我正在为表使用MYSQL(innoDB Engine),并且它们之间的关系映射关系到表。

Now, I have GroupManager session Bean class, which maintains the server_group and server_group_mapping table transactions. 现在,我有了GroupManager会话Bean类,该类维护server_group和server_group_mapping表事务。 When ever i create server group and the members, i have to do following transaction. 每当我创建服务器组和成员时,我都必须执行以下事务。

 1. First, add group id and group name to server_group table
 2. Second, map groupid with server id in server_group_mapping table

Following is the code. 以下是代码。

@Stateless
@Local
public class GroupManagerBean implements GroupManagerLocal {
    @Resource
    private SessionContext context;

    private static GroupLocal GroupLocal;
    private static GroupMapLocal GroupMapLocal;
    public GroupManagerBean() {
        GroupLocal = ServiceLocator.getGroupLocal();
        smscMapLocal = ServiceLocator.getGroupMapLocal();
    }
    public void addGroup(GroupBean bean, Integer serverId){
        group = GroupLocal.create(bean.getGroupName()); ---> 1
        ...
        GroupMapLocal.create(group.getGroupId(), serverId); ----> 2
        }

ServiceLocator class is the location where can i lookup all my beans. ServiceLocator类是我可以在其中查找所有bean的位置。 Bydefault, in ejb3 transaction attribute is required. 默认情况下,在ejb3中需要事务属性。 if i execute addGroup() method. 如果我执行addGroup()方法。 m getting following exception. 我得到以下例外。

javax.ejb.EJBTransactionRolledbackException: EntityManager must be access within a transaction
at org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:115)
at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:130)
at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:194)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:186)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:41)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:67)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
at org.jboss.ejb3.session.SessionSpecContainer.invoke(SessionSpecContainer.java:219)
at org.jboss.ejb3.proxy.handler.ProxyInvocationHandlerBase.invoke(ProxyInvocationHandlerBase.java:261)
....
Caused by: javax.persistence.TransactionRequiredException: EntityManager must be access within a transaction
at org.jboss.jpa.deployment.ManagedEntityManagerFactory.verifyInTx(ManagedEntityManagerFactory.java:155)
at org.jboss.jpa.tx.TransactionScopedEntityManager.persist(TransactionScopedEntityManager.java:186)
at com.example.GroupClient.create(GroupClient.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

As per the error persistence managers(GroupClient and GroupMapClient) is out of our transaction scope. 根据错误持久性管理器(GroupClient和GroupMapClient)不在我们的事务范围内。 I would like to know, how can i make the transaction happen completely while injecting the persistence managers into the transaction scope? 我想知道,在将持久性管理器注入事务范围的同时,如何使事务完全发生?

Refer below link for details. 有关详细信息,请参见下面的链接。

https://developer.jboss.org/message/525521 https://developer.jboss.org/message/525521

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM