admin管理员组文章数量:1208155
I have a test that creates an entity, updates it and checks that last modification date time is increased after update. I use JdbcClient to execute the following queries on PostgreSQL:
INSERT INTO entity (id, modified_on) VALUES (1, CURRENT_TIMESTAMP) RETURNING *;
UPDATE entity SET modified_on=CURRENT_TIMESTAMP WHERE id=1 RETURNING *;
The problem is that modified_on
is not increased.
Here is a simplified test that shows the problem:
@Autowired
private JdbcClient jdbcClient;
@Test
void test() {
var time1 = (java.sql.Timestamp) jdbcClient.sql("SELECT CURRENT_TIMESTAMP").query().singleValue();
System.out.println(time1);
var time2 = (java.sql.Timestamp) jdbcClient.sql("SELECT CURRENT_TIMESTAMP").query().singleValue();
System.out.println(time2);
assertThat(time2).isAfter(time1);
}
It's failed because time1
equals time2
.
I guess that JdbcClient
shoud commit queries immediately. But it seems that it executes them in a single transaction and that's why time is the same.
For sure I can use statement_timestamp()
and it will fix the test. But I need CURRENT_TIMESTAMP
. Is it possible to force commit between queries?
I have a test that creates an entity, updates it and checks that last modification date time is increased after update. I use JdbcClient to execute the following queries on PostgreSQL:
INSERT INTO entity (id, modified_on) VALUES (1, CURRENT_TIMESTAMP) RETURNING *;
UPDATE entity SET modified_on=CURRENT_TIMESTAMP WHERE id=1 RETURNING *;
The problem is that modified_on
is not increased.
Here is a simplified test that shows the problem:
@Autowired
private JdbcClient jdbcClient;
@Test
void test() {
var time1 = (java.sql.Timestamp) jdbcClient.sql("SELECT CURRENT_TIMESTAMP").query().singleValue();
System.out.println(time1);
var time2 = (java.sql.Timestamp) jdbcClient.sql("SELECT CURRENT_TIMESTAMP").query().singleValue();
System.out.println(time2);
assertThat(time2).isAfter(time1);
}
It's failed because time1
equals time2
.
I guess that JdbcClient
shoud commit queries immediately. But it seems that it executes them in a single transaction and that's why time is the same.
For sure I can use statement_timestamp()
and it will fix the test. But I need CURRENT_TIMESTAMP
. Is it possible to force commit between queries?
1 Answer
Reset to default 2One way to make the test pass, while it will be kind of artificial, is to introduce a service class that creates a new transaction for every call.
import org.springframework.jdbc.core.simple.JdbcClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.sql.Timestamp;
@Service
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class TimeService {
private final JdbcClient jdbcClient;
public TimeService(JdbcClient jdbcClient) {
this.jdbcClient = jdbcClient;
}
public Timestamp getCurrentTimestamp() {
return (java.sql.Timestamp) jdbcClient.sql("SELECT CURRENT_TIMESTAMP").query().singleValue();
}
}
Please note that this class has to be imported in the test class using @Import(TimeService.class)
The test:
@Autowired
TimeService timeService;
@Test
void test() {
Timestamp time1 = timeService.getCurrentTimestamp();
Timestamp time2 = timeService.getCurrentTimestamp();
assertThat(time2.getTime()).isGreaterThan(time1.getTime());
}
本文标签: Force commit JDBC queryStack Overflow
版权声明:本文标题:Force commit JDBC query - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738759408a2110866.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
SELECT clock_timestamp()
– Roar S. Commented Jan 18 at 19:08clock_timestamp()
fixes the test. But I can't use it because it's non-standard andCURRENT_TIMESTAMP
is better suited for my App – Denis Commented Jan 18 at 19:20