我试图manualy创建EJB BMT的例子和控制交易,但是当尝试运行我得到了一个NullException,我不硝酸钾我在做什么错。 下面是代码: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提交插入我!
我在做什么错?
韩国社交协会
行号不匹配您的码。 71和94行是干什么的?看起来你直接使用JavaSE启动Principal类:这不起作用;只有EE组件支持注入。你需要从JNDI查找您的EJB或应用程序客户端容器使用注射。 – 2012-02-07 23:26:37
很抱歉。第71行和第94行是“utx.begin();” 和 “utx.rollback();”。我就像你说的那样开始说话。你能给我举例说明如何去做你的建议吗? GlobalT类位于Jboss容器内的deploy文件夹中。在Jboss在线之后,我运行创建GlobalT实例的Principal类并调用sincroniza方法。 – IOSJR 2012-02-08 14:39:52