I’m trying to figure out if it’s efficient for me to cache all of my statements when I create my database connection or if I should only create those that are most used and create the others if/when they’re needed..
It seems foolish to create all of the statements in all of the client threads. Any feedback would be greatly appreciated.
A bit decent database will already cache them. Just fire
Connection#prepareStatement()at the moment you actually need to execute the query. You actually also have no other choice since connection, statement and resultset ought to be acquired and closed in the shortest possible scope, i.e. in atry-finallyblock in the very same method as you execute the query.Opening and closing the connection on every query in turn may indeed be expensive. A common solution to that is using a connection pool, for example c3p0.