I am trying to execute a native query in which I've created local temporary table. My front end call an Api and a method executes that query. If I call that method again, sometimes i get, object(#temptablename) already present in database, if i keep refreshing my front end, 1 out 10 times it works.

This is much simplified form of complex query, but you will get the gist of what i'm trying to do.

select * into #ts from (
    "some select query"
) as tmp1;

Select
    *
from
    #ts

This method doesn't work every time.

jdbcTemplate.queryForList(query);

This works every time.

namedParameterJdbcTemplate.queryForList(query, args);

Args in case of namedParameterJdbcTemplate is garbage args as this query doesn't require any arguments.

My hunch is jdbcTemplate keeps using the same connection for every call because local temp table is associated with connection and namedParameterJdbcTemplate creates new connection every time.

How I created data source inside @PostConstruct method

DataSource dataSource = DataSourceBuilder.create().username(username).password(password)
                            .url(url + ";ApplicationIntent=ReadOnly").build();
jdbcTemplate.setDataSource(dataSource);
0

There are 0 best solutions below