javax.naming.NoInitialContextException Tomcat DataSource JNDI intellij idea 14

2.4k views Asked by At

I am trying go get Connection Pool with Tomcat DataSource and MySQL in ntellij idea 14. I've done these steps:

Select "Project Structure" from the File menu

From there, select the "Facets" option. Make sure that you have a Web facet configured. If not, add one.

Once the web facet is added, select "Add Application Server specific descriptor..."

Select Tomcat Context Descriptor from the options and click OK.

I got META-INF/context.xml in web directory. I added next lines in context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
        <Resource name="jdbc/payments"
                  auth="Container"
                  type="javax.sql.DataSource"
                  driverClassName="com.mysql.jdbc.Driver"
                  url="jdbc:mysql://localhost:3306/paymentsdb"
                  username="root"
                  password="password"

                  maxActive="100"
                  maxIdle="20"
                  minIdle="5"
                  maxWait="10000"/>
</Context>

And this into web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <!--DB Connection start-->
    <description>MySQL Test App</description>
    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/payments</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

This is my method to get connection

public static Connection createConnection(){
        Context context = null;
        DataSource dataSource = null;
        Connection connection = null;
        try {
            context = (Context) new InitialContext().lookup("java:comp/env");
            dataSource = (DataSource) context.lookup("jdbc/payments");
            connection = dataSource.getConnection();
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

I got this exception in line

dataSource = (DataSource) context.lookup("java:comp/env/jdbc/payments");

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 ua.epam.kuts.dao.MySQLDAOFactory.createConnection(MySQLDAOFactory.java:22)
    at Test.main(Test.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

I use Tomcat 8.0.23. Any idea? I googled for several hours and didn't find anything that helped me. Drew me mad. I checked connection with Statment and it works. I have connector.jar.

1

There are 1 answers

0
Oleg Kuts On

I've done stupid mistake. I was trying to test DataSource connection running it in public static main(String[] args) method instead of running it on the server. That's why InitialContext was not initialized. Found answer here in second post.