Site Navigation

Mar 11, 2015

GlassFish 4 Reports a JNDI Lookup Exception for a NetBeans Project

Duke Waving

Here is a quick word around to a problem I ran into at work.

Problem: Calling a Java Servlet with JNDI code results in this error

Other Exception: Lookup failed for 'jdbc/yournamehere' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}

The initialization code is similar to the following:

    try {
        Context ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("jdbc/yournamehere");
        Connection con = ds.getConnection();

You have setup a connection pool and JDBC resource for your NetBeans project. All the configuration data appears to be correct.

Solution

The problem occurs when GlassFish fails to create a JDBC Connection Pool and JDBC Resource on the server when your application deploys. To wordaround the problem, manually create the pool and resource on the Glassfish server using the admin console.

Creating the JDBC Resource is pretty easy, so no additional comments on that.

When you create the JDBC Connection Pool, only include the required properties for your database in the Additional Properties tab. In this case, JavaDB (Derby) was used so the only required properties are:

  • User: YourUsername
  • Password: YourPassword
  • DatabaseName: YourDBName
  • ServerName: localhost
  • PortNumber: 1527

Some of the automatically added properties may cause your connection pool to fail to connect. Use the Ping feature of GlassFish to ensure the server is connecting to the database.

Post a Comment

Favorite Links Feed