title: Spring Application Context issue - Datasource Bean Creation
body: <p>This webapp of mine is working perfectly fine on my localhost jetty server. I'm testing it out on Jelastic so I can fix any issue before deploying to a production server.</p>
  
  <p>Issue is, app can't start because of a misconfiguration that I can't get fixed, can you help me out?</p>
  
  <p>Full stack trace for the issue is:</p>
  
  <pre><code>[ERROR] ioc.Registry Operations trace:
  [ERROR] ioc.Registry [ 1] Realizing service ApplicationContext
  [ERROR] ioc.Registry [ 2] Instantiating service ApplicationContext implementation via ObjectCreator for Spring ApplicationContext
  [ERROR] ioc.Registry [ 3] Creating Spring ApplicationContext via ContextLoader
  [ERROR] SpringModuleDef.ApplicationContext Construction of service ApplicationContext failed: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException; remaining name 'jdbc/appDB'
  org.apache.tapestry5.ioc.internal.OperationException: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException; remaining name 'jdbc/appDB'
      at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.logAndRethrow(OperationTrackerImpl.java:121)
      at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:88)
      at org.apache.tapestry5.ioc.internal.PerThreadOperationTracker.invoke(PerThreadOperationTracker.java:87)
      at org.apache.tapestry5.ioc.internal.RegistryImpl.invoke(RegistryImpl.java:1124)
      at org.apache.tapestry5.internal.spring.SpringModuleDef$3.createObject(SpringModuleDef.java:184)
      at org.apache.tapestry5.ioc.internal.OperationTrackingObjectCreator$1.invoke(OperationTrackingObjectCreator.java:45)
      at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:74)
      at org.apache.tapestry5.ioc.internal.PerThreadOperationTracker.invoke(PerThreadOperationTracker.java:87)
      at org.apache.tapestry5.ioc.internal.RegistryImpl.invoke(RegistryImpl.java:1124)
      at org.apache.tapestry5.ioc.internal.OperationTrackingObjectCreator.createObject(OperationTrackingObjectCreator.java:49)
      at org.apache.tapestry5.ioc.internal.SingletonServiceLifecycle.createService(SingletonServiceLifecycle.java:29)
      at org.apache.tapestry5.ioc.internal.LifecycleWrappedServiceCreator.createObject(LifecycleWrappedServiceCreator.java:46)
      at org.apache.tapestry5.ioc.internal.AdvisorStackBuilder.createObject(AdvisorStackBuilder.java:63)
      at org.apache.tapestry5.ioc.internal.InterceptorStackBuilder.createObject(InterceptorStackBuilder.java:54)
      at org.apache.tapestry5.ioc.internal.RecursiveServiceCreationCheckWrapper.createObject(RecursiveServiceCreationCheckWrapper.java:60)
      at org.apache.tapestry5.ioc.internal.OperationTrackingObjectCreator$1.invoke(OperationTrackingObjectCreator.java:45)
      at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:74)
      at org.apache.tapestry5.ioc.internal.PerThreadOperationTracker.invoke(PerThreadOperationTracker.java:87)
      at org.apache.tapestry5.ioc.internal.RegistryImpl.invoke(RegistryImpl.java:1124)
      at org.apache.tapestry5.ioc.internal.OperationTrackingObjectCreator.createObject(OperationTrackingObjectCreator.java:49)
      at org.apache.tapestry5.ioc.internal.services.JustInTimeObjectCreator.obtainObjectFromCreator(JustInTimeObjectCreator.java:66)
      at org.apache.tapestry5.ioc.internal.services.JustInTimeObjectCreator.createObject(JustInTimeObjectCreator.java:54)
      at $ConfigurableWebApplicationContext_38b45053c6a2.delegate(Unknown Source)
      at $ConfigurableWebApplicationContext_38b45053c6a2.getBeanDefinitionCount(Unknown Source)
      at org.apache.tapestry5.spring.SpringModule$1.initializeApplication(SpringModule.java:61)
      at $ApplicationInitializer_38b45053c6be.initializeApplication(Unknown Source)
      at org.apache.tapestry5.services.TapestryModule$40.initializeApplication(TapestryModule.java:2438)
      at $ApplicationInitializer_38b45053c6be.initializeApplication(Unknown Source)
      at $ApplicationInitializer_38b45053c6ba.initializeApplication(Unknown Source)
      at org.apache.tapestry5.services.TapestryModule$ServletApplicationInitializerTerminator.initializeApplication(TapestryModule.java:279)
      at $ServletApplicationInitializer_38b45053c69f.initializeApplication(Unknown Source)
      at org.apache.tapestry5.TapestryFilter.init(TapestryFilter.java:116)
      at org.mortbay.jetty.servlet.FilterHolder.doStart(FilterHolder.java:97)
      at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
      at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:713)
      at org.mortbay.jetty.servlet.Context.startContext(Context.java:140)
      at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1282)
      at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:518)
      at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:499)
      at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
      at org.mortbay.jetty.handler.HandlerCollection.doStart(HandlerCollection.java:152)
      at org.mortbay.jetty.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:156)
      at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
      at org.mortbay.jetty.handler.HandlerCollection.doStart(HandlerCollection.java:152)
      at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
      at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
      at org.mortbay.jetty.Server.doStart(Server.java:224)
      at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
      at org.mortbay.xml.XmlConfiguration.main(XmlConfiguration.java:985)
      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.mortbay.start.Main.invokeMain(Main.java:194)
      at org.mortbay.start.Main.start(Main.java:534)
      at org.mortbay.start.Main.start(Main.java:441)
      at org.mortbay.start.Main.main(Main.java:119)
  </code></pre>
  
  <p>It seeems to be a problem with <code>/WEB-INF/applicationContext.xml</code>, right? Let's check it out.</p>
  
  <p><strong>/WEB-INF/applicationContext.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
  &lt;!-- Enable the Spring MVC stuff --&gt;
  &lt;mvc:annotation-driven/&gt;
  
    &lt;!-- Enable usage of @Autowired. --&gt;
    &lt;context:annotation-config/&gt;
  
    &lt;!-- Enable component scanning for defining beans with annotations. --&gt;
    &lt;context:component-scan base-package="com.viktortech.automaton.model"/&gt;
  
    &lt;!--  For translating native persistence exceptions to Spring's 
          DataAccessException hierarchy. --&gt;
  &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt;
  
  &lt;bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"&gt;
      &lt;property name="environment"&gt;
          &lt;props&gt;
              &lt;prop key="java.naming.factory.initial"&gt;org.mortbay.naming.InitialContextFactory&lt;/prop&gt;
              &lt;prop key="java.naming.provider.url"&gt;org.mortbay.naming&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
  
      &lt;property name="jndiTemplate"&gt;
        &lt;ref bean="jndiTemplate"/&gt;
      &lt;/property&gt;
      &lt;property name="jndiName"&gt;
            &lt;value&gt;jdbc/appDB&lt;/value&gt;
      &lt;/property&gt;
    &lt;/bean&gt;
  
  
  &lt;!-- ***********************************************************************
       To avoid requesting unnecessary connections (not necessary for
       MiniBank) with the Open Session in View filter, uncomment this and
       change "sessionFactory" bean (bellow) to use "dataSourceProxy" instead
       of "dataSource".
  &lt;bean id="dataSourceProxy" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"
      p:targetDataSource-ref="dataSource"/&gt;
       ******************************************************************* --&gt;
  
  &lt;!-- Hibernate Session Factory --&gt;
    &lt;bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
        p:dataSource-ref="dataSource"
        p:configLocation="classpath:/automaton-hibernate-config.xml"/&gt;
  
    &lt;!-- Transaction manager for a single Hibernate SessionFactory.  --&gt;
    &lt;bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory" /&gt;
  
  &lt;!-- Enable the configuration of transactional behavior based on
       annotations. --&gt;
    &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
    &lt;bean id="manageService"
      class="com.viktortech.automaton.model.manageservice.ManageServiceImpl"/&gt;
  
  &lt;bean id="botService"
      class="com.viktortech.automaton.model.botservice.BotServiceImpl"/&gt;
  
  &lt;context:component-scan base-package="com.viktortech.automaton.rest" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>So what's this JNDI jdbc/appDB, where it's defined and how? Right away, it's in the <code>/WEB-INF/jetty-env.xml</code> file
      
      </p>
  
  <pre><code>&lt;Configure class="org.mortbay.jetty.webapp.WebAppContext"&gt;
  
      &lt;New id="viktor" class="org.mortbay.jetty.plus.naming.Resource"&gt;
          &lt;Arg&gt;jdbc/appDB&lt;/Arg&gt;
          &lt;Arg&gt;
              &lt;New class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
                  &lt;Set name="driverClassName"&gt;com.mysql.jdbc.Driver&lt;/Set&gt;
                  &lt;Set name="url"&gt;jdbc:mysql://workingDBserver/database&lt;/Set&gt;
                  &lt;Set name="username"&gt;******&lt;/Set&gt;
                  &lt;Set name="password"&gt;******&lt;/Set&gt;
              &lt;/New&gt;
          &lt;/Arg&gt;
     &lt;/New&gt;
  &lt;/Configure&gt;
  </code></pre>
  
  <p>So that's it. As I said, it runs pretty smooth on localhost:8080, but when deploying to a remote jetty server (Same jetty version) it goes 503 and then produces that error stated above. </p>
  
  <p>Please, please, ask for any other resource you need to fix this out. Thanks in advance.</p>