From a93334c99d1523d89d7069a861756fa815a4810c Mon Sep 17 00:00:00 2001 From: Joshua Beha Date: Fri, 24 Mar 2023 10:48:02 -0400 Subject: [PATCH] Fix race condition --- .../samples/jpetstore/dao/ibatis/SqlMapSequenceDao.java | 7 +++---- .../samples/jpetstore/dao/ibatis/maps/Sequence.xml | 5 +++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/jpetstore/src/org/springframework/samples/jpetstore/dao/ibatis/SqlMapSequenceDao.java b/jpetstore/src/org/springframework/samples/jpetstore/dao/ibatis/SqlMapSequenceDao.java index bf0e7d1..d5e2e54 100644 --- a/jpetstore/src/org/springframework/samples/jpetstore/dao/ibatis/SqlMapSequenceDao.java +++ b/jpetstore/src/org/springframework/samples/jpetstore/dao/ibatis/SqlMapSequenceDao.java @@ -15,14 +15,13 @@ public class SqlMapSequenceDao extends SqlMapClientDaoSupport { */ public int getNextId(String name) throws DataAccessException { Sequence sequence = new Sequence(name, -1); - sequence = (Sequence) getSqlMapClientTemplate().queryForObject("getSequence", sequence); + sequence = (Sequence) getSqlMapClientTemplate().queryForObject("incrementAndReturnModified", sequence); if (sequence == null) { throw new DataRetrievalFailureException( - "Could not get next value of sequence '" + name + "': sequence does not exist"); + "Could not get next value of sequence '" + name + "': sequence does not exist"); } - Object parameterObject = new Sequence(name, sequence.getNextId() + 1); - getSqlMapClientTemplate().update("updateSequence", parameterObject, 1); return sequence.getNextId(); } } + diff --git a/jpetstore/src/org/springframework/samples/jpetstore/dao/ibatis/maps/Sequence.xml b/jpetstore/src/org/springframework/samples/jpetstore/dao/ibatis/maps/Sequence.xml index 8c6dbe9..e8d4870 100644 --- a/jpetstore/src/org/springframework/samples/jpetstore/dao/ibatis/maps/Sequence.xml +++ b/jpetstore/src/org/springframework/samples/jpetstore/dao/ibatis/maps/Sequence.xml @@ -20,4 +20,9 @@ update sequence set nextid = #nextId# where name = #name# + + +