简体   繁体   中英

JDBC Connection Pool setup not working on Glassfish5

I am currently new to the Glassfish application server, I have been following various tutorials on line to setup a JDBC connection pool on the server using the admin interface.

I can see the created connection pool is listed along with the two connection pools created by default on the server as shown in the screenshot below

在此处输入图片说明

I pinged this setup to be sure all is fine, However when I try to obtain a connection to the database from the my code listing below

public static Connection getDatabaseConnection()
{
    Connection con = null;
    try {
        Context initialContext = new InitialContext();

        if ( initialContext == null){
            System.out.println("JNDI problem. Cannot get InitialContext.");
        }
        DataSource datasource = (DataSource)initialContext.lookup("AppDb");
        if (datasource != null) {
            con = datasource.getConnection();
        }
        else {
            System.out.println("Failed to lookup datasource.");
        }

    }
    catch(Exception ex)
    {
        System.out.println("error looking up connection");
        ex.printStackTrace();
    }


    return con;
   }

but I get the following error below stating that connection is not found

javax.naming.NamingException: Lookup failed for 'AppDb' 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} [Root exception is javax.naming.NameNotFoundException: AppDb not found]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at com.keystone.test.User.getDatabaseConnection(User.java:156)

Creating a JDBC connection pool is not enough, you should :

1) In the "JDBC Resources" section, create a resource that uses your pool

2) Give a JNDI name to that resource

3) Lookup that name in your code

(cf. Payara/GlassFish DataSource JNDI Lookup Reference and JDBC Connection Pool for GlassFish and Payara Java Application Servers )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM