Using Spring JdbcTemplate for Multiple Database Operations

I like the obvious simplicity of JdbcTemplate, but I'm a bit confused as to how this works. It seems that each operation (query () or update ()) retrieves the connection from the data source and closes it.

Nice, but how do you execute multiple SQL queries in the same connection?

I may need to perform several operations in a sequence (for example, SELECT, followed by INSERT, followed by a commit), or I may need to execute nested queries (SELECT, and then perform a second SELECT based on the result of each row).

How to do it with JdbcTemplate. Am I using the correct class?

+3
source share
2 answers

How to execute multiple SQL query queries within the same connection?

The correct answer is "Use Transactions . " If you start a transaction and then perform several operations with JdbcTemplate, each of these operations will be within the transaction and, therefore, the use of the same connection is guaranteed.

, JdbcTemplate, execute(ConnectionCallback action), ConnectionCallback, Connection, , . , JdbcTemplate .

Spring, (. ).

+6

, ? , Spring, JdbcTemplate .

, Ibatis. Spring JDBC , : Spring, . , , .

: 50 Spring , 2 .

, Spring JDBC SQL. Java , Java () .

+4

Source: https://habr.com/ru/post/1739544/


All Articles