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