Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java id的Webservice增量变量使用单例/同步_Java_Web Services_Design Patterns_Singleton_Primary Key - Fatal编程技术网

Java id的Webservice增量变量使用单例/同步

Java id的Webservice增量变量使用单例/同步,java,web-services,design-patterns,singleton,primary-key,Java,Web Services,Design Patterns,Singleton,Primary Key,我正在实现一个用于攻击一个DB的webservice witch。 我需要为我存储的对象生成ID,但我不知道最好的方法是什么。 我需要增加一个整数 显然,Web服务必须同时适用于这么多人,而且可能是各种各样的人 那么,什么是好的解决方案 单例/同步 我想这是我知道的唯一方法,也许还有其他更好的方法 如果你能给我举个例子,我将不胜感激 提前谢谢 使用同步块来实现这一点。在同步块中,只有一个线程可以进入其中 JVM保证Java同步代码一次只能由一个线程执行。同步具有可怕的开销。如果您只需要一个增量计

我正在实现一个用于攻击一个DB的webservice witch。 我需要为我存储的对象生成ID,但我不知道最好的方法是什么。 我需要增加一个整数

显然,Web服务必须同时适用于这么多人,而且可能是各种各样的人

那么,什么是好的解决方案

单例/同步

我想这是我知道的唯一方法,也许还有其他更好的方法

如果你能给我举个例子,我将不胜感激


提前谢谢

使用同步块来实现这一点。在同步块中,只有一个线程可以进入其中


JVM保证Java同步代码一次只能由一个线程执行。

同步具有可怕的开销。如果您只需要一个增量计数器,那么可以使用。将AtomicLong放在一个单例中,以具有服务器范围的访问权限

编辑:一些代码示例:

import java.util.concurrent.atomic.AtomicLong;

public class AtomicIdGenerator
{
    private static class SingletonHolder
    {
        public static final AtomicIdGenerator instance = new AtomicIdGenerator();
    }

    public static AtomicIdGenerator getInstance()
    {
        return SingletonHolder.instance;
    }

    private AtomicLong mIdGenerator = null;

    private AtomicIdGenerator()
    {
        mIdGenerator = new AtomicLong();
    }

    private AtomicLong getGenerator()
    {
        return mIdGenerator;
    }

    public long getNewId()
    {
        return getGenerator().incrementAndGet();
    }
}
用法示例很简单:

long tNewId = AtomicIdGenerator.getInstance().getNewId();

这将是线程安全的,并且没有任何来自同步的开销。如果您预见到自己将在未来处理大量并发用例,那么该软件包将为您的用例提供大量经过战斗验证的实现。

您可以这样做。我以前做过,它是基于PostgreSql和iBatis的,但是你可以理解

public class Sequence implements Serializable {
    private static final long serialVersionUID = 7526471155622776147L;
    private String name  = null;
    private int nextId = 0;

    public Sequence () {
    }

    public Sequence (String name, int nextId) {
        this.name = name;
        this.nextId = nextId;
    }

    public final String getName () {
        return name;
    }

    public final void setName (String name) {
        this.name = name;
    }

    public final int getNextId () {
        return nextId;
    }

    public final void setNextId (int nextId) {
        this.nextId = nextId;
    }

}


public class SequenceSqlMapDao extends SqlMapClientDaoSupport implements SequenceDao {

  /**
   * This is a generic sequence ID generator that is based on a database
   * table called 'SEQUENCE', which contains two columns (NAME, NEXTID).
   * <p/>
   * This approach should work with any database.
   *
   * @param name The name of the sequence.
   * @return The Next ID
   * @
   */
  public final synchronized int getNextId(String name) {
    Sequence sequence = new Sequence(name, -1);
    //Sequence sequence = new Sequence();
    sequence = (Sequence) getSqlMapClientTemplate ().queryForObject("getSequence", sequence);
    if (sequence == null) {
            try {
                throw new IllegalArgumentException("Error: SHOOT! A null sequence was returned from the database (could not get next " + name + " sequence).");
            } catch (Exception ex) {
                Logger.getLogger(SequenceSqlMapDao.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
    Object parameterObject = new Sequence(name, sequence.getNextId() + 1);
    getSqlMapClientTemplate ().update("updateSequence", parameterObject);
    int nextId = sequence.getNextId();

    parameterObject  = null;
    sequence = null;

    return nextId;
  }

}
如果没有别的,这是相当不可知的数据库。您仍然需要在Web服务中公开该方法。
PS-我忘了这是从哪里得到的,否则我会相信正确的来源。

谢谢,它工作得很好=我不知道当不同的客户同时使用它时会发生什么,但我希望它能工作,谢谢!!我首先尝试了我们的搭档在另一个答案中告诉我的,我希望它能起作用