EJB:通过Bean管理事务实现事务传播?

时间:2013-10-28 09:14:16

标签: java java-ee jboss transactions ejb-3.1

我需要尝试跨多个有状态bean实现事务传播 我的应用程序中有3个有状态的EJB。通过远程接口方法调用将开始和结束事务控制为外部Java应用程序。

@Remote
@Stateful
public class MyEJB1 implements RemoteEJB1{
@EJB
 private RemoteEJB2 ejb2;
@Resource
UserTransaction utx;

public void startTransaction() {
    try {

        utx.begin();

    } catch (NotSupportedException e) {
        throw new EJBException(e);
    } catch (SystemException e) {
        throw new EJBException(e);
    }
}

public void commitTransaction() {
    try {
        utx.commit();

    } catch (SecurityException e) {
        throw new EJBException(e);
    } catch (IllegalStateException e) {
        throw new EJBException(e);
    } catch (RollbackException e) {
        throw new EJBException(e);
    } catch (HeuristicMixedException e) {
        throw new EJBException(e);
    } catch (HeuristicRollbackException e) {
        throw new EJBException(e);
    } catch (SystemException e) {
        throw new EJBException(e);
    }
}

public RemoteEJB2 getEJB2() {
    return ejb2;
}

}


public class MyEJB2 implements RemoteEJB2{

@EJB
private RemoteEJB3 ejb3;
@Resource(name = "java:jboss/datasources/MyDS")
private DataSource ds;

public RemoteEJB3 getEJB3() {
    return ejb3;
}

@TransactionAttribute(TransactionAttributeType.MANDATORY)
public void insertElement(String elementName) {
    PreparedStatement pStat = null;
    Connection con = null;
    try {
        con = ds.getConnection();
        String sql = "insert into TRANSACTIONTEST(COL1,COL2) values(?,?)";
        pStat = con.prepareStatement(sql);
        pStat.setString(1, elementName);
        pStat.setDouble(2, Math.random());
        pStat.executeUpdate();

    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
}

public class MyEJB3 implements RemoteEJB3{
@Resource(name="java:jboss/datasources/MyDS")
private DataSource ds;

@TransactionAttribute(TransactionAttributeType.MANDATORY)
public void updateElement(String newName) {
    PreparedStatement pStat = null;
    Connection con = null;
    try{    con = getDs().getConnection();
        String sql ="update  TRANSACTIONTEST set COL1=?";           
        pStat = con.prepareStatement(sql);
        pStat.setString(1, newName);        
        pStat.executeUpdate();

    }catch(Exception ex){
        ex.printStackTrace();
    }finally{
        try {
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
}

测试类:

public class MyTest{
public static void main(String[] args) throws Exception {
    final Hashtable jndiProperties = new Hashtable();
    jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    final Context context = new InitialContext(jndiProperties);
    MyEJB1 ejb1 = context.lookup("ejb:/EJBTrials/MyEJB1!edu.in.ejbinterfaces. RemoteEJB1?stateful");

    ejb1.startTransaction();
    RemoteEJB2 ejb2 = ejb1.getEJB2();
    ejb2.insertElement (“Test”);
    RemoteEJB3 ejb3 = ejb2.getEJB3();
    ejb3.updateElement (“UpdatedTest”);
    ejb1.commitTransaction();
}
}

理想情况下,我想在RemoteEJB1 bean上调用commitTransaction()之后完成整个事务(db中的记录插入)。

我为EJB2和EJB3尝试了针对EJB1和CMT的BMT组合,这导致抛出EJBTransactionRequiredException 我试图将所有的豆子都当成BMT。但是根据EJB3.1规范,BMT不能跨多个bean传播。 您能否告诉我可用于解决此问题的任何想法/链接?

参考应用服务器:JBOSS AS 7.1

1 个答案:

答案 0 :(得分:0)

  

您能否告诉我可用于解决此问题的任何想法/链接?

如果我正确理解您的问题,您需要的是所谓的客户管理交易划分。 UnLike Container Maneged Transaction,在这种情况下,客户端负责设置事务边界(启动和提交/回滚事务)。您可以了解如何实现它here