⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 idbroker.java

📁 torque服务器源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package org.apache.torque.oid;/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment: *       "This product includes software developed by the *        Apache Software Foundation (http://www.apache.org/)." *    Alternately, this acknowledgment may appear in the software itself, *    if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and *    "Apache Turbine" must not be used to endorse or promote products *    derived from this software without prior written permission. For *    written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", *    "Apache Turbine", nor may "Apache" appear in their name, without *    prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */import java.math.BigDecimal;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;import java.util.ArrayList;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import org.apache.commons.configuration.Configuration;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.torque.Torque;import org.apache.torque.TorqueException;import org.apache.torque.map.DatabaseMap;import org.apache.torque.map.TableMap;import org.apache.torque.util.Transaction;//!!// NOTE:// It would be nice to decouple this from// Torque. This is a great stand-alone utility./** * This method of ID generation is used to ensure that code is * more database independent.  For example, MySQL has an auto-increment * feature while Oracle uses sequences.  It caches several ids to * avoid needing a Connection for every request. * * This class uses the table ID_TABLE defined in * conf/master/id-table-schema.xml.  The columns in ID_TABLE are used as * follows:<br> * * ID_TABLE_ID - The PK for this row (any unique int).<br> * TABLE_NAME - The name of the table you want ids for.<br> * NEXT_ID - The next id returned by IDBroker when it queries the *           database (not when it returns an id from memory).<br> * QUANTITY - The number of ids that IDBroker will cache in memory.<br> * <p> * Use this class like this: * <pre> * int id = dbMap.getIDBroker().getNextIdAsInt(null, "TABLE_NAME"); *  - or - * BigDecimal[] ids = ((IDBroker)dbMap.getIDBroker()) *     .getNextIds("TABLE_NAME", numOfIdsToReturn); * </pre> * * NOTE: When the ID_TABLE must be updated we must ensure that * IDBroker objects running in different JVMs do not overwrite each * other.  This is accomplished using using the transactional support * occuring in some databases.  Using this class with a database that * does not support transactions should be limited to a single JVM. * * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a> * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a> * @version $Id: IDBroker.java,v 1.27 2003/08/25 16:33:22 henning Exp $ */public class IDBroker implements Runnable, IdGenerator{    /**     * Name of the ID_TABLE = ID_TABLE     */    public static final String ID_TABLE = "ID_TABLE";    /**     * Fully qualified Table_Name column name     */    public static final String TABLE_NAME = ID_TABLE + ".TABLE_NAME";    /**     * Fully qualified Next_ID column name     */    public static final String NEXT_ID = ID_TABLE + ".NEXT_ID";    /**     * Fully qualified Quantity column name     */    public static final String QUANTITY = ID_TABLE + ".QUANTITY";    /** The TableMap referencing the ID_TABLE for this IDBroker. */    private TableMap tableMap;    /**     * The default size of the per-table meta data <code>Hashtable</code>     * objects.     */    private static final int DEFAULT_SIZE = 40;    /**     * The cached IDs for each table.     *     * Key: String table name.     * Value: List of Integer IDs.     */    private Hashtable ids = new Hashtable(DEFAULT_SIZE);    /**     * The quantity of ids to grab for each table.     *     * Key: String table name.     * Value: Integer quantity.     */    private Hashtable quantityStore = new Hashtable(DEFAULT_SIZE);    /**     * The last time this IDBroker queried the database for ids.     *     * Key: String table name.     * Value: Date of last id request.     */    private Hashtable lastQueryTime = new Hashtable(DEFAULT_SIZE);    /**     * Amount of time for the thread to sleep     */    private static final int SLEEP_PERIOD = 1 * 60000;    /**     * The safety Margin     */    private static final float SAFETY_MARGIN = 1.2f;    /**     * The houseKeeperThread thread     */    private Thread houseKeeperThread = null;    /**     * Are transactions supported?     */    private boolean transactionsSupported = false;    /**     * The value of ONE!     */    private static final BigDecimal ONE = new BigDecimal("1");    /** the configuration */    private Configuration configuration;    /** property name */    private static final String DB_IDBROKER_CLEVERQUANTITY =        "idbroker.clever.quantity";    /** property name */    private static final String DB_IDBROKER_PREFETCH =        "idbroker.prefetch";    /** property name */    private static final String DB_IDBROKER_USENEWCONNECTION =        "idbroker.usenewconnection";    /** the log */    private Log log = LogFactory.getLog(IDBroker.class);    /**     * Creates an IDBroker for the ID table.     *     * @param tMap A TableMap.     */    public IDBroker(TableMap tMap)    {        this.tableMap = tMap;        configuration = Torque.getConfiguration();        // Start the housekeeper thread only if prefetch has not been disabled        if (configuration.getBoolean(DB_IDBROKER_PREFETCH, true))        {            houseKeeperThread = new Thread(this);            // Indicate that this is a system thread. JVM will quit only when            // there are no more active user threads. Settings threads spawned            // internally by Torque as daemons allows commandline applications            // using Torque terminate in an orderly manner.            houseKeeperThread.setDaemon(true);            houseKeeperThread.start();        }        // Check for Transaction support.  Give warning message if        // IDBroker is being used with a database that does not        // support transactions.        String dbName = tMap.getDatabaseMap().getName();        Connection dbCon = null;        try        {            dbCon = Torque.getConnection(dbName);            transactionsSupported = dbCon.getMetaData().supportsTransactions();        }        catch (Exception e)        {            transactionsSupported = false;        }        finally        {            try            {                // Return the connection to the pool.                dbCon.close();            }            catch (Exception e)            {            }        }        if (!transactionsSupported)        {            log.warn("IDBroker is being used with db '" + dbName                    + "', which does not support transactions. IDBroker "                    + "attempts to use transactions to limit the possibility "                    + "of duplicate key generation.  Without transactions, "                    + "duplicate key generation is possible if multiple JVMs "                    + "are used or other means are used to write to the "                    + "database.");        }    }    /**     * Set the configuration     *     * @param configuration the configuration     */    public void setConfiguration(Configuration configuration)    {        this.configuration = configuration;    }    /**     * Returns an id as a primitive int.  Note this method does not     * require a Connection, it just implements the KeyGenerator     * interface.  if a Connection is needed one will be requested.     * To force the use of the passed in connection set the configuration     * property torque.idbroker.usenewconnection = false     *     * @param connection A Connection.     * @param tableName an Object that contains additional info.     * @return An int with the value for the id.     * @exception Exception Database error.     */    public int getIdAsInt(Connection connection, Object tableName)        throws Exception    {        return getIdAsBigDecimal(connection, tableName).intValue();    }    /**     * Returns an id as a primitive long. Note this method does not     * require a Connection, it just implements the KeyGenerator     * interface.  if a Connection is needed one will be requested.     * To force the use of the passed in connection set the configuration     * property torque.idbroker.usenewconnection = false     *     * @param connection A Connection.

⌨️ 快捷键说明

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