Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8083865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:36:43+00:00 2026-06-05T17:36:43+00:00

I am running into OutOfMemoryError on a long running background process on Glassfish. Memory

  • 0

I am running into OutOfMemoryError on a long running background process on Glassfish. Memory analysis has shown that at the time the error is thrown 50% of the heap is dedicated to instances of com.mysql.JDBC4ResultSet (28.1%) and com.mysql.jdbc.StatementImpl (22.1%).

My code is structured on the assumption that my JDBC objects will be Garbage Collected once they loose scope however this is obviously not the case. I originally did this using JPA but the memory load exploded so I reverted to JDBC but I am still getting enormous memory leaks consisting of references to JDBC Statements & ResultSets.

So I am wondering if Glassfish is caching these queries and how I can disable that, the data objects are quite large and I really don’t need them to be cached.

I have included below the structure of my code consisting of one class and two methods (modified for brievity).

@Stateless
public class WSDFileCollector implements Collector {

    @PersistenceContext
    private EntityManager em;

    @Override
    @Asynchronous
    public void run(final CollectorEntity collector) throws SQLException {

        final Connection connection = em.unwrap(Connection.class);
        final String table = "tmp_sat_" + collector.getSite().getId();
        final String column = "filename";
        try {
            // Create temporary table
            // Scan files & folders under a given directory.
            // Load filenames into MySQL Temporary table.

            final Statement statement = connection.createStatement();
            final ResultSet results = statement.executeQuery("SELECT filename FROM temporary_table WHERE filename NOT IN (SELECT filename FROM existing_files");
            while (results.next()) {
                final File file = new File(collector.getPath(), results.getString("filename"));
                if (file.isFile()) {
                    extractAndSave(file, collector);
                }
            }
        } finally {
            // Delete temporary table
        }
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    private void extractAndSave(final File file, final CollectorEntity collector) {
        final Connection connection = em.unwrap(Connection.class);
        try {
            // Begin a transaction
            // INSERT new file into existing_files

            // Scan the file, extract data and insert into multiple database tables.

            // Commit transaction
        } catch (final SQLException | FileNotFoundException ex) {
            // Rollback transaction
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:36:45+00:00Added an answer on June 5, 2026 at 5:36 pm

    As you’ve unwrapped the connection from the entity manager and thus completely take over the control over the resources in your own hands, you should explicitly close them yourself in the finally block of the very same try block as where they’re been acquired as per the following standard JDBC idiom.

    // Declare resources.
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
    
    try {
        // Acquire resources.
        connection = getConnectionSomehow();
        statement = connection.createStatement();
        resultSet = statement.executeQuery(sql);
    
        // ...
    } finally {
        // Close resources in reversed order.
        if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
    }
    

    Otherwise they will remain in an opened state and indeed leak away in both the server and the DB.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running into 'Error: Java heap space, java.lang.OutOfMemoryError: Java heap space' when compiling an
Im running into this error that I can't work out Im writing some code
I'm running into a situation using the Entity Framework (EF) that has me totally
When running JUnit tests, I always seem to run into this error: eclipse outOfMemoryError:
Running into a problem where on certain servers we get an error that the
My application keeps running into Timeout Expired SqlExceptions. The thing is that this query
I am running into an error I am having trouble figuring out. I have
After running into linker errors one time too many, I have come to the
After running into this issue for the 10 billionth time and nearly whipping my
I am running into a project that read a stream from a txt file,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.