Skip to main content

Glassfish 4.0 using jdbc/__default only

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
5 replies [Last post]
Kuzmit4
Offline
Joined: 2013-06-27

Hello All!
I'm using Netbeanse 7.3.1 + Glassfish 4. I wrote simple web application using JSTL

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>

        <sql:query var="txt" dataSource="jdbc/mrm_db">
            SELECT * FROM T2
        </sql:query>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <table>
        <c:forEach var="row" items="${txt.rows}">
            <tr><td><c:out value="${row.txt}"/></td></tr>
        </c:forEach>
        </table>
    </body>
</html>

Also i have added Galssfish JDBC Resource and configured connection pool and JDBC Resource. Added web.xml and config resource references.

And when i trying start my applicatio a get error

ype Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused."

root cause

javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused."

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.0 logs.

It's looks like that Glassfish try use jdbc/__default, but not my datasource.

If i'm create this project into Netbeans 7.2 and Glassfish 3.2, it's work fine.

Can you help me, what am i doing wrong?

Thanks!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mlaris
Offline
Joined: 2010-07-17

Have you resolved this issue? I am experiencing the same problem attempting to use PostgreSQL 9.1. I moved a (mostly) working EAR from a 3.1 installation to test out GFv4.

Mike Laris
mglaris@gmail.com

Kuzmit4
Offline
Joined: 2013-06-27

No. I have posted this issue at some forums, but it has no one answer.
I hope that the next build will work fine. Now I am creating datasourse on the jsp page and share it with scope = session

mlaris
Offline
Joined: 2010-07-17

Kuzmit,

I finally got my GF4 DataSource working. In all honesty, I do not know precisely which change did the trick, or if it was a combination of the changes that did it. Here's what I did:

1. Updated to the latest PostgreSQL Driver (9.2)
2. Changed from using the PGConnectionPoolDataSource to the PGSimpleDataSource
3. Deleted the Derby connection pool and jdbc/__default resources from my domain.xml
4. Reworked my Database getConnection() method
5. Moved my Connection Pool and JDBC Resource config into the the java:app JNDI namespace.

Here is my getConnection() method. (It is still a bit messy from the whole debugging process):

   private static String jndiName     = null;
   private static InitialContext initCtx     = null;
   private static Context        appContext  = null;
   private static DataSource   dataSource  = null;

   public static Connection getConnection() throws SQLException
   {
   Logger logger  = Logger.getLogger(DBConnection.class);
   Connection con     = null;
   Object jndiRes = null;

   try {
   //
   // Retrieve an return a connection from the JNDI connection pool.
   //
   if (initCtx == null) {
   logger.info("Creating Initial Context...");
   initCtx = new InitialContext();
   }

   //
   // Retrieve the JNDI Context for the application resources.
   //
   if (appContext == null) {
   logger.info("Looking up java:app/...");
   appContext = (Context) initCtx.lookup("java:app/");
   if (appContext == null) {
   logger.error("ERROR!!! Null ENV Context!");
   throw new SQLException("Lookup on java:app/ Failed!");
   }
   }

   //
   // Lookup the Datasource from the APPLICATION context.
   //
   if (dataSource == null) {
   logger.info(String.format("Retrieving DataSource (%s)...", jndiName));
   jndiRes = appContext.lookup(jndiName);
   if (jndiRes == null) {
   logger.error("ERROR!!! Null Context Lookup!");
   throw new SQLException("Datasource not found");
   } else {
   if (jndiRes instanceof DataSource) {
   dataSource = (DataSource)jndiRes;
   } else {
   SQLException ex = new SQLException(errorMsg);
   logger.fatal(errorMsg, ex);
   throw ex;
   }
   }
   }

   //
   // Get the connection from the DataSource
   //
   logger.info("Retrieving Connecton...");
   con = dataSource.getConnection();
   if (con == null) {
   logger.error("ERROR!!! Null Connection!\n");
   throw new SQLException("Could not get connection");
   }

   } catch (NamingException e) {
   logger.error("ERROR!!! Caught NamingException!",e);
   throw new SQLException(e.getMessage());
   } catch (SQLException sqle) {
   logger.error("ERROR!!! Caught SQLException!", sqle);
   throw sqle;
   }

   return con;

   }

I suspect deleting the default datasoure was the primary fix, but I have not yet gone back to verify.

What I thought was strange was that the getConnection() method was NOT new. I first wrote it several years ago and it has been essentially unchanged since then. I was quite surprised when the code started having problems. My suspicion is that GF4 has made changes to the JNDI lookup and naming classes, but that is just a guess.

I hope this helps.

Mike Laris
mglaris@gmail.com

Kuzmit4
Offline
Joined: 2013-06-27

Mike, Hi!

I have tried to remove __default datasource early and try again, but it does not work.
After removing datasource i'm getting new error message

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver found for jdbc/mrmdb"

root cause

javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver found for jdbc/mrmdb"

p.s. sorry for my formating message, i cannot find code block button

Seifi
Offline
Joined: 2011-07-05

After I create this file , everything worked fine

workspace/project/project-web/src/main/webapp/WEB-INF/glassfish-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
  <context-root>project</context-root>

  <resource-ref>
    <res-ref-name>jdbc/projectDB</res-ref-name>
    <jndi-name>jdbc/projectDB</jndi-name>
  </resource-ref>
  <class-loader delegate="true"/>
</glassfish-web-app>