JBoss中查找UserTransaction的JNDI问题,急啊!

ccc426 2005-04-18 05:26:14
我在JBOSS下用到UserTransaction,但是总是在绑定JNDI时出错
我是需要进行什么配置吗?还是代码有问题,诸位大侠帮忙

代码:
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.transaction.UserTransaction;
import java.util.Hashtable;

import com.basic.vo.adm.RolesVO;
import com.bo.adm.RolesManager;

public class TestJTA2 {
public TestJTA2() {
}

private Context getInitialContext() throws Exception {
Hashtable environment = new Hashtable();
environment.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
environment.put(Context.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
environment.put(Context.PROVIDER_URL, "jnp://localhost:1099");
return new InitialContext(environment);
}

public void test() throws Exception {
Context context = getInitialContext();
UserTransaction ut = (UserTransaction) context.lookup("java:/comp/UserTransaction");
try {
ut.begin();
//操作数据
RolesVO roles = new RolesVO();
roles.setStaffID("pk");
roles.setAgentID("ag");
roles.setName("tt");
RolesManager.getInstance().registerRoles(roles);
ut.commit();
} catch (Exception ex) {
try {
ut.rollback();
} catch (Exception syex) {
throw new Exception
("Rollback failed: " + syex.getMessage());
}
throw new Exception
("Transaction failed: " + ex.getMessage());
}

}

public static void main(String[] args) {
TestJTA2 testjta2 = new TestJTA2();
try {
testjta2.test();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

错误:
javax.naming.NameNotFoundException: comp not bound

at org.jnp.server.NamingServer.getBinding(NamingServer.java:490)

at org.jnp.server.NamingServer.getBinding(NamingServer.java:498)

at org.jnp.server.NamingServer.getObject(NamingServer.java:504)

at org.jnp.server.NamingServer.lookup(NamingServer.java:248)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:324)

at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:261)

at sun.rmi.transport.Transport$1.run(Transport.java:148)

at java.security.AccessController.doPrivileged(Native Method)

at sun.rmi.transport.Transport.serviceCall(Transport.java:144)

at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)

at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)

at java.lang.Thread.run(Thread.java:534)

at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)

at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)

at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:133)

at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)

at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:530)

at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:509)

at javax.naming.InitialContext.lookup(InitialContext.java:347)

at testjta.TestJTA2.test(TestJTA2.java:35)

at testjta.TestJTA2.main(TestJTA2.java:64)
...全文
332 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccc426 2005-04-19
  • 打赏
  • 举报
回复
谢谢楼上两位,通过!
飞行的兔子 2005-04-18
  • 打赏
  • 举报
回复
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
prop.put(Context.PROVIDER_URL, "localhost:1099");
prop.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
是这样写,没错,楼主将jndi 目录空间搞错了
jboss: java:/defaultDs

tomcat: java:comp/env/defaultDs
这是两个服务器的默认jndi空间名称!
如果楼主用comp/UserTransaction这个作为绑定名称,也可用comp/UserTransaction作为查找名称啊!
jFresH_MaN 2005-04-18
  • 打赏
  • 举报
回复
你应该配置了UserTransaction
javax.transaction.UserTransaction作为JNDI那么
lookup的时候应该使用
java:/JNDIName即java:/UserTransaction
jFresH_MaN 2005-04-18
  • 打赏
  • 举报
回复
你使用的JNDI name是tomcat的
呵呵
应该是

UserTransaction ut = (UserTransaction) context.lookup("java:/UserTransaction");
007remember 2005-04-18
  • 打赏
  • 举报
回复
不懂
帮您顶下先

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧