Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Connection pool and Datasource Implementation Is Not Functioning

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Connection pool and Datasource Implementation Is Not Functioning

    Good Evening,

    I am literally about to pull my hair out.

    Here is my problem:

    The web application that I created had been functioning correctly up until the point that I changed the Connection object from a DriverManager to a DataSource. I am 100% positive that the problem has to do with how the datasource is set-up in my servlet class and/or how the listener structured in the web.xml file.

    I have been at this for several hours and I have exhausted all of different ways to debug this problem. It seems to be fairly simple issue to resolve, but I cannot see what the problem really is.

    Here are my steps took to create the problem.

    1.Initially in my code, there is a servlet class named NewsFeedServlet.java. This servlet is responsible for collecting the news feeds from the news.rss xml file and to send it back to the jsp file named home.jsp to display the information.

    2. I used a DriverManager to get the news feeds from the MySQL database. Here is the sample of the code:

    Connection connection = DriverManager.getConnection(
                        "jdbc:mysql://localhost/publisher", "publisher",
                        "publisher");
    3. I deployed the web application and had not errors. I was able to type in the url and see the link to the news feeds.

    4. I modified the NewsFeedServlet.java to change the DriverManager to a DataSource is a can use a Connection Pool. Here is a sample of what included in and modified:

    private static DataSource dataSource;
     
    Connection connection = dataSource.getConnection();
     
    public static void setDataSource(DataSource dataSource)
    {
        NewsFeedServlet.dataSource = dataSource;
    }

    5. Added a class named Init.java that implements ServletContentListener

    package publisher.web;
     
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.sql.DataSource;
     
    import org.apache.log4j.Logger;
     
    public class Init implements ServletContextListener {
     
        private Logger logger = Logger.getLogger(this.getClass());
     
        public void contextDestroyed(ServletContextEvent sce) {
        }
     
        private void contextInitialized2(ServletContext servletContext)
        throws Exception {
           InitialContext enc = new InitialContext();
           Context compContext = (Context) enc.lookup("java:comp/env");
           DataSource dataSource = (DataSource) compContext.lookup("dataSource");
           NewsFeedServlet.setDataSource(dataSource);
        }
     
        public void contextInitialized(ServletContextEvent sce) {
            ServletContext servletContext = sce.getServletContext();
            try {
               contextInitialized2(servletContext);
            }
            catch (Exception e)
            {
               logger.error("Initialization failed.", e);
               throw new RuntimeException(e);
            }
            logger.debug("Initialization succeeded.");
        }
    }


    6. Added a <listener > element to the web.xml file

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <servlet>
          <servlet-name>news-feed</servlet-name>
          <servlet-class>publisher.web.NewsFeedServlet</servlet-class>
       </servlet>
       <servlet-mapping>
          <servlet-name>news-feed</servlet-name>
          <url-pattern>/news.rss</url-pattern>
       </servlet-mapping>
       <listener>
       <listener-class>publisher.web.Init</listener-class>
    </listener>
    </web-app>

    7. Added a file publisher.xml to configure Tomcat to create a datasource

    <Context path="/publisher" docBase="C:/TestWebApps/publisher/web">
      <Resource name="datasource" 
                type="javax.sql.DataSource"
                auth="Container"
                maxActive="10"
                maxIdle="3" 
                maxWait="10000"
                username="publisher" 
                password="publisher" 
                driverClassName="com.mysql.jdbc.Driver"
                url="jdbc:mysql://localhost:3306/publisher?autoReconnect=true" />
    </Context>

    And the completes my steps. Now the error message I am receiving after I try to deploy the web application:

    java.io.FileNotFoundException: http://localhost:8080/publisher/web/news.rss
    	sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    	com.sun.syndication.io.XmlReader.<init>(XmlReader.java:237)
    	com.sun.syndication.io.XmlReader.<init>(XmlReader.java:213)
    	website1.web.HomeServlet.doGet(HomeServlet.java:44)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    This seems to be telling me that file news.rss file cannot be found. To repeat myself again, before I made the changes, I did not any errors. But as soon as a changed the necessary changes to configure Tomcat to use the DataSource instead of DriverManager, I received these error message.

    Please provide me with any thoughts of how I can modify my code. I have attached the files for your review

    Thanks in advance for you help. I truly appreciate it.
    Attached Files Attached Files
    Last edited by mychickbad; July 14th, 2011 at 06:22 PM.


Similar Threads

  1. Problem Configuring Connection Pool, DataSource, with MySQL, Tomcat 6
    By eboraks in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: July 11th, 2011, 10:50 AM
  2. Replies: 0
    Last Post: March 26th, 2011, 11:07 AM
  3. Replies: 0
    Last Post: January 12th, 2011, 08:41 AM
  4. thread pool question
    By balexios in forum Threads
    Replies: 3
    Last Post: October 24th, 2010, 03:47 AM
  5. Testing the DataSource with MySQL
    By srinivasan_253642 in forum JDBC & Databases
    Replies: 0
    Last Post: January 9th, 2010, 02:23 AM