Hello Rob,

Thanks.  I wrote this before I knew about the “try-with” feature, but will look 
into it.  I see that an issue with my current code is that the connection won’t 
be closed if an error is thrown.

Best regards,

Dan

From: Rob Sargent <rsarg...@xmission.com>
Sent: Thursday, July 31, 2025 11:22 PM
To: Tomcat Users List <users@tomcat.apache.org>
Cc: Tomcat Users List <users@tomcat.apache.org>
Subject: Re: How to access a REST service

Daniel, You might take a look at java’s “ try with” construct The 
try-with-resources Statement (The Java™ Tutorials > Essential Java Classes > 
Exceptions) docs.oracle.com 
--------------------------------------------------------------------------------------------------------------
NkdkJdXPPEBannerStart
Be Careful With This Message
From (Rob Sargent 
<rsarg...@xmission.com>)<https://godaddy1.cloud-protect.net/email-details/?k=k1&payload=53616c7465645f5f43cdb6b8a8a52fa3034cb686bdec16527bc6702454bf60bc53c293e94b791a875d6eef29456ccde42b0f1f2e37268fc23df4e1a4c20f59fbfd081ad91e2206beca270f4502ddce16efdf9c0027285e07a7ead6a0d71b2f7cc67a347c0cc24f535337c9f1799502fdd6fe0419af566980908394dacbaa83d51a09ff16e0358c73c1ae9661daf2f8d969452b484810e64edad60cae5b674bc26056eb22d41f2a4d1e4cdcc1124a1e67d2a3aa0ed3c5f949128180ed87e8b5f6c8a8fc40cb5925515b14d3fa45c2c3198a8fa11c0ff352ea71fde364d47d4aff649892e45b646ae1d29f30e4a6f2352b>
Learn 
More<https://godaddy1.cloud-protect.net/email-details/?k=k1&payload=53616c7465645f5f43cdb6b8a8a52fa3034cb686bdec16527bc6702454bf60bc53c293e94b791a875d6eef29456ccde42b0f1f2e37268fc23df4e1a4c20f59fbfd081ad91e2206beca270f4502ddce16efdf9c0027285e07a7ead6a0d71b2f7cc67a347c0cc24f535337c9f1799502fdd6fe0419af566980908394dacbaa83d51a09ff16e0358c73c1ae9661daf2f8d969452b484810e64edad60cae5b674bc26056eb22d41f2a4d1e4cdcc1124a1e67d2a3aa0ed3c5f949128180ed87e8b5f6c8a8fc40cb5925515b14d3fa45c2c3198a8fa11c0ff352ea71fde364d47d4aff649892e45b646ae1d29f30e4a6f2352b>
Potential Impersonation
The sender's identity could not be verified and someone may be impersonating 
the sender. Take caution when interacting with this message.

NkdkJdXPPEBannerEnd
Daniel,
You might take a look at java’s “ try with” construct

The try-with-resources Statement (The Java™ Tutorials > Essential Java Classes 
> 
Exceptions)<https://urldefense.proofpoint.com/v2/url?u=https-3A__docs.oracle.com_javase_tutorial_essential_exceptions_tryResourceClose.html&d=DwMFaQ&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=AbCalLxzopgQUG9LLcXdB80OM-GtDfItX76RMxNYqz4&m=-hacTP6KVKuoWm9UAmO35pU3860b8Zy2BhScJ-KwihYqxah3UKba5boSuGVBsRx0&s=mNhXT4swanwGi8JqA0d0saa_QrNH6RP9EBEnFjz-kE0&e=>
docs.oracle.com<https://urldefense.proofpoint.com/v2/url?u=https-3A__docs.oracle.com_javase_tutorial_essential_exceptions_tryResourceClose.html&d=DwMFaQ&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=AbCalLxzopgQUG9LLcXdB80OM-GtDfItX76RMxNYqz4&m=-hacTP6KVKuoWm9UAmO35pU3860b8Zy2BhScJ-KwihYqxah3UKba5boSuGVBsRx0&s=mNhXT4swanwGi8JqA0d0saa_QrNH6RP9EBEnFjz-kE0&e=>
[favicon.ico]<https://urldefense.proofpoint.com/v2/url?u=https-3A__docs.oracle.com_javase_tutorial_essential_exceptions_tryResourceClose.html&d=DwMFaQ&c=euGZstcaTDllvimEN8b7jXrwqOf-v5A_CdpgnVfiiMM&r=AbCalLxzopgQUG9LLcXdB80OM-GtDfItX76RMxNYqz4&m=-hacTP6KVKuoWm9UAmO35pU3860b8Zy2BhScJ-KwihYqxah3UKba5boSuGVBsRx0&s=mNhXT4swanwGi8JqA0d0saa_QrNH6RP9EBEnFjz-kE0&e=>

--------------------------------------------------------------------------------------------------------------
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<mailto:users-unsubscr...@tomcat.apache.org>
For additional commands, e-mail: 
users-h...@tomcat.apache.org<mailto:users-h...@tomcat.apache.org>


---------------------------------------------------------------------
To unsubscribe, e-mail: 
users-unsubscr...@tomcat.apache.org<mailto:users-unsubscr...@tomcat.apache.org>
For additional commands, e-mail: 
users-h...@tomcat.apache.org<mailto:users-h...@tomcat.apache.org>

Reply via email to