简体   繁体   中英

Spring Jdbc 4 transaction management

I'm new to Spring (even to version 4.0.3 that I am currently using). In some code I have to extend, JdbcTemplate is uniquely used, an instance is shared among objects to keep all the sql statements under the same transaction. I'd like to use NamedParameterJdbcTemplate so I get one by using

NamedParameterJdbcTemplate npJT = new NamedParameterJdbcTemplate(givenJdbcTemplate.getDataSource());

Sadly npJT and givenJdbcTemplate look not to share the same transaction. This is an issue for me, how can I overcome this problem and keep all the sql instruction I'll do by using npJT in the same transaction as givenJdbcTemplate ?

Just use the other constructor which can take a JdbcTemplate as a parmeter and pass your JdbcTemplate to it.

NamedParameterJdbcTemplate npJT = new NamedParameterJdbcTemplate(givenJdbcTemplate);

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