0
votes

I have created a JDBC Connection Pool in Glassfish and use it as a JDBC Resource "jdbc/__default". I can ping this connection pool successfully in the Administration Console interface.

Then, I am trying to run the following program:

package de.java2enterprise.onlineshop;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.annotation.Resource;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

@WebServlet("/test")
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    // @Resource(name="jdbc/__default")
    // private DataSource ds;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PrintWriter writer = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");
        writer.println("<!DOCTYPE html>");
        writer.println("<html><body>");

        Connection con = null;
        DataSource ds;
        try {

            ds = (DataSource) InitialContext.doLookup("jdbc/__default");
            con = ds.getConnection();

            if (con.isValid(10)) {
                writer.println("<BR>Connected!");
            }
            con.close();

        } catch (Exception ex) {
            writer.println(ex.getMessage());
        } finally {
            if (con != null){
                try{
                    con.close();
                } catch (Exception ex){

                }
            }
        }
        writer.println("<BR>Test finished!</body></html>");
        writer.close();
    }
}

However, then I receive the following error in the web browser:

Error in allocating a connection. Cause: Connection could not be allocated because: Listener refused the connection with the following error: ORA-12518, TNS:listener could not hand off client connection Test finished!

In the server log, I get the following error:

2015-04-08T08:15:20.540+0200|Information: Loading application [onlineshop#onlineshop-war.war] at [onlineshop-war] 2015-04-08T08:15:20.636+0200|Information: onlineshop was successfully deployed in 611 milliseconds. 2015-04-08T08:15:38.552+0200|Warnung: Context path from ServletContext: /onlineshop-war differs from path from bundle: onlineshop-war 2015-04-08T08:15:38.802+0200|Information: visiting unvisited references 2015-04-08T08:15:39.519+0200|Information: Successfully got INSTRUMENTATION: sun.instrument.InstrumentationImpl@2d618042

2015-04-08T08:15:41.471+0200|Warnung: RAR5038:Unexpected exception while creating resource for pool Onlineshop. Exception : javax.resource.spi.ResourceAllocationException: Connection could not be allocated because: Listener refused the connection with the following error: ORA-12516, TNS:listener could not find available handler with matching protocol stack

2015-04-08T08:15:41.471+0200|Warnung: RAR5117 : Failed to obtain/create connection from connection pool [ Onlineshop ]. Reason : com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: Listener refused the connection with the following error: ORA-12516, TNS:listener could not find available handler with matching protocol stack

2015-04-08T08:15:41.486+0200|Warnung: RAR5114 : Error allocating connection : [Error in allocating a connection. Cause: Connection could not be allocated because: Listener refused the connection with the following error: ORA-12516, TNS:listener could not find available handler with matching protocol stack ]

After that, I can not ping the connection pool again and receive more or less the same error in the Administration Console.

I use the following configuration: JDK 1.8.0_25, GlassFish Server Open Source Edition 4.1 (build 13), Oracle Database XE 11.2.

In Glassfish administration console, I've created the following JDBC Resource: JNDI Name: jdbc/__default, Logical JNDI Name: java:comp/DefaultDataSource, Connection Pool: Onlineshop. The JDBC Connection Pool Name is Onlineshop, Resource Type: javax.sql.DataSource, Classname: oracle.jdbc.pool.OracleDataSource.

I've a minimal pool size of 800 connections, maximum of 3200 connections. The other parameters I've kept as they were, I've only added user and password at JDBC Connection Pool Properties.

Anyone an idea? :-)

1

1 Answers

2
votes

Close your connection in a finally block or in try-with-resources if you use jdk7+ as it should be done in Java. See if it helps