Announcement Announcement Module
Collapse
No announcement yet.
Error in configuring JDBC Datasource Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error in configuring JDBC Datasource

    I am using Springsource Tool Suite 2.5.1 and tc server 2.1. I am trying to configure a data source to PostgresSql.

    I followed the steps outlined in http://tomcat.apache.org/tomcat-6.0-...tml#PostgreSQL.

    1. I tried to add data source resource in $CATALINA_BASE/conf/Catalina/localhost/<myappcontextfilename>.xml file similar to the one below. Unfortunately, Springtool suite keeps overwriting this file and the <Resource name...> definition given is lost. Springsuite overwrites it with <Context docBase.... source=..../>

    Code:
                <Context docBase="C:\springsource\tc-server-developer-2.0.4.RELEASE\spring-insight-instance\wtpwebapps\myapp" path="/myapp" reloadable="true" source="org.eclipse.jst.jee.server:myapp">
                    <Resource name="jdbc/postgres" auth="Container" type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
                        url="jdbc:postgresql://localhost:5432/myapp" username="postgres" password="password" maxActive="20" 
                        maxIdle="10" maxWait="-1"/>
                </Context>
    2. Then I attempted to define it as a global resource though the Apache link warns that it some time does not work. The definition is given below.

    Code:
        <GlobalNamingResources>
            <Resource auth="Container" driverClassName="org.postgresql.Driver" maxActive="20" maxIdle="10" maxWait="-1" name="jdbc/postgres" password="password" type="javax.sql.DataSource" url="jdbc:postgresql://localhost:5432/myapp" username="postgres"/>            
        </GlobalNamingResources>
    3. Then I add the resource reference in web.xml as below.

    Code:
      <resource-ref>
        <description>postgreSQL Datasource example</description>
        <res-ref-name>jdbc/postgres</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
      </resource-ref>
    4. But when the spring tc server comes up, it gives the below error.

    Code:
    Nov 13, 2010 10:42:09 PM org.apache.naming.NamingContext lookup
    WARNING: Unexpected exception resolving reference
    java.sql.SQLException
    	at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:201)
    	at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:150)
    	at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:619)
    	at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:562)
    	at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:425)
    	at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:130)
    	at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:93)
    	at org.apache.tomcat.jdbc.pool.DataSourceFactory.createDataSource(DataSourceFactory.java:490)
    	at org.apache.tomcat.jdbc.pool.DataSourceFactory.getObjectInstance(DataSourceFactory.java:219)
    	at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:140)
    	at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:793)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
    	at org.apache.catalina.core.NamingContextListener.addResource(NamingContextListener.java:1020)
    	at org.apache.catalina.core.NamingContextListener.createNamingContext(NamingContextListener.java:629)
    	at org.apache.catalina.core.NamingContextListener.lifecycleEvent(NamingContextListener.java:237)
    	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
    	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4540)
    	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
    	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
    	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:568)
    	at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:637)
    	at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:563)
    	at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:498)
    	at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1282)
    	at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:321)
    	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    	at org.apache.catalina.core.StandardHost.start(StandardHost.java:807)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
    	at org.apache.catalina.core.StandardService.start(StandardService.java:519)
    	at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
    Caused by: java.lang.NullPointerException
    	at java.lang.Class.forName0(Native Method)
    	at java.lang.Class.forName(Class.java:247)
    	at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:195)
    	... 41 more
    Nov 13, 2010 10:42:09 PM org.apache.catalina.core.NamingContextListener addResource
    WARNING: Failed to register in JMX: javax.naming.NamingException
    Nov 13, 2010 10:42:11 PM org.apache.catalina.core.ApplicationContext log
    5. Finally I configured the data source using Data Source Explorer. I set-up with the same parameters as given above in <GlobalNamingResources>. When I click on Test Connection, I get Ping Successful message. When I expand the 'jdbc/postgres' connection in Data Source Explorer, I am able to see myapp database.

    6. But when I use the below code snippet to obtain data source, I still get the error.

    Code:
    		System.out.println("before initialcontext");
    		InitialContext ctx = null; 
    		DataSource ds = null;
    		try {
    			ctx = new InitialContext();
    			System.out.println("after initialcontext");
    			if ( ctx == null ) {
    				   System.out.println("Uh oh -- no context!");
    			} else {
    					System.out.println("Context not null");
    			}			
    			
    			ds = (DataSource) ctx.lookup( "java:/comp/env/jdbc/postgres" );			
    		} catch (NamingException nae) {
    			System.out.println("NAE Message = " + nae.getMessage());
    			nae.printStackTrace();
    		}
    		
    		if ( ds == null ) {
    		   System.out.println("Data source not found!");
    		} else {
    			System.out.println("Data source found!");
    		}
    I get the below error.

    Code:
    before initialcontext
    after initialcontext
    Context not null
    NAE Message = null
    javax.naming.NamingException
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:805)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
    	at org.apache.naming.SelectorContext.lookup(SelectorContext.java:152)
    	at javax.naming.InitialContext.lookup(InitialContext.java:392)
    	at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    
    ......
    Data source not found!
    Please Help! I have been struggling with this for almost 3 days . Thanks in advance for all your help
    Last edited by j123; Nov 14th, 2010, 09:41 AM.

  • #2
    Some more debugging info

    I tried to debug using the below code. To obtain list of all context, I used the below code. Please review and help

    Code:
    		    System.out.println("Before Context list");
    			Context ctx1 = (Context) new InitialContext().lookup("java:comp/env");
    			listContext(ctx1, "");
    		    System.out.println("After Context list");
    
    
    	/**
    	* Recursively exhaust the JNDI tree
    	*/
    	private static final void listContext(Context ctx, String indent) {
    		try {
    		   NamingEnumeration list = ctx.listBindings("");
    		   while (list.hasMore()) {
    		       Binding item = (Binding) list.next();
    		       String className = item.getClassName();
    		       String name = item.getName();
    		       System.out.println(indent + className + " " + name);
    		       Object o = item.getObject();
    		       if (o instanceof javax.naming.Context) {
    		    listContext((Context) o, indent + " ");
    		       }
    		   }
    		} catch (NamingException ex) {
    		   System.out.println("JNDI failure: " + ex);
    		   ex.printStackTrace();
    		}
    	}
    I get the below error.

    Code:
    before initialcontext
    Before Context list
    org.apache.naming.NamingContext jdbc
    org.apache.naming.NamingContext jdbc
    JNDI failure: javax.naming.NamingException
    javax.naming.NamingException
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:805)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
    	at org.apache.naming.NamingContextBindingsEnumeration.nextElementInternal(NamingContextBindingsEnumeration.java:113)
    	at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:71)
    	at com.mybuildingspace.mybuilding.service.delivery.DeliveryServiceImpl.listContext(DeliveryServiceImpl.java:72)
    	at com.mybuildingspace.mybuilding.service.delivery.DeliveryServiceImpl.listContext(DeliveryServiceImpl.java:78)
    	at com.mybuildingspace.mybuilding.service.delivery.DeliveryServiceImpl.getDeliveryList(DeliveryServiceImpl.java:38)
    	at com.mybuildingspace.mybuilding.web.delivery.DeliveryJqgridDataController.handleRequest(DeliveryJqgridDataController.java:27)
    	at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)

    Comment


    • #3
      Define a global pool

      When you define global resources, specify a resource link to tie it all together.
      Otherwise it tries to create a pool without attributes.

      {code}
      <GlobalNamingResources>
      <Resource auth="Container" driverClassName="org.postgresql.Driver" maxActive="20" maxIdle="10" maxWait="-1" name="jdbc/postgres" password="password" type="javax.sql.DataSource" url="jdbcostgresql://localhost:5432/myapp" username="postgres"/>
      </GlobalNamingResources>
      {code}

      In your context, you need to specify a ResourceLink such as
      {code}
      <Context....>
      <ResourceLink name="jdbc/postgres" global="jdbc/postgres" type="javax.sql.DataSource"/>
      </Context>
      {code}

      Comment

      Working...
      X