package org.nstart.dep265.requestservice.service

import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Service

@Service
class GridRebuildLockService(
    private val jdbcTemplate: JdbcTemplate,
) {
    fun tryAcquireTransactionLock(): Boolean {
        return jdbcTemplate.queryForObject(
            "select pg_try_advisory_xact_lock(?)",
            Boolean::class.java,
            GRID_REBUILD_LOCK_KEY,
        ) == true
    }

    private companion object {
        const val GRID_REBUILD_LOCK_KEY = 26_501_001L
    }
}
