• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

javax.naming.NameNotFoundException: in context "java:comp/env"

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have developed a servlet based application (war) on tomcat, which I am trying to port to WAS. I am getting following Exception:

java.sql.SQLException: Error looking up data source for name: jdbc/MySQLDB
Caused by: javax.naming.NameNotFoundException: Name jdbc not found in context "java:comp/env".
at com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInternal(NameSpace.java:1767)
at com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1083)
at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:991)
at com.ibm.ws.naming.urlbase.UrlContextImpl.lookup(UrlContextImpl.java:1263)
at com.ibm.ws.naming.java.javaURLContextImpl.lookup(javaURLContextImpl.java:384)
at com.ibm.ws.naming.urlbase.UrlContextImpl.lookup(UrlContextImpl.java:1307)

Here is the details about application:

Flow OF Execution is like 1. JMS Broker Servlet Context Listener starts the ActiveMQ 2. JMS client gets initialized
from StartUpServlet 3. Servlet trying to lookup DataSource throws java.sql.SQLException: Error looking up data
source for name: jdbc/MySQLDB. If StartUpServlet gets commented out from web.xml then lookup for DataSource works fine.
But I need both of them to work together.

WEB-INF/web.xml
---------------
<listener-class>com.myservlet.StartJMSBrokerServletContextListener</listener-class>
........................................
<servlet>
<servlet-name>StartUpServlet</servlet-name>
<load-on-startup>1</load-on-startup>
<servlet>
........................................

StartUpServlet.java
-------------------
public class StartUpServlet extends GenericServlet
{
private Context jndiContext;

public void init() throws ServletException
{
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.setProperty(Context.PROVIDER_URL, "tcp://localhost:61616");
jndiContext = new InitialContext(props);

QueueConnectionFactory queueConnectionFactory =
(QueueConnectionFactory) jndiContext.lookup("queueConnectionFactory");
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
Queue queue = (Queue) jndiContext.lookup("MyQueue");

........................................
}
}
META-INF/context.xml
--------------------
<Context debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/MySQLDB"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test?autoReconnect=true"
maxActive="50" initialSize="5" />
</Context>

DataManager.java
---------------
Context context = (Context) new InitialContext().lookup("java:comp/env");
javax.sql.DataSource ds =(javax.sql.DataSource) context.lookup("jdbc/MySQLDB");
Connection sqlConnection = ds.getConnection() ;
statement = sqlConnection.prepareStatement(queryString);;


WEB-INF/Classes/jndi.properties
-------------------------------
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url = tcp://localhost:61616
onnectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactory
queue.MyQueue = MyQueue
queue.MyResponseQueue = MyResponseQueue

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic