Implementation of multi-example mode of Sequence key generator

Source: Internet
Author: User

public class Mutilkeygenerator {
private static HashMap keys=new HashMap ();
private static final int pool_size=20;
Private KeyInfo Keyinfo=null;
Private Mutilkeygenerator () {}
Private Mutilkeygenerator (String keyName) {
Keyinfo=new KeyInfo (pool_size, keyName);
}
/*
* Static factory to provide its own instance
* */
Public synchronized static Mutilkeygenerator getinstance (String keyName) {
Mutilkeygenerator Keygenerator=null;
if (Keys.containskey (KeyName)) {
Keygenerator= (Mutilkeygenerator) keys.get (keyName);
System.out.println (keyname+ "founded");
}
else{
Keygenerator=new Mutilkeygenerator (keyName);
Keys.put (KeyName, keygenerator);
System.out.println (keyname+ "created");
}
return keygenerator;
}

public int Getnextkey () {
return Keyinfo.getnextkey ();
}
}

Class KeyInfo
{
private int Keymax;
private int keymin;
private int nextkey;
private int poolsize;
Private String KeyName;
Public KeyInfo (int poolsize,string keyName)
{
This.poolsize=poolsize;
This.keyname=keyname;
Retrivefromdb ();
}
public int Getkeymax () {
return Keymax;
}
public int getkeymin () {
return keymin;
}
public int Getnextkey ()
{
if (Nextkey>keymax) {
Retrivefromdb ();
}
return nextkey++;
}
/*
* Extract current key values from the database
* */
private void Retrivefromdb ()
{
String sql1= "Update keyTable set keyvalue=keyvalue+" +poolsize+ "where Keyname= '" +keyname+ "'";
String sql2= "Select KeyValue from keyTable where Keyname= '" +keyname+ "'";
Excute the Sql,assume the value returned is 1000
int keyfromdb=1000;
This.keymax=keyfromdb;
this.keymin=keyfromdb-poolsize+1;
This.nextkey=keymin;
}
}

Implementation of multi-example mode of Sequence key generator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.