Nidheesh
Nidheesh

Reputation: 4562

Name [services] is not bound in this Context. Unable to find [services]

I was trying to migrate a war deployed in jboss to tomcat 8. I am facing the below exception while loading my application context xml. This was working in jboss. (Had Same issue with dataSource, but it got fixed when I configured <GlobalNamingResources/> settings in context.xml.)

Application context has

..
<jee:jndi-lookup id="txnService" jndi-name="services/TxnService"
        lazy-init="true" />

<jee:jndi-lookup id="mailService" jndi-name="services/MailService"
        lazy-init="true" expected-type="com.tone.mailservice.MailService" />
..

And error:

Caused by: javax.naming.NameNotFoundException: Name [services/MailService] is not bound in this Context. Unable to find [services].

and

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txnService': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [services/TxnService] is not bound in this Context. Unable to find [services].      

I can see the TxnService class has the following code.

public abstract interface TxnService
{
  public static final String JNDI_NAME = "services/TxnService";
  ..
}

This class is inside a jar and i have that jar inside the lib folder. I have tried giving java:/comp/env/services/TxnService and java:/comp/env/services/MailService instead. But still same issue.

Full trace:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailService': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [services/MailService] is not bound in this Context. Unable to find [services].
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1482)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByName(AbstractAutowireCapableBeanFactory.java:1146)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1096)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    ... 45 more
Caused by: javax.naming.NameNotFoundException: Name [services/MailService] is not bound in this Context. Unable to find [services].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:816)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154)
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178)
    at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:104)
    at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
    at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201)
    at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
    ... 54 more
Related cause:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txnService': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [services/TxnService] is not bound in this Context. Unable to find [services].
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1482)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getTypeForFactoryBean(AbstractBeanFactory.java:1368)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:720)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:523)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:356)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:334)
    at org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:187)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:897)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:795)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:196)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1045)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:949)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:989)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4940)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5250)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:952)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1823)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.naming.NameNotFoundException: Name [services/TxnService] is not bound in this Context. Unable to find [services].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:816)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154)
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178)
    at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
    at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
    at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201)
    at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
    ... 51 more

Upvotes: 2

Views: 10798

Answers (1)

SkyWalker
SkyWalker

Reputation: 29168

Your problem seems to be a JNDI lookup related issue.

Actually, I am trying to provide a fulfill procedure for JBoss to Tomcat migration. Here I have not given the specific answer. Please follow the procedure. After that, if any issue arise, we would start further discussion.

There are mainly 3 areas need to be considered for migration between jboss and tomcat8.

  1. Common libraries,
  2. Data source configutation,
  3. JNDI bindings and basic server configuration

Common libraries:

For Tomcat:

Apache Tomcat stores the common libraries into the CATALINE_HOME/lib folder.

For JBoss:

JBoss AS release ==== Path for common libs
4.x ================= JBOSS_HOME/server/[server-name]/lib
5.x - 6.x============ JBOSS_HOME/common/lib
7.x ================= JBOSS_HOME/modules

Resource Link:

http://www.mastertheboss.com/jboss-as-7/how-to-install-a-module-on-jboss-as-7


Data Source Configuration

If you use Oracle as database then

For Tomcat

A Datasource is configured in the context.xml file of Tomcat:

<Resource name="jdbc/oracledb"
    auth="Container"
    type="javax.sql.DataSource"
    username="scott"
    password="tiger"
    driverClassName="oracle.jdbc.driver.OracleDriver"
    url="jdbc:oracle:thin:@localhost:1521:XE"
    maxActive="10"
    maxIdle="2"
    removeAbandoned="true"
    removeAbandonedTimeout="30"
    maxWait="5000"
    logAbandoned="true"
    accessToUnderlyingConnectionAllowed="true"/>

then, you should add your JDBC driver into CATALINA_HOME/lib folder

For JBoss:

JBoss AS release ======== Path for data source
4.x - 5.x - 6.x  ======== datasource-ds.xml file into JBOSS_HOME/server/[server-name]/deploy
7.x ===================== datasource-ds.xml file into JBOSS_HOME/standalone/deployments or as a module into JBOSS_HOME/modules

JNDI bindings and basic server configuration

In JNDI binding, code related changing should be required in tomcat. JNDI binding can be done in 2 ways.

  1. Using lookup code
  2. Using the @Resource annotation (javax.annotation.Resource) instead of the lookup code

Using lookup code We can look up the configured JNDI DataSource using Java code as follows:

Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/UsersDB");
Connection conn = ds.getConnection();

After obtaining the connection, we can use it as trivial JDBC code:

And then

Statement statement = conn.createStatement();
String sql = "select username, email from users";
ResultSet rs = statement.executeQuery(sql);
// iterates over the result set...

Using the @Resource annotation (javax.annotation.Resource) instead of the lookup code

It needs to declare a field called dataSource in the servlet like this:

@Resource(name = "jdbc/UsersDB")
private DataSource dataSource;

Tomcat will look up the specified resource name and inject an actual implementation when it discovers this annotation.

For full source code with example, you can go through this tutorial: Configuring JNDI DataSource for Database Connection Pooling in Tomcat

For JBoss:

when using JBoss AS 7 you have to choose a JNDI location like java:/ or java:/jboss for your datasources in order to be accepted. The portable approach for defining data sources is to use a resource reference.

In our example, we need to define into your web.xml for your connection pool:

DB Connection jdbc/oracledb javax.sql.DataSource Container

Alternatively, starting in Java EE 5 (Servlet 2.5), this can be done even easier within your code using the @Resource annotation.

public class MyServlet extends HttpServlet {

    @Resource(name = "jdbc/oracledb")
    private DataSource dataSource;

Then on the JBoss side define in your jboss-web.xml

<jboss-web>
 <resource-ref>
 <res-ref-name>jdbc/oracledb</res-ref-name>
 <jndi-name>java:jboss/datasources/jdbc/oracledb</jndi-name>
 </resource-ref>
</jboss-web>

Fine, now your JNDI lookup will work in either environment, without changing one line of code!


Server Configuration

by default both Tomcat and JBoss are delivering http applications on port 8080, chances are that you might need configuring the http port. Here's tomcat's server.xml

<Connector port="32080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />

Resource Link:

  1. Tomcat DataSource JNDI Example in Java
  2. Tomcat to JBoss migration

Upvotes: 3

Related Questions