2014-10-05 77 views
1

我正在使用Spring 4.1.1并试图设置Hibernate。这是我的@Configuration类:在Tomcat 8的Hibernate 4.3.6中'无法查找JNDI名称'

@Configuration 
public class PersistencyBeansConfiguration { 
    @Bean(name = "sessionFactory") 
    public SessionFactory buildSessionFactory() { 
     try { 
      org.hibernate.cfg.Configuration configuration = new org.hibernate.cfg.Configuration(); 
      configuration.addAnnotatedClass(User.class).setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect").setProperty("hibernate.connection.datasource", "jdbcConnector").setProperty("hibernate.order_updates", "true"); 
      System.out.println("Hibernate Configuration loaded"); 
      ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); 
      System.out.println("Hibernate serviceRegistry created"); 
      SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
      return sessionFactory; 
     } catch (Throwable ex) { 
      ex.printStackTrace(); 
      return null; 
     } 
    } 
} 

我有一个数据源context.xml文件的Tomcat 8宣称:

<Resource 
    name="jdbcConnector" auth="Container" type="javax.sql.DataSource" 
    maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="root" 
    password="password" driverClassName="com.mysql.jdbc.Driver" 
    url="jdbc:mysql://localhost:3306/spring_tutorial_db?characterEncoding=utf8" 
/> 

当我尝试运行我得到以下错误的应用程序:

org.hibernate.engine.jndi.JndiException: Unable to lookup JNDI name [jdbcConnector] 
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:117) 
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:115) 
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) 
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260) 
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94) 
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) 
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843) 
    at org.demian.spring.tutorial.mvc.dao.PersistencyBeansConfiguration.buildSessionFactory(PersistencyBeansConfiguration.java:19) 
    at org.demian.spring.tutorial.mvc.dao.PersistencyBeansConfiguration$$EnhancerBySpringCGLIB$$1d69d32e.CGLIB$buildSessionFactory$0(<generated>) 
    at org.demian.spring.tutorial.mvc.dao.PersistencyBeansConfiguration$$EnhancerBySpringCGLIB$$1d69d32e$$FastClassBySpringCGLIB$$f8dc0fa4.invoke(<generated>) 
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) 
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312) 
    at org.demian.spring.tutorial.mvc.dao.PersistencyBeansConfiguration$$EnhancerBySpringCGLIB$$1d69d32e.buildSessionFactory(<generated>) 
    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.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166) 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:590) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1113) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1008) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1081) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1006) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:527) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4751) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5175) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:744) 
Caused by: javax.naming.NameNotFoundException: Name [jdbcConnector] is not bound in this Context. Unable to find [jdbcConnector]. 
    at org.apache.naming.NamingContext.lookup(NamingContext.java:818) 
    at org.apache.naming.NamingContext.lookup(NamingContext.java:152) 
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:134) 
    at javax.naming.InitialContext.lookup(InitialContext.java:415) 
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:114) 
    ... 59 more 

关于这个错误的一个奇怪的事情是,Spring对于我在我的一个xml bean定义文件中插入的JNDI查找没有问题:

<jee:jndi-lookup jndi-name="jdbcConnector" id="dataSource" 
    expected-type="javax.sql.DataSource" /> 

如果您需要更多详细信息,请在评论中提问。我是Hibernate的新手,如果我的代码错误,我很抱歉。谢谢!

+0

使用'LocalSessionFactoryBean',而不是尝试手动配置Hibernate和刚注入的数据源。使用'JndiDataSourceLookup'进行查找。 – 2014-10-06 06:04:50

回答

1

我有同样的问题,但通过将hibernate-core升级到4.3.6.Final解决了这个问题。

参见:Closed bug report

+0

从“Hibernate 4.3.5.Final”到“4.3.6.Final”修复了Tomcat 8的“NameNotFoundException”问题。 – VinZ 2016-02-04 11:04:53