Hi,
Daniel,
You might take a look at java’s “ try with” construct
This is an important step, indeed:
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();
...
}
A couple of questions:
1) How often is your class instantiated/created? Is it a singleton? A
new one per each incoming request?
2) What happens in case of an exception in your try block? Looking at
the above snipped the connection isn't closed.
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".
3) Have a look at the version of the database driver you're using. Is
there perhaps a newer one? If yes, have a look at the release notes and
check the changes. Perhaps there's a leak in the driver itself...?
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.
AFAICT only in case of proper execution wrt to the code snippet
presented. In case of an exception during the #doIt(...) call this won't
happen (correctly) and can result in a memory leak (AFAIK).
What happens in case of multiple simultaneously incoming requests? Does
your database (and driver) allow enough parallel connections so that
each request is processed correctly?
Regards
Thorsten
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org