简体   繁体   中英

prepared statements sharing connection

There is an example that shows using transactions:

con.setAutoCommit(false);
updateSales = con.prepareStatement(updateString);
updateTotal = con.prepareStatement(updateStatement);
...
con.commit();
...
finally
updateSales close
updateTotal close

If I wanted to move two prepared statements into separate methods sharing a connection/transaction, each called from a parent method, I do not see how to handle closing the prepared statements, since the parent method would open the connection and then commit it.

parentmethod:
con.setAutoCommit(false)
method1(con)
method2(con)
con.commit()

Do not need to engineer it as such. Just felt logical to separate out the updates.

Why not have the PreparedStatement 's scope set to the class level? The parent method could then have a finally block that closes all resources that the child methods may have opened.

This still isn't the best solution (generally you'd want the same method that opened a resource to close the resource).

The elegant way, in my opinion, is to keep the PreparedStatement calls at the parent level, alongside other resources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM