BMT EJB 如何使用 UserTransaction

发布于 2025-01-03 13:20:02 字数 3065 浏览 1 评论 0原文

我正在尝试创建 BMT EJB 的示例并手动控制事务,但是当尝试运行时出现 NullException,并且我不知道我做错了什么。 下面是代码:

@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();
            }
        }
    }
}




}

当我运行这个类时,我收到以下错误:

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 isDeployed at a Jboss 4.2.2serve 并且在 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试图强制 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.
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:

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!!!

What am I doing wrong?

Tks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱格式化 2025-01-10 13:20:02

如果您使用主类,则必须使用应用程序客户端容器,并且您的 @Resource 必须是主类中的静态字段,而不是您创建的对象中的静态字段。但是,请注意,JavaEE 平台规范并不要求应用程序客户端容器支持 UserTransaction。如果 JBoss 在其应用程序客户端容器中不支持 UserTransaction,那么您的选择是:

  1. 在客户端中使用本地事务并忽略 EE 功能。换句话说,setAutoCommit(false),并编写 JDBC 代码来连接到数据库、执行更新等。
  2. 将此逻辑移至服务器并使用远程 EJB、SOAP 或某些其他 RPC 技术从客户端调用它。

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. However, note that the JavaEE platform specification does not require application client containers to support UserTransaction. If JBoss does not support UserTransaction in its application client container, then your options are:

  1. Use local transactions in your client and ignore EE features. In other words, setAutoCommit(false), and write the JDBC code to connect to your database, execute the update, etc.
  2. Move this logic to the server and invoke it from the client using a remote EJB, SOAP, or some other RPC technology.
梦萦几度 2025-01-10 13:20:02

我相信您应该避免使用 @Resource 在 Bean 级别(BMT 无状态 bean)检索 UserTransaction。相反,您可以使用以下方式获取对 SessionContext 的引用:

@Resource
SessionContext context

然后使用上下文引用来检索业务方法内的当前 UserTranaction

context.getUserTransaction();

I believe you should avoid using @Resource for retrieving UserTransaction at Bean level (BMT stateless bean). Instead, you can get the reference to SessionContext using:

@Resource
SessionContext context

then use the context reference to retrieve the current UserTranaction inside the business method

context.getUserTransaction();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文