1. 程式人生 > >本地java類訪問websphere的JNDI

本地java類訪問websphere的JNDI

java websphere JNDI

# 問題1、獲取數據源時報錯 # javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet # 代碼:dataSource=(DataSource)envCtx.lookup("jdbc/admin"); # 原因:本地測試如果要訪問遠程的JNDI資源,就必須用飽含JNDI環境參數Hashtable初始化InitialContext。 # 必要的環境參數如: # Context.INITIAL_CONTEXT_FACTORY//連接工廠 # Context.PROVIDER_URL//訪問連接 # Context.SECURITY_PRINCIPAL//安全用戶 # Context.SECURITY_CREDENTIALS//用戶密碼 問題2、配置InitialContext # websphere 7 Hashtable<String,String> env= new Hashtable<String,String>(); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory"); env.put(Context.PROVIDER_URL, "iiop://localhost:2809"); ctx = new InitialContext(env); dataSource=(DataSource)context.lookup("jdbc/admin"); # websphere 5 Hashtable parms = new Hashtable(); parms.put(Context.INITIAL_CONTEXT_FACTORY, com.ibm.websphere.naming.WsnInitialContextFactory.class.getName()); parms.put(Context.PROVIDER_URL, "iiop://localhost:900/"); Context ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup("userdb"); //jboss: Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory" Context.URL_PKG_PREFIXES, "org.jboss.naming" Context.PROVIDER_URL, "localhost:1099" //weblogic: Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" Context.PROVIDER_URL, "t3://localhost:7001" //apusic(金蝶): Context.INITIAL_CONTEXT_FACTORY, "com.apusic.jndi.InitialContextFactory" Context.PROVIDER_URL, "rmi://localhost:6888" //WebSphere 5: Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory" Context.PROVIDER_URL, "iiop://localhost:900" //J2EE SDK(J2EE RI): Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory" Context.PROVIDER_URL, "iiop://127.0.0.1:1050" //SilverStream: Context.INITIAL_CONTEXT_FACTORY, "com.sssw.rt.jndi.AgInitCtxFactory" Context.PROVIDER_URL, "sssw://localhost:80" //OC4J: Context.INITIAL_CONTEXT_FACTORY, "com.evermind.server.rmi.RMIInitialContextFactory" Context.PROVIDER_URL, "ormi://127.0.0.1/" //WAS5: Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory" Context.PROVIDER_URL, "iiop://localhost:2809" 常用JNDI服務提供者連接工廠: Filesystem: Com.sun.jndi.fscontext.FSContextFactory或者com.sun.jndi.fscontext.RefFSContextFactory LDAPv3: Com.sun.jndi.ldap.LdapCtxFactory NDS: com.novell.naming.service.nds.NdsInitialContextFactory NIS: com.sun.jndi.nis.NISCtxFactory RMI registry: com.sun.jndi.rmi.registry.RegistryContextFactory IBM LDAP服務提供者: com.ibm.jndi.LDAPCtxFactory BEA 名字服務提供者: weblogic.jndi.WLInitialContextFactory JBOSS名字服務提供者: org.jnp.interfaces.NamingContextFactory # 問題3、數據源配置成功後,Junit調試報錯 # java.lang.NoClassDefFoundError:com/ibm/ws/bootstrp/raswsloggerFactory # 原因:com.ibm.websphere.naming.WsnInitialContextFactory這個類找不到,需要把websphere的客戶端使用的.jar包含在classpath中。 com.ibm.ws.ejb.thinclient_7.0.0.jar # javax.naming.NamingException: Failed to initialize the ORB [Root exception is org.omg.CORBA.INITIALIZE: can't instantiate default ORB implementation com.ibm.CORBA.iiop.ORB vmcid: 0x0 minor code: 0 completed: No] com.ibm.ws.orb_7.0.0.jar #所需websphere環境中的jar包 com.ibm.ws.ejb.thinclient_7.0.0.jar com.ibm.ws.orb_7.0.0.jar (下面測試中未使用) com.ibm.ws.runtime.jar ibmorb.jar 總結: 我開始測試時只是在本地建了一個JAVA類並沒有部署一個應用程序到websphere中。在websphere中部署一個應該程序後已經可以獲得連接了。 不在websphere中部署應用程序而想測試連接是否成功,則需要在getConnection()方法中傳入數據庫用戶名與密碼。 如: Hashtable parms = new Hashtable(); parms.put(Context.INITIAL_CONTEXT_FACTORY, com.ibm.websphere.naming.WsnInitialContextFactory.class.getName()); parms.put(Context.PROVIDER_URL, "iiop://localhost:900/"); Context ic = new InitialContext(); ds = (DataSource) ic.lookup("jndi/dsc"); Connection conn = ds.getConnection("userdb","userdb"); com.ibm.ws.admin.client_7.0.0.jar java.lang.NoClassDefFoundError: org/eclipse/wst/common/internal/emf/utilities/EncoderDecoder at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:800) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at com.ibm.ws.naming.util.WsnInitCtxFactory.decodeCredentials(WsnInitCtxFactory.java:778) at com.ibm.ws.naming.util.WsnInitCtxFactory.getInitialContextInternal(WsnInitCtxFactory.java:428) at com.ibm.ws.naming.util.WsnInitCtx.getContext(WsnInitCtx.java:123) at com.ibm.ws.naming.util.WsnInitCtx.getContextIfNull(WsnInitCtx.java:798) at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:164) at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:179) at javax.naming.InitialContext.lookup(InitialContext.java:411) at cn.com.icbc.netm.utils.DataSourceUtils.getDataSource(DataSourceUtils.java:52) at cn.com.icbc.netm.utils.DataSourceUtils.getConnection(DataSourceUtils.java:69) at cn.com.icbc.netm.demo.junitDemo.show(junitDemo.java:31) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: java.lang.ClassNotFoundException: org.eclipse.wst.common.internal.emf.utilities.EncoderDecoder at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) ... 45 more com.ibm.ws.ejb.thinclient_7.0.0.jar 獲取datasource失敗 javax.naming.NamingException: Failed to initialize the ORB [Root exception is java.lang.ClassCastException: com.sun.corba.se.impl.orb.ORBImpl cannot be cast to com.ibm.CORBA.iiop.ORB] at com.ibm.ws.naming.util.Helpers.getOrb(Helpers.java:364) at com.ibm.ws.naming.util.WsnInitCtxFactory.getInitialContextInternal(WsnInitCtxFactory.java:421) at com.ibm.ws.naming.util.WsnInitCtx.getContext(WsnInitCtx.java:123) at com.ibm.ws.naming.util.WsnInitCtx.getContextIfNull(WsnInitCtx.java:798) at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:164) at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:179) at javax.naming.InitialContext.lookup(InitialContext.java:411) at cn.com.icbc.netm.utils.DataSourceUtils.getDataSource(DataSourceUtils.java:52) at cn.com.icbc.netm.utils.DataSourceUtils.getConnection(DataSourceUtils.java:69) at cn.com.icbc.netm.demo.junitDemo.show(junitDemo.java:31) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: java.lang.ClassCastException: com.sun.corba.se.impl.orb.ORBImpl cannot be cast to com.ibm.CORBA.iiop.ORB at com.ibm.ws.orb.GlobalORBFactory.init(GlobalORBFactory.java:92) at com.ibm.ejs.oa.EJSORBImpl.initializeORB(EJSORBImpl.java:179) at com.ibm.ejs.oa.EJSClientORBImpl.<init>(EJSClientORBImpl.java:83) at com.ibm.ejs.oa.EJSClientORBImpl.<init>(EJSClientORBImpl.java:59) at com.ibm.ejs.oa.EJSORB.init(EJSORB.java:102) at com.ibm.ws.naming.util.Helpers.getOrb(Helpers.java:356) ... 32 more 獲取數據失敗 java.lang.NullPointerException at cn.com.icbc.netm.utils.DataSourceUtils.getConnection(DataSourceUtils.java:69) at cn.com.icbc.netm.demo.junitDemo.show(junitDemo.java:31) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


本地java類訪問websphere的JNDI