Announcement Announcement Module
Collapse
No announcement yet.
Need Help in JNDI Datasource using tomcat and spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need Help in JNDI Datasource using tomcat and spring

    I am trying to connect my local database using JNDI datasource in tomcat and spring

    I have done the below configuration in Tomcat 7 server:

    In tomcat server.xml (Path: E:\apache-tomcat-7.0.35\conf) I added the below configuration

    <Resource name="UserDatabase" auth="Container"
    type="org.apache.catalina.UserDatabase"
    description="User database that can be updated and saved"
    factory="org.apache.catalina.users.MemoryUserDatab aseFactory"
    pathname="conf/tomcat-users.xml" />

    <Resource name="jdbc/myApp" auth="Container"
    type="javax.sql.DataSource"
    driverClassName="oracle.jdbc.OracleDriver"
    url="jdbc:oracle:thin:hr@localhost:1521:XE>"
    username="testUser" password="password"
    maxActive="10" maxIdle="5"
    maxWait="60000" removeAbandoned="true"
    removeAbandonedTimeout="60" logAbandoned="true"/>


    In web.xml, I have added the below config:

    <resource-ref>
    <description>Oracle Spring JNDI Datasource</description>
    <res-ref-name>jdbc/myApp</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>


    And in my Spring context xml (ws-context.xml), added below config:

    <bean name="myDataSourceInJndi" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName" value="java:comp/env/jdbc/myApp"/>
    </bean>

    <jee:jndi-lookup id="dataSource" jndi-name="/jdbc/myApp" resource-ref="true"/>


    In my Java code, I am trying the below code to connect DataBase:

    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    DataSource ds = (DataSource) envCtx.lookup("jdbc/myApp");
    ds.getConnection();


    But I am getting the below exception while executing ****ds.getConnection();***


    java.lang.IllegalStateException: Connection factory returned null from createConnection
    at org.apache.tomcat.dbcp.dbcp.PoolableConnectionFact ory.makeObject(PoolableConnectionFactory.java:584)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.valida teConnectionFactory(BasicDataSource.java:1556)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.create PoolableConnectionFactory(BasicDataSource.java:154 5)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.create DataSource(BasicDataSource.java:1388)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getCon nection(BasicDataSource.java:1044)
    at com.wipro.spring.impl.EmployeeImpl.getDetails(Empl oyeeImpl.java:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.xml.ws.api.server.InstanceResolver$1.invok e(InstanceResolver.java:246)
    at com.sun.xml.ws.server.InvokerTube$2.invoke(Invoker Tube.java:146)
    at com.sun.xml.ws.server.sei.EndpointMethodHandler.in voke(EndpointMethodHandler.java:257)
    at com.sun.xml.ws.server.sei.SEIInvokerTube.processRe quest(SEIInvokerTube.java:93)
    at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:5 98)
    at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:55 7)
    at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:542 )
    at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:4 39)
    at com.sun.xml.ws.server.WSEndpointImpl$2.process(WSE ndpointImpl.java:243)
    at com.sun.xml.ws.transport.http.HttpAdapter$HttpTool kit.handle(HttpAdapter.java:471)
    at com.sun.xml.ws.transport.http.HttpAdapter.handle(H ttpAdapter.java:244)
    at com.sun.xml.ws.transport.http.servlet.ServletAdapt er.handle(ServletAdapter.java:135)
    at com.sun.xml.ws.transport.http.servlet.WSServletDel egate.doGet(WSServletDelegate.java:129)
    at com.sun.xml.ws.transport.http.servlet.WSServletDel egate.doPost(WSServletDelegate.java:160)
    at com.sun.xml.ws.transport.http.servlet.WSSpringServ let.doPost(WSSpringServlet.java:52)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:728)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBas e.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(A ccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.p rocess(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnect ionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProce ssor.run(AprEndpoint.java:1822)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run Task(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)


    Can you please me to resolve this issue? Am I missing any configuration or any mistake in my java code please suggest your suggestions.


    Thanks

  • #2
    Why...

    Code:
    <bean name="myDataSourceInJndi" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName" value="java:comp/env/jdbc/myApp"/>
    </bean>
    is the same as
    Code:
    <jee:jndi-lookup id="dataSource" jndi-name="/jdbc/myApp" resource-ref="true"/>
    So basically that is duplicate... However both statements are useless because you are doing a jndi lookup in code.

    Code:
    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    DataSource ds = (DataSource) envCtx.lookup("jdbc/myApp");
    ds.getConnection();
    So that is rendering your duplicate statements useless.

    However JNDI seems correct, at least the lookup. The exception is from the internals of the pool so it might be a class or configuration problem of your datasource.

    Comment


    • #3
      Thank you very much. I removed the spring config and I had problem with my database configuration, after fixing the DB config its working fine now.

      Comment

      Working...
      X