Robert (and all),

I will work on answers your various questions.  However, I decided to first 
explore the comment (by someone) that a servlet can "swallow" an exception, 
which I take to mean that it can throw an exception without reporting the 
exception or terminating the program.  

I have this system running on a PC identified as localhost. The test URL is:

http://localhost:8080/HolidaysRESTJSON-1.0-SNAPSHOT/webresources/holidaysandevents/countries
 

I ran two tests.  First, I wrote in code to throw an SQL exception, which did 
get caught in my catch clause, which printed out some messages and a stack 
trace.  The web browser showed the retrieved list of countries, but nothing 
else.  

Second, I replaced the line that throws the SQL exception by one that tries to 
do a division by zero.  This of course was not caught, but it did print out a 
stack trace and reported a 505 error in the browser, and the program did 
terminate.  

I take this to mean that exceptions are not being "swallowed" by my program.  
When/if an exception occurs, there is definitely some indication of this, 
either in the server.log or the browser.  Because I have never seen either of 
these actions, I'm fairly sure that my program is not throwing exceptions and 
all database connections are being closed immediately after they are used, 
i.e., no memory leaks in this respect.  

The actual code fragments and outputs are copied below.  

----------------------------------------------------------------
The code that throws the SQL exception
---------------------------------------------------------------- 
    @GET
    @Path("/countries")
    @Produces(MediaType.APPLICATION_JSON)
    public String getJsonCountries() {
        if (TempDataStorage.countryList == null) {
            Connection connection = null;
            try {
                connection = dataSource.getConnection();
                System.out.println("countries connection has been opened");
                TempDataStorage.countryList = GetCountryList.doIt(connection);
                throw new SQLException("testing sql exception");
//                connection.close();
//                System.out.println("countries connection has been closed");
            } catch (SQLException e) {
                System.out.println(e);
                System.out.println("catching sql exception");
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException ex) {
                        System.out.println(ex);
                    }
                }
            }
        }

------------------------------------------------------------------
The output in the server.log file
------------------------------------------------------------------
[2025-08-14T23:34:42.019-0400] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=40 
_ThreadName=Thread-8] [timeMillis: 1755228882019] [levelValue: 800] [[
  countries connection has been opened]]

[2025-08-14T23:34:42.022-0400] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=40 
_ThreadName=Thread-8] [timeMillis: 1755228882022] [levelValue: 800] [[
  java.sql.SQLException: testing sql exception
        at 
com.worldholidaysandevents.restjsonwebservice.HolidaysRESTJSONResource.getJsonCountries(HolidaysRESTJSONResource.java:54)

        ... stack trace ...

[2025-08-14T23:34:42.022-0400] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=40 
_ThreadName=Thread-8] [timeMillis: 1755228882022] [levelValue: 800] [[
  catching sql exception]]

----------------------------------------------------------------
The code that divides by zero
---------------------------------------------------------------- 
    @GET
    @Path("/countries")
    @Produces(MediaType.APPLICATION_JSON)
    public String getJsonCountries() {
        if (TempDataStorage.countryList == null) {
            Connection connection = null;
            try {
                connection = dataSource.getConnection();
                System.out.println("countries connection has been opened");
                TempDataStorage.countryList = GetCountryList.doIt(connection);
                float something = 1/0;
                connection.close();
                System.out.println("countries connection has been closed");
            } catch (SQLException e) {
                System.out.println(e);
                System.out.println("catching sql exception");
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException ex) {
                        System.out.println(ex);
                    }
                }
            }
        }

-----------------------------------------------------------------------
The output in the server.log file
-----------------------------------------------------------------------
[2025-08-14T23:45:29.324-0400] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=36 
_ThreadName=Thread-8] [timeMillis: 1755229529324] [levelValue: 800] [[
  countries connection has been opened]]

[2025-08-14T23:45:29.328-0400] [glassfish 4.1] [WARNING] [] 
[javax.enterprise.web] [tid: _ThreadID=36 _ThreadName=http-listener-1(1)] 
[timeMillis: 1755229529328] [levelValue: 900] [[
  
StandardWrapperValve[com.worldholidaysandevents.restjsonwebservice.ApplicationConfig]:
 Servlet.service() for servlet 
com.worldholidaysandevents.restjsonwebservice.ApplicationConfig threw exception
java.lang.ArithmeticException: / by zero
        at 
com.worldholidaysandevents.restjsonwebservice.HolidaysRESTJSONResource.getJsonCountries(HolidaysRESTJSONResource.java:53)

... stack trace ...

------------------------------------------------------------------------
The browser showed the page:
------------------------------------------------------------------------
HTTP Status 500 - Internal Server Error
type Exception report

messageInternal Server Error

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

exception

javax.servlet.ServletException: java.lang.ArithmeticException: / by zero
root cause

java.lang.ArithmeticException: / by zero
note The full stack traces of the exception and its root causes are available 
in the GlassFish Server Open Source Edition 4.1 logs.

GlassFish Server Open Source Edition 4.1
-----------------------------------------------------------------------

-----Original Message-----
From: Robert Turner <rtur...@e-djuster.ca.INVALID> 
Sent: Thursday, August 14, 2025 6:15 PM
To: Tomcat Users List <users@tomcat.apache.org>
Subject: Re: [EXTERNAL EMAIL] How to access a REST service

(apologies for the top post -- this thread is getting messy)

I assume you mean JAX-RS (or similar).

As a few poeple have already pointed out, these are Servlets under the covers 
-- regardless, what you have described still has conflicting
statements:
- you state it's regular Java program (implies it has a main function)
- you state it's a WAR file created in IntelliJ (implies it is a Servlet-based 
application -- whether you are using REST, JSP or others) Those are conflicting 
statements and why we have been getting very confused.

Based on some of your most recent comments, I believe you are using standard 
Servlet packaging (WAR file), and you are definitely using Servlets under the 
covers, whether you wrote the Servlet class or not.

So, as you said in the email to Chris, you still have this bizarre DB 
connection issue. Let's see if we can get to the bottom of that...no need to 
discuss the "client-portion" of the program -- let's focus on just the 
operations (HTTP operations) that go to your server.

As I've said before, I do not expect this is an issue with Glassfish ("select 
isn't broken"), and that something you have done in setting up either the pool, 
or the rest endpoints is causing the problem. Here's a list of specific 
questions that would be helpful to have answers to (actual answers with data 
from logs, or code samples, etc.)

Q1: Are you able to post the code for a single "REST endpoint" (without the 
business logic), and any classes it uses? This would be much more precise and 
less prone to communication issues.

Q2: It's also not clear what analysis has been done on SQL connection usage?

Q3: Do you have logs from your program showing SQL connections acquisition in 
relation to client requests?

Q4: Have you done any analysis by reducing the pool size to the minimum and 
invoking single REST requests (without the client code .. it's not important to 
this problem)?

Q5: What do you see in terms of connection usage? If you "graphed the count", 
do you get something like this (fixed width font needed):
                                   ___
DB connection count over time:  __/   \__
REST request:                    +-----+

If not, then you want to look at when the connection is released back to the 
pool.

You also haven't provided any information about how the requests you see coming 
into your server relate to the SQL connections. It would be helpful to have 
some correlation of these so as to provide suggestions.


Q6: Can you provide some data around the number of REST requests your server is 
receiving per minute (or some period) that correlates to how quickly your 
session pool was overflowing before you increased the size to 1000?

Q7: Could you share your pool connection configuration details? It would be 
helpful to know if there are "idle periods" (like after release) or "connection 
test" operations that might be causing some of your issues.

Q8: Can you reproduce your pool overflow condition on your development computer 
with a low pool count and share the Glassfish logs leading up to and including 
the exception?

Q9: Can you share your WEB.INF file that's packaged in your WAR file?


Robert


On Thu, Aug 14, 2025 at 5:42 PM Daniel Schwartz <d...@danielgschwartz.com>
wrote:

> Robert,
>
> Please see the reply is just sent to Chris.  I think that this answers 
> your questions.
>
> I don't know why people thought I was using servlets.  It's a REST web 
> service.  I thought that this was clear.
>
> Dan
>
> -----Original Message-----
> From: Robert Turner <rtur...@e-djuster.ca.INVALID>
> Sent: Thursday, August 14, 2025 1:27 AM
> To: Tomcat Users List <users@tomcat.apache.org>
> Subject: Re: [EXTERNAL EMAIL] How to access a REST service
>
> Oh....
>
>
> On Thu, Aug 14, 2025, 00:57 Daniel Schwartz <d...@danielgschwartz.com>
> wrote:
>
> >
> >
> >
> > DGS: You are talking above my head on this, but I’m not using any 
> > servlets, and the entire JVM process is the main thread.
> >
> >
> >
> > DGS: Let me put this in context.  My system has two components, (1) 
> > a backend REST webservice written in Java and running in Glassfish, 
> > (2) a website written in Next.js that consumes the webservice. The 
> > Java program access a MySql database through the Glassfish pooling system.
> > But this is just an ordinary Java program running in a single thread.
> > If this throws an exception that is caught, then the code for the 
> > catch clause will output an error message, and if it throws an 
> > exception that is not caught, the JRE will output a stack trace and 
> > terminate.  You say that Glassfish will somehow “swallow” the 
> > exception and keep running.  I really don’t think so.  Maybe 
> > something like this will happen with servlets, but this is just an 
> > ordinary Java program, and this is how Java behaves.  It has nothing 
> > to do with
> Glassfish.
> >
>
> Now I know why are all so confused ..
>
> FWIW, this made me laugh for 3 minutes...
>
> Either you've made things much harder for yourself, or you have some 
> good reason for this approach...
>
> Typically one would use a server based application with a servlet 
> container (Glassfish or Tomcat).
>
> What is acting as your web server/TCP server! Performing your listen / 
> accept / etc on the sockets?
>
> Are you using bits of Glassfish as a library?
>
> Is there a reference example of what you've built somewhere?
>

Reply via email to