"System resource exceeded" during connection to Access file through Java jdbc odbc

Stefanos Kargas picture Stefanos Kargas · Jan 11, 2012 · Viewed 12.6k times · Source

I've read all "System resource exceeded" posts, but this is nothing like them. I've spend the last 3 hours searching for a solution. I don't have many connections / statements / resultsets and I always close all of them. My code used to work but now I get the "System resource exceeded" exception, not during queries, but WHEN I TRY TO CONNECT. I didn't change a thing from my code, however it doesn't work at the moment, except 1 out of 10 times I try it. I tried to change some things in it but no difference. My Access files are 15 - 50 MB. My code is:

private String accessFilePath;
private Connection myConnection;
public boolean connectToAccess(String myAccessFilePath) {
    accessFilePath = myAccessFilePath;
    //Get connection to database
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        // set properties for unicode
        Properties myProperties = new Properties();
        myProperties.put("charSet", "windows-1253");
        myConnection = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + accessFilePath, myProperties); // I get the exception here
    } catch (Exception ex) {
        System.out.println("Failed to connect to " + accessFilePath + " database\n" + ex.getMessage());
        return false;
    }
    return true;
}

What is now different from other times? Do Access files keep previous connections open? What can be wrong here?

Answer

Stefanos Kargas picture Stefanos Kargas · Jan 13, 2012

OK, I found the solution. At first I started a new java project and copied the same codelines there. I successfully connected to my files every time I tried it in my new project. So it struck me. I looked at my VM settings. In my original program I ASSIGNED TOO MUCH MEMORY TO THE VIRTUAL MACHINE so there was no memory left even for a single connection to the files.

My settings were --> VM Options: -Xmx1536m -Xms768m (a little bit excessive)

I changed it to --> VM Options: -Xmx512m -Xms256m

And it worked. Thank you for your comments. I hope this helps other people, because I spend many hours to find it.