2012-02-07 37 views
0

我试图manualy创建EJB BMT的例子和控制交易,但是当尝试运行我得到了一个N​​ullException,我不硝酸钾我在做什么错。 下面是代码:BMT EJB如何使用UserTransaction的

@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) 

该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 

我试图强制UserTransaction提交插入我!

我在做什么错?

韩国社交协会

+0

行号不匹配您的码。 71和94行是干什么的?看起来你直接使用JavaSE启动Principal类:这不起作用;只有EE组件支持注入。你需要从JNDI查找您的EJB或应用程序客户端容器使用注射。 – 2012-02-07 23:26:37

+0

很抱歉。第71行和第94行是“utx.begin();” 和 “utx.rollback();”。我就像你说的那样开始说话。你能给我举例说明如何去做你的建议吗? GlobalT类位于Jboss容器内的deploy文件夹中。在Jboss在线之后,我运行创建GlobalT实例的Principal类并调用sincroniza方法。 – IOSJR 2012-02-08 14:39:52

回答

0

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

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

我相信你应该避免使用@Resource在Bean级别(BMT无状态bean)检索UserTransaction。相反,你可以使用获得参考SessionContext:

@Resource 
SessionContext context 

然后使用上下文引用从堆栈跟踪检索当前UserTranaction业务方法里面

context.getUserTransaction();