最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Spring JCBDTemplate transactions - Stack Overflow

programmeradmin3浏览0评论

I'm using a mix of NamedParameterJdbcTemplate and JdbcTemplate in my Spring API application. I'm working with a transaction and Oracle temp table, and not getting the results I expect.

In the first batch, I have data, and the batchUpdate Insert returns 4 rows affected. But I turn around and read the table I just inserted into, and get zero record count. (Code comments indicate the results along the way.)

I insert, then try to read, and the read finds no records. Should the mix of tpl and npTpl be fine inside @Transactional?

The templates are initialized in a central class:

    @Bean
    public JdbcTemplate tpl(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean
    public NamedParameterJdbcTemplate npTpl(DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }

My DAO class starts much like this:

@Repository
public class PageDAO extends AbstractDAO<Pageoverview> {

    @Autowired
    private DataSource dataSource;

    @Autowired
    @Qualifier("npTpl")
    private NamedParameterJdbcTemplate npTpl;
    
    @Autowired
    private JdbcTemplate tpl;

and later this is the DAO method executed

    @Transactional
    public List<List<String>> getData(Map<String, String> parameterMap,
            String storedProcName, Long planId) throws SQLException {

        String sql = "insert into TempParameter (tepaovpaSystemString, tepaovpaValue) values (:systemString, :value)";

        try {
            MapSqlParameterSource[] batchParams = parameterMap
                    .entrySet().stream().map(entry -> new MapSqlParameterSource()
                            .addValue("systemString", entry.getKey()).addValue("value", entry.getValue()))
                    .toArray(MapSqlParameterSource[]::new);

            // before run, batchParams = [MapSqlParameterSource {systemString=PARAMSYSTEMSTRING, value=CALCS1}, MapSqlParameterSource {systemString=PLANID, value=33182}, MapSqlParameterSource {systemString=PERIODCODE, value=6226}, MapSqlParameterSource {systemString=PLEMID, value=-99}]
            int[] batchResult = npTpl.batchUpdate(sql, batchParams);
            // after run, batchResult = [1, 1, 1, 1]
            
            int resultTest = 0;
            try {
                Integer columnCount = tpl.queryForObject("select count(*) from TempParameter", Integer.class);
                resultTest = columnCount;
                // after run, columnCount = 0
             } catch (Exception e) {
                log.error("Error retrieving column count", e);
            }
发布评论

评论列表(0)

  1. 暂无评论