Reputation: 1042
I define a Prepared Statement initially via
prepstmt=conn.prepareStatement("...");
and then use it (via a function) in various parts of the code
prepstmt.setDouble(1,x);
prepstmt.execute();
Now I sometimes get "No operations are allowed after statement closed." which is caused by the underlying connection being closed/timed out/reset elsewhere in the code.
Now obviously I could just try/catch the error and reset prepstmt then, but this would make "rerunning" the query more awkward, so was wondering how "costly" it is to do check first with something like
if (prepstmt.isClosed())
prepstmt=conn.prepareStatement("...");
prepstmt.setDouble(1,x);
prepstmt.execute();
i.e. checking whether the Prepared Statement is closed before every use of it. Guessing this must have some impact on performance, but no idea how significant. Estimating that for my particular application its closed less than 0.1% of the time.
Upvotes: 0
Views: 304
Reputation: 73528
You clearly have a design problem if you're passing PreparedStatements
around and don't even know if they're closed or not. Sounds like there can be resource leaks also.
However isClosed()
is not an expensive operation, it simply returns the boolean that is tracking the state of the statement. That is of course driver specific, but I'd be surprised if any driver implemented it in a less effective way.
Upvotes: 5