how do you perform multiple SQL
queries within the same connection?
The correct answer here is "use transactions". If you begin transaction and then perform multiple operations with JdbcTemplate
, each of those operations will be within the scope of the transaction, and therefore are guaranteed to use the same connection.
If you don't want to get involved with transactions, then the alternative is to use the more primitive operations on JdbcTemplate
, like execute(ConnectionCallback action)
, where you supply an instance of ConnectionCallback
which is given a Connection
, on which you can then perform any operations you choose. Of course, but doing this you don't get JdbcTemplate
's help in any of the actual operations.
Transactions are really quite easy in Spring, you should look into using them (see above link).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…