Daniel, You might take a look at java’s “ try with” construct
--------------------------------------------------------------------------------------------------------------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: [email protected]For additional commands, e-mail: [email protected]---------------------------------------------------------------------To unsubscribe, e-mail: [email protected]For additional commands, e-mail: [email protected]
|