sequenceservice.java

来自「一个很好的开源项目管理系统源代码」· Java 代码 · 共 94 行

JAVA
94
字号
/* */package net.java.workeffort.service;import java.util.HashMap;import java.util.Map;import net.java.workeffort.service.support.OptimisticLockingException;/** * The sequence service. * <p> * The update of the sequence table will happen in a new transaction seperate * from that of the business transaction. This is configured in spring using the * transaction attribute <code>PROPAGATION_REQUIRES_NEW</code> * </p> * <p> * Note: There has to be an entry for each sequence in the * <code>SEQUENCE_TABLE</code> * </p> * Designed to work in a clustered environment. * @author Antony Joseph */public class SequenceService extends BaseService implements ISequenceService {    private int maximumTries = 10; // default. Can be set using spring.    /**     * Get the nextid for the sequence block     * @param name The sequence name     * @param blockSize The block size of the sequence     * @return The next id     */    public Long getNextBlockSequenceId(String name, Integer blockSize) {        // Need to loop for clustered environments where requests for the        // same sequence may come from multiple cluster nodes and collide        // causing an OptimisticLockingException.        // (Synchronizing will not protect us here because calls are from        // different jvms).        // Try maximumTries times before throwing exception.        int loopCnt = 0;        Map sequence = null;        while (loopCnt < maximumTries) {            sequence = (Map) dao.queryForObject("Sequence.getSequence", name);            if (sequence == null)                throw new IllegalArgumentException("Sequence with name " + name                        + " not found. Check for entry in SEQUENCE_TABLE.");            Integer newVersion = new Integer(                    ((Integer) sequence.get("version")).intValue() + 1);            Long newNextId = new Long(((Long) sequence.get("nextId"))                    .longValue()                    + blockSize.intValue() + 1);            Map map = new HashMap();            map.put("nextId", newNextId);            map.put("name", name);            map.put("newVersion", newVersion);            map.put("version", sequence.get("version"));            try {                dao.update("Sequence.updateSequence", map, false);                break;            }            catch (OptimisticLockingException e) {                // keep looping if the update was not successful.            }            loopCnt++;        }        if (loopCnt < maximumTries)            return (Long) sequence.get("nextId");        else {            throw new RuntimeException("Failed to get sequence for " + name                    + " after " + maximumTries + " tries.");        }    }    /**     * @return Returns the maximumTries.     */    public int getMaximumTries() {        return maximumTries;    }    /**     * @param maximumTries The maximumTries to set. Can be set by spring IOC     */    public void setMaximumTries(int maximumTries) {        this.maximumTries = maximumTries;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?