Hello Mark, Thanks for taking the time to reply. Please see my notes below, marked "DGS".
-----Original Message----- From: Mark Thomas <ma...@apache.org> Sent: Thursday, July 31, 2025 4:32 AM To: users@tomcat.apache.org Subject: Re: How to access a REST service On 31/07/2025 05:10, Daniel Schwartz wrote: > I have a REST webservice written in Java that runs on the Glassfish server, > but I started having problems with the JDBC pooling system. It kept > crashing, saying that it was unable to allocate any more connection objects. > This made no sense to me, as I had set the maximum pool size to 1000, and I > surely was not getting that many simultaneous connections. Also, I consulted > with some Glassfish experts at a company called OmniFish, and this didn't > help. > > So I decided to shift to Tomcat, which doesn't require connection pooling. Umm... The requirement for connection pooling comes from your application, not the container. Like Glassfish, Tomcat supports connection pooling if you want to use it. DGS: I initially tried to rewrite my Glassfish app to by-pass the connection pooling to access MySQL directly and got error messages complaining that connection pooling had not been set up. It seems that you have to use connection pooling. At least this is what I concluded. The chances are you do want to use connection pooling as it will improve your application's performance. DGS: Yes, I would like to, but I was having problems with this that I couldn't figure out. From your description of your application's behaviour on Glassfish, it sounds like the application has a connection leak. You might find it is a better use of your time working on finding and fixing that leak rather than porting the application to Tomcat (where the leak will still be present). DGS: Someone else suggested the leak issue, but I believe that I am closing every database connection immediately after using it, so there shouldn't be a link. I have copied some relevant code fragments at the end of this message. Maybe you could look to see if there is an issue with this. DGS: It would be simpler for me to just stay with Glassfish, if I could only stop it from crashing. DGS: In any case, I have found out that some hacker has found a way to submit bogus queries through my website, but they are doing this less than 100 times per day, and certainly not 1000 at the same time. However, I did recently notice that the maximum pool size had somehow been set back to the default of 32, so maybe this is why the crashing started back up. Since I set it back to 1000, there have been no subsequent crashes for 2-3 days now. > I have implemented my system in both IntelliJ IDEA and VSCode, with > suitable changes to make a direct connection to MySQL. Both of these > produce a file called HolidaysRESTJSON-1.0-SNAPSHOT.war. This deploys > in Tomcat (I'm using tomcat-9.0.107), but when I try to access it with > my test URL, > > > https://urldefense.proofpoint.com/v2/url?u=http-3A__localhost-3A8080_H > olidaysRESTJSON-2D1.0-2DSNAPSHOT_webresources_holidaysandevents_countr > ies&d=DwICaQ&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=AbCalLxzo > pgQUG9LLcXdB80OM-GtDfItX76RMxNYqz4&m=nSgNUwSqfNb_FwrQmpLJdMUvWIQm1oNqS > Ocqbl8CX4EzoTIFqySerc91W-TTV0ga&s=5mkyVUuj6wpySF-BT4slF7BjEi38D8jisUdS > Vmu5Exo&e= > > I get a 404 error. It is supposed to return a list of countries. This did > work with my Glassfish version, however. So it should work with Tomcat. > > The question is: Why not? Insufficient information to provide a definitive answer. We can guess, but it is little more than that. > Is there something wrong with my URL? Probably. > How would I know? You wrote the application so you defined the URL mapping for your servlet. At a guess, you need to remove "/webresources" from the URL. DGS: I tried this, and it didn't help. Things you could provide that might help us help you: - clean logs of Tomcat starting and you making a single request - the web.xml for you application - any information related to defining the URL that is meant to return the list of countries. DGS: I plan to look at some sample Tomcat REST apps and see if I can figure out why mine isn't working. It might only require some minor code changes. If this doesn't work, I will send you the logs, web.xml, etc. In the meantime, I would appreciate it if you could tell me if the following code fragments look okay. Mark -------------------------------------------------------------------------------------------------------------- Here is a fragment of the code from the file HolidaysRESTJSONResource.jar. -------------------------------------------------------------------------- public class HolidaysRESTJSONResource { DataSource dataSource; public HolidaysRESTJSONResource() { dataSource = DataSourceSingleton.getInstance().dataSource; } @GET @Path("/countries") @Produces(MediaType.APPLICATION_JSON) public String getJsonCountries() { ... Connection connection; try { connection = dataSource.getConnection(); TempDataStorage.countryList = GetCountryList.doIt(connection); connection.close(); ... } -------------------------------------------------------------------------- The constructor for HolidaysRESTJSONResource creates a DataSource by creating an instance of the following DataSourceSingleton class and retrieving the value of the DataSource instance variable. I borrowed this code from somewhere. It seems to work. The MySql database is called "holidays". -------------------------------------------------------------------------- public class DataSourceSingleton { private static DataSourceSingleton instance = null; public DataSource dataSource = null; private DataSourceSingleton() { try { InitialContext ctx = new InitialContext(); dataSource = (DataSource) ctx.lookup("jdbc/holidays"); } catch (NamingException e) { System.out.println(e); } } public static DataSourceSingleton getInstance() { if (instance == null) { instance = new DataSourceSingleton(); } return instance; } } -------------------------------------------------------------------------- Back in HolidaysRESTJSONResource, in the method getJsonCountries(), the DataSource object, called "dataSource", is used to create the connection object, and this is used in a method call GetCountryList.doIt() to retrieve a list of countries from the database. Then this connection object is closed. I think that this is all that is needed to prevent memory leaks. Is this correct? I do this for every such method call that accesses the database. --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org