Hi. Quick question.
Does the jPetStore data access code correctly handle concurrency correctly? For example when retrieving a new Sequence number used in the Orders, what happens if a one thread calls getNextID() and gets 1000 as the next ID. Meanwhile before that thread continues with the call to update the sequence another thread calls getNextId and also receives 1000 as it’s new ID. This is going to be a problem. How does Spring-iBatis handle this? I’ve always handled this with sprocs and database auto-increment features before. Can iBatis and hibernate handle this in a way that I’m not understanding?
Thanks,
IBexx
------------------ Code frags -------------------------------
Code:public class SqlMapSequenceDao extends SqlMapClientDaoSupport { /** * This is a generic sequence ID generator that is based on a database * table called 'SEQUENCE', which contains two columns (NAME, NEXTID). * This approach should work with any database. * @param name the name of the sequence * @return the next ID */ public int getNextId(String name) throws DataAccessException { Sequence sequence = new Sequence(name, -1); sequence = (Sequence) getSqlMapClientTemplate().queryForObject("getSequence", sequence); if (sequence == null) { throw new DataRetrievalFailureException("Error: A null sequence was returned from the database (could not get next " + name + " sequence)."); } Object parameterObject = new Sequence(name, sequence.getNextId() + 1); getSqlMapClientTemplate().update("updateSequence", parameterObject, 1); return sequence.getNextId(); } } <sqlMap namespace="Sequence"> <resultMap id="result" class="org.springframework.samples.jpetstore.dao.ibatis.Sequence"> <result property="name" column="name" columnIndex="1"/> <result property="nextId" column="nextid" columnIndex="2"/> </resultMap> <select id="oracleSequence" resultMap="result"> select '$name$' as name, $name$.nextval as nextid from dual </select> <select id="getSequence" resultMap="result"> select name, nextid from sequence where name = #name# </select> <update id="updateSequence"> update sequence set nextid = #nextId# where name = #name# </update> </sqlMap>


Reply With Quote