All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bixuebihui.sequence.KeyInfo Maven / Gradle / Ivy

Go to download

a fast small database connection pool and a active record flavor mini framework

There is a newer version: 1.15.3.3
Show newest version
package com.bixuebihui.sequence;

import com.bixuebihui.jdbc.IDbHelper;
import com.bixuebihui.util.DbUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-2 15:24:52
* Note: Sequence载体 DROP TABLE IF EXISTS t_sequence; CREATE TABLE * t_sequence( KEYNAME varchar(24) NOT NULL ,--COMMENT 'Sequence名称', KEYVALUE * numeric(20) DEFAULT 10000,-- COMMENT 'Sequence最大值', PRIMARY KEY (KEYNAME) ) ; * * @author xingwx * @version $Id: $Id */ public class KeyInfo { /** * Constant SEQUENCE_TABLE="t_sequence" */ public static final String SEQUENCE_TABLE = "t_sequence"; /** Constant SEQUENCE_DBHELPER_NAME="sequenceDbHelper" */ public static final String SEQUENCE_DBHELPER_NAME = "sequenceDbHelper"; /** 当前Sequence载体的最大值 */ private long maxKey; /** 当前Sequence载体的最小值 */ private long minKey; /** 下一个Sequence值 */ private long nextKey; /** Sequence值缓存大小 */ private int poolSize; /** Sequence的名称 */ private String keyName; private static final String SQL_UPDATE = "UPDATE " + SEQUENCE_TABLE + " SET KEYVALUE = KEYVALUE + ? WHERE KEYNAME = ? and KEYVALUE=?"; private static final String MOVE_TO_MAX = "UPDATE " + SEQUENCE_TABLE + " SET KEYVALUE = ? WHERE KEYNAME = ? "; private static final String SQL_QUERY = "SELECT KEYVALUE FROM " + SEQUENCE_TABLE + " WHERE KEYNAME = ?"; static final String[] INSTALL ={ "CREATE TABLE IF NOT EXISTS t_sequence ( KEYNAME varchar(24) NOT NULL PRIMARY KEY ,"+ " KEYVALUE INT default 10000"+ " );" }; private IDbHelper dbHelper; protected Logger LOG = LoggerFactory.getLogger(KeyInfo.class); private void init(String keyName, int poolSize, IDbHelper dbHelper) throws SQLException{ this.dbHelper = dbHelper; this.poolSize = poolSize; this.keyName = keyName; retrieveFromDB(); } /** *

Constructor for KeyInfo.

* * @param keyName a {@link java.lang.String} object. * @param poolSize a int. * @param dbHelper a {@link IDbHelper} object. * @throws java.sql.SQLException if any. */ public KeyInfo(String keyName, int poolSize, IDbHelper dbHelper) throws SQLException { init(keyName, poolSize, dbHelper); } /** *

Getter for the field keyName.

* * @return a {@link java.lang.String} object. */ public String getKeyName() { return keyName; } /** *

Getter for the field maxKey.

* * @return a long. */ public long getMaxKey() { return maxKey; } /** *

Getter for the field minKey.

* * @return a long. */ public long getMinKey() { return minKey; } /** *

Getter for the field poolSize.

* * @return a int. */ public int getPoolSize() { return poolSize; } /** * 获取下一个Sequence值 * * @return 下一个Sequence值 * @throws java.sql.SQLException 数据库出错 */ public long getNextKey() throws SQLException { if (nextKey > maxKey) { int maxTries=10; int i=0; int res=0; while(imoveTo.

* * @param max a {@link java.lang.Long} object. * @throws java.sql.SQLException if any. */ public void moveTo(Long max) throws SQLException { dbHelper.executeNoQuery(MOVE_TO_MAX, new Object[] { max, keyName }); nextKey = max; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy