我正在使用Hibernate和Websphere Application Server 8.0开发应用程序。
我在Websphere中创建了一个数据源,它可以成功连接数据库。
但是从应用程序中我得到以下错误:
SystemErr R创建会话时出错:org.hibernate.service.jndi.JndiException:无法查找JNDI名称[java:comp / env / jdbc / OracleDS]
以下是我所做的设置:
的web.xml
当我试图获得sessiofactory时,它给了我错误:
HibernateUtil.java:
try
{
Configuration configuration = new Configuration().configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
catch (HibernateException he)
{
System.err.println("Error creating Session: " + he);
throw new ExceptionInInitializerError(he);
}
错误:
显示java.lang.NullPointerException 在com.ibm.ws.webcontainer.metadata.WebComponentMetaDataImpl.getJavaNameSpace(WebComponentMetaDataImpl.java:143) 在com.ibm.ws.threadContext.JavaNameSpaceAccessorImpl.getJavaNameSpace(JavaNameSpaceAccessorImpl.java:79) 在com.ibm.ws.naming.java.javaURLContextFactory.createURLContextRoot(javaURLContextFactory.java:137) at com.ibm.ws.naming.urlbase.UrlContextFactory.getObjectInstance(UrlContextFactory.java:101) at org.apache.aries.jndi.URLContextProvider.getContext(URLContextProvider.java:43) at org.apache.aries.jndi.DelegateContext.getURLContext(DelegateContext.java:252) at org.apache.aries.jndi.DelegateContext.findContext(DelegateContext.java:214) 在org.apache.aries.jndi.DelegateContext.findContext(DelegateContext.java:207) 在org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:157) 在javax.naming.InitialContext.lookup(InitialContext.java:432) 在org.hibernate.service.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:65) at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:116) 在org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) 在org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:223) 在org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89) 在org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) 在org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1818) 在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1776) 在com.test.util.HibernateUtil。(HibernateUtil.java:25)
创建会话时出错:org.hibernate.service.jndi.JndiException:无法查找JNDI名称[java:comp / env / jdbc / OracleDS]
请让我知道我错过了什么。
我已经应用了更多尝试,现在我遇到了以下错误:
FFDC异常:javax.naming.NamingException SourceId:com.ibm.ws.naming.java.javaURLContextFactory.createURLContextRoot ProbeId:142 Reporter:java.lang.Class@dfac0b43 javax.naming.NamingException:NMSV0308W:javaURLContextFactory无法创建javaURLContext对象,因为当前没有可从执行线程访问的java URL名称空间。 在com.ibm.ws.naming.java.javaURLContextFactory.createURLContextRoot(javaURLContextFactory.java:170) at com.ibm.ws.naming.urlbase.UrlContextFactory.getObjectInstance(UrlContextFactory.java:101) at org.apache.aries.jndi.URLContextProvider.getContext(URLContextProvider.java:43) at org.apache.aries.jndi.DelegateContext.getURLContext(DelegateContext.java:252) at org.apache.aries.jndi.DelegateContext.findContext(DelegateContext.java:214) 在org.apache.aries.jndi.DelegateContext.findContext(DelegateContext.java:207) 在org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:157) 在javax.naming.InitialContext.lookup(InitialContext.java:432) 在org.hibernate.service.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:65) at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:116) 在org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) 在org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:223) 在org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89) 在org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) 在org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1818) 在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1776) 在com.test.util.HibernateUtil。(HibernateUtil.java:25)
答案 0 :(得分:1)
您是否在引用和JNDI名称之间创建了绑定?
你可以做到
Enterprise Application > ApplicationName > Resource references
。然后选择引用并将其映射到数据源JNDI名称ibm-web-bnd.xml
文件:
<resource-ref name="jdbc/OracleDS" binding-name="jdbc/OracleDS" />
答案 1 :(得分:0)
在WebSphere中,休眠数据源不需要java:comp/env
。
所以您的hibernate.cfg.xml
看起来像这样:
.
.
.
<!-- JNDI Datasource -->
<property name="hibernate.connection.datasource">jdbc/OracleDS</property>
.
.
.