# 问题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)
原文地址:http://blog.51cto.com/f1yinsky/2103803