MxLDevs
MxLDevs

Reputation: 19506

Storing prepared statements as static members

I have 15 prepared statements that I will be using very frequently throughout my application. A few of them will be called several million times during a single task.

I have decided to use prepared statements since parsing a regular statement a million times does not seem like a good idea.

I am planning to create a utility class that will manage all database-related things and all of the prepared statements. The statements will be stored as static members which I can call by passing in the appropriate arguments.

Are there any issues with storing prepared statements as static members that I should be aware of?

I am using sqlite-jdbc if that makes a difference.

EDIT:

This is how I'm planning to manage my prepared statements

class DBUtils {

   private static PreparedStatement psInsertPerson;

   public static void createStatements() {
      // assuming a connection has been set up already
      psInsertPerson = conn.prepareStatement( ... );
   }

   public static void insertPerson( ... ) {
      psInsertPerson.set...
      psInsert.addBatch();

      // figure out when to perform batch insertion at some point
   }
}

Upvotes: 3

Views: 209

Answers (1)

user207421
user207421

Reputation: 310893

Don't do it with statics. Use the Apache DBCP connection pool as your DataSource, and configure it to pool PreparedStatements as well.

Upvotes: 1

Related Questions