简体   繁体   English

BMT EJB如何使用UserTransaction

[英]BMT EJB How to use the UserTransaction

I'm trying to create an example of BMT EJB and control the transaction manualy but when try to run I got an NullException and I don't kno what I'm doing wrong. 我正在尝试创建BMT EJB的示例并手动控制事务,但是当尝试运行时,出现了NullException,并且我不知道我在做什么错。 Here is the code: 这是代码:

@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
@Local(GlobalTLocal.class)
public class GlobalT implements GlobalTLocal {

@Resource
  private UserTransaction utx;

public GlobalT() {
}

@Override
public void sincroniza() {



    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
    env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;






    try {


        String sqlPostgres = "SELECT * FROM RH.RHINFORMIX";
        String insert = "INSERT INTO RH.RHINFORMIX (id_agente,fk_codigo_area,nome,email,excluido,tipoinclusao,id,teste)"+
                 " VALUES (11,11,'teste','teste',false,'I',12,'teste')";






        InitialContext ic = new InitialContext(env);
        DataSource ds = (DataSource) ic.lookup("jdbc/RHMigracaoPostgres");





        con = ds.getConnection();

        utx.begin();

//          con.setAutoCommit(false);

        stmt = con.createStatement();

        rs = stmt.executeQuery(sqlPostgres);
        while (rs.next()) {
            System.out.println("Query '" + sqlPostgres + "' returned "
                    + rs.getString(3));
        }

        stmt.executeUpdate(insert);

//          con.commit();


 //         if(true){

  //                throw new Exception();
   //           }

        utx.commit();
    } catch (Exception e) {
        try {
            utx.rollback();
        } catch (Exception e1) {

            e1.printStackTrace();
        }
        e.printStackTrace();
    } finally{
        if(rs != null)
            try {
                rs.close();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}




}

When I run this class I receive the following error: 当我运行此类时,会出现以下错误:

java.lang.NullPointerException
at br.gov.dprf.GlobalT.sincroniza(GlobalT.java:94)
at Principal.main(Principal.java:11)
java.lang.NullPointerException
at br.gov.dprf.GlobalT.sincroniza(GlobalT.java:71)
at Principal.main(Principal.java:11)

This EJB is deployed at a Jboss 4.2.2 serve and there is no erro during the Jboss startup, and we can see log of the deployed EJB: 该EJB部署在Jboss 4.2.2服务中,并且在Jboss启动期间没有错误,我们可以看到已部署EJB的日志:

15:51:33,529 INFO  [EJB3Deployer] Deployed: file:/C:/Jboss-Limpos/jboss-4.2.2.GA/server/all/deploy/GlobalTransaction.jar

I'm trying to force that the UserTransaction commit the insert for me!!! 我试图强迫UserTransaction为我提交插入内容!!!

What am I doing wrong? 我究竟做错了什么?

Tks ks

If you are using a main class, then you must launch using an application client container , and your @Resource must be a static field in the main class, not in an object you create. 如果使用的是主类,则必须使用应用程序客户端容器启动,并且@Resource必须是主类中的静态字段,而不是您创建的对象中的静态字段。 However, note that the JavaEE platform specification does not require application client containers to support UserTransaction. 但是,请注意,JavaEE平台规范不需要应用程序客户端容器来支持UserTransaction。 If JBoss does not support UserTransaction in its application client container, then your options are: 如果JBoss在其应用程序客户端容器中不支持UserTransaction,那么您的选择是:

  1. Use local transactions in your client and ignore EE features. 在客户端中使用本地事务,并忽略EE功能。 In other words, setAutoCommit(false), and write the JDBC code to connect to your database, execute the update, etc. 换句话说,setAutoCommit(false),然后编写JDBC代码以连接到数据库,执行更新等。
  2. Move this logic to the server and invoke it from the client using a remote EJB, SOAP, or some other RPC technology. 将此逻辑移至服务器,并使用远程EJB,SOAP或其他某种RPC技术从客户端调用它。

I believe you should avoid using @Resource for retrieving UserTransaction at Bean level (BMT stateless bean). 我相信您应该避免使用@Resource在Bean级别(BMT无状态Bean)上检索UserTransaction。 Instead, you can get the reference to SessionContext using: 相反,您可以使用以下方法获取对SessionContext的引用:

@Resource
SessionContext context

then use the context reference to retrieve the current UserTranaction inside the business method 然后使用上下文引用在业务方法中检索当前的UserTranaction

context.getUserTransaction();

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

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