0

I am trying to do the connection pooling with context.xml and web.xml.

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/project_1">

<Resource auth="Container"
    driverClassName="oracle.jdbc.driver.OracleDriver"
    maxActive="100" maxIdle="30"
    maxWait="10000"
    name="connpool"
    password="oracle11"
    type="javax.sql.DataSource"
    url="jdbc:oracle:thin:@localhost:1523:system"
    username="system"/>
</Context>

web.xml

<resource-ref>
        <description>DB Connection</description>
        <res-ref-name>connpool</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

Connection code

InitialContext initialContext = new InitialContext();
Context context = (Context) initialContext.lookup("java:comp/env");
// The JDBC Data source that we just created
DataSource ds = (DataSource) context.lookup("connpool");
this.con = ds.getConnection();

Exception and stack trace

Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at demo.main(demo.java:27)

C:\Users\BigGoal\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Hemal
  • 26
  • 1

1 Answers1

1

Hemal , The default constructor will return you the context when you run the code on the server. When you have a standalone program , the default constructor will not return the IC. You will have to look at how to get the IC , you may want to set it

Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.rmi.registry.RegistryContextFactory");
env.put(Context.PROVIDER_URL, "rmi://server:1099");
InitialContext context = new InitialContext(env);

This is referenced Tomcat book here and also in the documentation here

You may also want to refer this question here Initialcontext in a standalone Java program

Hope it helps!

Community
  • 1
  • 1
Ramachandran.A.G
  • 4,788
  • 1
  • 12
  • 24