📄 sequencemanager.java
字号:
package com.redmoon.oa.db;import java.sql.*;import cn.js.fan.web.Global;import org.apache.log4j.Logger;import cn.js.fan.db.Conn;public class SequenceManager implements com.cloudwebsoft.framework.base.ISequence { String connname; Logger logger = Logger.getLogger(SequenceManager.class.getName()); private static final String LOAD_ID = "SELECT id FROM redmoonID WHERE idType=?"; private static final String UPDATE_ID = "UPDATE redmoonID SET id=? WHERE idType=? AND id=?"; public static final int OA_DOCUMENT_CMS = 0; public static final int OA_WORKFLOW = 1; public static final int OA_WORKFLOW_ACTION = 2; public static final int OA_WORKFLOW_LINK = 3; public static final int OA_EMAIL = 4; public static final int OA_DOCUMENT_FLOW = 5; public static final int OA_TASK = 6; public static final int OA_MESSAGE = 7; public static final int OA_DOCUMENT_NETDISK = 8; public static final int OA_VISUAL_DOCUMENT = 9; public static final int OA_LOG = 10; public static final int OA_ADDRESS = 11; public static final int OA_ADDRESS_GROUP = 12; public static final int OA_WORKFLOW_PREDEFINED = 13; public static final int SQ_MESSAGE = 14; public static final int OA_ARCHIVE_STUDY = 15; public static final int OA_ARCHIVE_RESUME = 16; public static final int OA_ARCHIVE_FAMILY = 17; public static final int OA_ARCHIVE_PROFESSION = 18; public static final int OA_ARCHIVE_ASSESS = 19; public static final int OA_ARCHIVE_REWARDS = 20; public static final int OA_ARCHIVE_DUTY = 21; public static final int OA_ARCHIVE_QUERY = 22; public static final int OA_ARCHIVE_QUERY_CONDITION = 23; public static final int OA_ARCHIVE_USER_HIS = 24; public static final int OA_ARCHIVE_STUDY_HIS = 25; public static final int OA_ARCHIVE_RESUME_HIS = 26; public static final int OA_ARCHIVE_FAMILY_HIS = 27; public static final int OA_ARCHIVE_PROFESSION_HIS = 28; public static final int OA_ARCHIVE_ASSESS_HIS = 29; public static final int OA_ARCHIVE_REWARDS_HIS = 30; public static final int OA_ARCHIVE_DUTY_HIS = 31; public static final int OA_ARCHIVE_PRIVILEGE = 32; public static final int OA_WORKFLOW_SEQUENCE = 33; public static final int OA_WORKFLOW_MYACTION = 34; public static final int OA_DOCUMENT_ATTACH_CMS = 35; public static final int OA_DEPT_USER = 36; public static final int OA_USER_DESKTOP_SETUP = 37; public static final int OA_SMS_SEND_RECORD = 38; public static final int OA_SCHEDULER = 39; private static final int INCREMENT = 11; private static SequenceManager[] managers; static { managers = new SequenceManager[40]; for (int i=0; i<managers.length; i++) { managers[i] = new SequenceManager(i); } } public static long nextID(int type) { return managers[type].nextUniqueID(); } public long getNextId(int type) { return nextID(type); } public static long nextID() { return managers[0].nextUniqueID(); } private int type; private long currentID; private long maxID; public SequenceManager() { } public SequenceManager(int type) { connname = Global.defaultDB; if (connname.equals("")) logger.info("SequenceManager:默认数据库名为空!"); this.type = type; currentID = 0l; maxID = 0l; } public synchronized long nextUniqueID() { if (! (currentID < maxID)) { getNextBlock(5); } long id = currentID; currentID++; return id; } private void getNextBlock(int count) { if (count == 0) { System.err.println("Failed at last attempt to obtain an ID, aborting..."); return; } boolean success = false; Conn conn = new Conn(connname); PreparedStatement pstmt = null; try { pstmt = conn.prepareStatement(LOAD_ID); pstmt.setInt(1, type); ResultSet rs = pstmt.executeQuery(); if (!rs.next()) { throw new SQLException("Loading the current ID failed. The " + "jiveID table may not be correctly populated."); } long currentID = rs.getLong(1); pstmt.close(); long newID = currentID + INCREMENT; pstmt = conn.prepareStatement(UPDATE_ID); pstmt.setLong(1, newID); pstmt.setInt(2, type); pstmt.setLong(3, currentID); success = pstmt.executeUpdate() == 1; if (success) { this.currentID = currentID; this.maxID = newID; } } catch( Exception sqle ) { sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } if (conn!=null) { conn.close(); conn = null; } } if (!success) { System.err.println("WARNING: failed to obtain next ID block due to " + "thread contention. Trying again..."); try { Thread.currentThread().sleep(75); } catch (InterruptedException ie) { } getNextBlock(count-1); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -