We recently started using the DAO pattern to create classes used to retreive data from Oracle.
We have a DAOFactory which is called to get a particular DAO class, which is really just an interface for an ORImpl class that accesses the database and gets the data.
We created all of the ORImpl classes as singletons, with the private constructor, static instance variable, getInstance() method, etc.
The ORImpl also has methods for retrieving/updating data in Oracle, so each method opens a connection, creates a PreparedStatement, executes the query, closes the connection, and so on.
Everything works fine and we are able to get data and update data. However, ever since we implemented the DAO's, we are getting serious OutOfMemory errors (like every day) in our application.
My question is this:
Suppose we have methods in the ORImpl that can return large amounts of data. The method creates a Collection, puts each record retrieved as an object (DTO) in the Collection, and returns the Collection to the calling class to do whatever it will. But since the ORImpl is created as a singleton and static, will the Collection that was created ever get garbage collected? The same method in the ORImpl could get called numerous times, returning different data each time, and creating a new Collection each time. The Collection itself is not created as static, but since the class that creates the Collection is static (a singleton), and never gets garbage collected, won't there always be a reference (the singleton) to each Collection that gets created, thereby causing it to never be garbage collected?
From what I've read about garbage collection, as long as there is a reference to an object, that object is reachable, and therefore unable to be garbage collected.
We also implemented EJB's at the same time as the DAO's, but we're only using stateless session beans (no entity beans), so the memory hit on those should be minimal.
Let me know what you think.