So I'm trying to optimize a Java application that uses the java sql library to read and write from a remote SQL server. (this is for a project).
We are limited to the following libraries:
com.mysql.*
java.io.*
java.lang.*
java.sql.*
java.text.*
java.util.*
javax.sql.*
And we have an 8MB memory limit on our JVM.
The way the code is set up right now, for every function that needs to access the database, a new connection is created and closed. for example:
public static void Read()
{
Connection connection = NULL;
try {
connection = DriverManager.getConnection(EnvManager.getDbUrl());
Statement statement = connection.createStatement();
statement.setFetchSize(Integer.MIN_VALUE);
******* FUNCTION ********
}finally {
if(connection != null) {
try {
connection.close();
} catch (Exception e) {
}
}
}
public static void Write()
{
Connection connection = NULL;
try {
connection = DriverManager.getConnection(EnvManager.getDbUrl());
Statement statement = connection.createStatement();
statement.setFetchSize(Integer.MIN_VALUE);
******* FUNCTION ********
}finally {
if(connection != null) {
try {
connection.close();
} catch (Exception e) {
}
}
}
etc...
I'm trying to set up a persistent connection so that I can either pass in the connection object, i.e.
...
...
Connection connection = NULL;
try {
connection = DriverManager.getConnection(EnvManager.getDbUrl());
Read(connection);
Write(connection);
}finally {
if(connection != null) {
try {
connection.close();
} catch (Exception e) {
}
}
...
...
However, when I try to do this, whenever I try to write and then read a large file to the database (anything over 5 MB), my JVM keeps throwing errors that it's running out of memory.
My question is basically, what happens when you make a single connection object and keep passing it into a function? It seems like it somehow grows or replicates every time it's used.
I looked at this website:
http://benjchristensen.com/2008/05/27/mysql-jdbc-memory-usage-on-large-resultset/
Which suggested I use the statement.setFetchSize(Integer.MIN_VALUE); function, in order to curtail the JVM buffering the entire result set in memory by default, but that didn't help.
Any ideas on how to fix this issue would be greatly appreciated, thank you.