Write update query in HQL (#523)

This commit is contained in:
Thomas Schroeter 2018-03-14 15:01:04 +00:00 committed by GitHub
parent 1b02291321
commit a2783f4c8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -139,20 +139,18 @@ class RunOnceService(private val database: CordaPersistence, private val machine
private fun updateTimestamp(session: Session, mutualExclusion: MutualExclusion): Boolean { private fun updateTimestamp(session: Session, mutualExclusion: MutualExclusion): Boolean {
val minWaitTime = mutualExclusion.timestamp.plus(waitInterval, ChronoField.MILLI_OF_SECOND.baseUnit) val minWaitTime = mutualExclusion.timestamp.plus(waitInterval, ChronoField.MILLI_OF_SECOND.baseUnit)
val query = session.createNativeQuery("UPDATE $TABLE SET $MACHINE_NAME = :machineName, $TIMESTAMP = CURRENT_TIMESTAMP, $PID = :pid " + val hql = "UPDATE RunOnceService\$MutualExclusion SET $MACHINE_NAME = :machineName, $TIMESTAMP = CURRENT_TIMESTAMP, $PID = :pid " +
"WHERE $ID = 'X' AND " + "WHERE $ID = 'X' AND " +
// we are master node // we are master node
"($MACHINE_NAME = :machineName OR " + "($MACHINE_NAME = :machineName OR " +
// change master node // change master node
"($MACHINE_NAME != :machineName AND " + "($MACHINE_NAME != :machineName AND " +
// no one else has updated timestamp whilst we attempted this update // no one else has updated timestamp whilst we attempted this update
"$TIMESTAMP = CAST(:mutualExclusionTimestamp AS DATETIME) AND " + "$TIMESTAMP = CAST(:mutualExclusionTimestamp AS LocalDateTime) AND " +
// old timestamp // old timestamp
"CURRENT_TIMESTAMP > CAST(:waitTime AS DATETIME)))", MutualExclusion::class.java) "CURRENT_TIMESTAMP > CAST(:waitTime AS LocalDateTime)))"
query.unwrap(org.hibernate.SQLQuery::class.java).addSynchronizedEntityClass(MutualExclusion::class.java) val query = session.createQuery(hql)
query.setParameter("pid", pid) query.setParameter("pid", pid)
query.setParameter("machineName", machineName) query.setParameter("machineName", machineName)