0

Me and my colleagues noticed weird behaviour on our local environments (Manjaro & Windows) thanks to integration tests.

Field of the type OffsetDateTime differs in nano before and after real save to the DB. I was trying to find some information and it seems, that there is some time truncation or rounding. But weird thing is that this is happening on some of our locals environments, but not in our pipelines (GitLab).

Simple test code:

val plainEntity = Entity(url = "url")
val savedEntity = repository.save(plainEntity)
val retriEntity = repository.findByUrl(plainEntity.url)

assertTrue(savedEntity.date.isEqual(retriEntity?.date)) // this is false due difference in nano part

Produces such data:

plainEntity.date //2022-08-18T13:35:36.953334471+02:00
savedEntity.date //2022-08-18T13:35:36.953334471+02:00
retriEntity.date //2022-08-18T13:35:36.953334+02:00

Entity (simplified):

@Entity
@Table(name = TABLE_NAME)
@SequenceGenerator(name = SEQUENCE_GEN, sequenceName = SEQUENCE_NAME, allocationSize = 1)
class Entity(
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE_GEN)
    var id: Long = 0L,

    @Column(nullable = false)
    val url: String,

    @Column(nullable = false)
    var date: OffsetDateTime = OffsetDateTime.now()
    // there is no difference if I remove this default value and set it in construction of the entity
)

Repository:

@Repository
interface Repository : CrudRepository<Entity, Long> {
    fun findByUrl(url: String): Entity?
}

Table creation:

<createTable tableName="tableName">
    <column name="id" type="bigint" defaultValueSequenceNext="table_id_seq">
        <constraints primaryKey="true" primaryKeyName="table_id_pk" nullable="false"/>
    </column>

    <column name="url" type="text">
        <constraints nullable="false" unique="true"/>
    </column>

    <column name="date" type="timestamp with time zone">
        <constraints nullable="false"/>
    </column>
</createTable>

Project set-up:

  • spring-boot app
  • spring data JPA
  • Liquibase for definitions
  • testcontainers with Postgres in tests
  • By any chance are those local environments running in different timezone than gitlab pipelines ? – SRJ Aug 18 '22 at 11:50
  • 1
    Is the datetime indeed stored as `2022-08-18T13:35:36.953334471+02:00`? `save` might have yielded a cached version. Check in a database tool. And why not using ZonedDateTime accounting for DST (summer time) – Joop Eggen Aug 18 '22 at 12:01
  • @shubham yes, it is possible. Don't have the possibility to check the pipeline right now, but by default pipeline should be in UTC and our locals probably in +2 – Petr Binčík Aug 18 '22 at 12:02
  • @JoopEggen first the second part - we are using OffsetDateTime because by architecture. It's our old technical debt, we are planning to change it soon. And the database contains the second time (with truncated time) – Petr Binčík Aug 18 '22 at 12:07
  • I was solving this [also](https://github.com/spring-projects/spring-data-jpa/issues/2435) at the end I've just created some mechanism where I truncated the nanos from entities. – bilak Aug 18 '22 at 14:05
  • @bilak hmm, I was thinking about it, but it is weird, that the same logic is working in GitLab pipelines. – Petr Binčík Aug 19 '22 at 11:05

0 Answers0