JBoss中查找UserTransaction的JNDI问题,急啊!
我在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)